Warrior_EA/Expert/ExpertSignalAIBase.mqh
super.admin 0a527b0cf9 convert
2025-05-30 16:35:54 +02:00

1685 lines
55 KiB
MQL5

//+------------------------------------------------------------------+
//| Warrior_EA |
//| AnimateDread |
//| |
//+------------------------------------------------------------------+
#include "ExpertSignalCustom.mqh"
#include "..\AI\Network.mqh"
class CExpertSignalAIBase : public CExpertSignalCustom
{
protected:
string ID;
CiOpen m_Open;
CiClose m_Close;
CiHigh m_High;
CiLow m_Low;
CiVolumes m_Volumes;
CiTime m_Time;
CiAD m_AD;
CiADX m_ADX;
CiAC m_AC;
CiAlligator m_Alligator;
CiAO m_AO;
CiBWMFI m_BWMFI;
CiGator m_Gator;
CiBands m_Bands;
CiCCI m_CCI;
CiMACD m_MACD;
CiMFI m_MFI;
CiOBV m_OBV;
CiRSI m_RSI;
CiRVI m_RVI;
CiStochastic m_STOCH;
CiSAR m_SAR;
CiWPR m_WPR;
CNet *Net;
CArrayDouble *TempData;
double dError;
double dUndefine;
double dForecast;
double dPrevSignal;
datetime dtStudied;
bool bEventStudy;
//--- variables
int m_buttonHandle; // Handle for the play/pause button
bool m_isPlaying; // Flag to indicate whether training is playing or paused
string m_fileName;
string m_folderPath;
int m_optimizationAlgo;
int m_historyBars;
int m_outputNeuronsCount;
int m_minNeuronsCount;
int m_initialNeuronsCount;
int m_neuronsCount;
double m_neuronsReduction;
int m_hiddenLayersCount;
int m_studyPeriod;
int m_minTrainYear;
bool m_isInitialized;
int m_stopTrainWR;
int m_fractalPeriods;
//--- functions
virtual bool InitIndicators(CIndicators *indicators);
void DrawObject(datetime time, double signal, double high, double low);
void DeleteObject(datetime time);
void PurgeChart(void);
ENUM_SIGNAL DoubleToSignal(double value);
bool ResizeBuffers(int barIndex);
bool RefreshData();
bool BufferTempData(int idx);
void Train(datetime StartTrainBar = 0);
//--- method of initialization of the indicators
bool InitOpen(CIndicators *indicators);
bool InitClose(CIndicators *indicators);
bool InitHigh(CIndicators *indicators);
bool InitLow(CIndicators *indicators);
bool InitVolumes(CIndicators *indicators);
bool InitTime(CIndicators *indicators);
bool InitAC(CIndicators *indicators);
bool InitAlligator(CIndicators *indicators);
bool InitAO(CIndicators *indicators);
bool InitBWMFI(CIndicators *indicators);
bool InitGator(CIndicators *indicators);
bool InitAD(CIndicators *indicators);
bool InitADX(CIndicators *indicators);
bool InitBands(CIndicators *indicators);
bool InitCCI(CIndicators *indicators);
bool InitMACD(CIndicators *indicators);
bool InitMFI(CIndicators *indicators);
bool InitOBV(CIndicators *indicators);
bool InitRSI(CIndicators *indicators);
bool InitRVI(CIndicators *indicators);
bool InitSTOCH(CIndicators *indicators);
bool InitSAR(CIndicators *indicators);
bool InitWPR(CIndicators *indicators);
bool SaveTopologyConfiguration(string fileName, int initialNeuronsCount, int hiddenLayersCount, double neuronsReduction, int minNeuronsCount, int optimizationAlgo, int historyBars, int outputNeuronsCount, int neuronsCount, int studyPeriod, int minTrainYear, bool isInitialized, int stopTrainWR, int fractalPeriods);
bool LoadAndCompareTopologyConfiguration(string fileName, int initialNeuronsCount, int hiddenLayersCount, double neuronsReduction, int minNeuronsCount, int optimizationAlgo, int historyBars, int outputNeuronsCount, int neuronsCount, int studyPeriod, int minTrainYear, bool isInitialized, int stopTrainWR, int fractalPeriods);
//--- input data
bool m_useVolumes;
bool m_useTime;
bool m_useATR;
bool m_useAC;
bool m_useAlligator;
bool m_useAO;
bool m_useBWMFI;
bool m_useGator;
bool m_useAD;
bool m_useADX;
bool m_useBands;
bool m_useCCI;
bool m_useMACD;
bool m_useMFI;
bool m_useOBV;
bool m_useRSI;
bool m_useRVI;
bool m_useSTOCH;
bool m_useSAR;
bool m_useWPR;
public:
CExpertSignalAIBase(void);
~CExpertSignalAIBase(void);
//--- methods of setting adjustable parameters
void OptimizationAlgo(int algo) { m_optimizationAlgo = algo; }
void InitialNeuronsCount(int value) { m_initialNeuronsCount = value; }
void OutputNeuronsCount(int value) { m_outputNeuronsCount = value; }
void MinNeuronsCount(int value) { m_minNeuronsCount = value; }
void HiddenLayersCount(int value) { m_hiddenLayersCount = value; }
void NeuronsReduction(int value) { m_neuronsReduction = value; }
void StopTrainWR(int value) { m_stopTrainWR = value; }
void HistoryBars(int value) { m_historyBars = value; }
void StudyPeriod(int value) { m_studyPeriod = value; }
void MinTrainYear(int value) { m_minTrainYear = value; }
void UseVolumes(bool value) { m_useVolumes = value; }
void UseTime(bool value) { m_useTime = value; }
void UseATR(bool value) { m_useATR = value; }
void UseAC(bool value) { m_useAC = value; }
void UseAlligator(bool value) { m_useAlligator = value; }
void UseAO(bool value) { m_useAO = value; };
void UseBWMFI(bool value) { m_useBWMFI = value; };
void UseGator(bool value) { m_useGator = value; };
void UseAD(bool value) { m_useAD = value; }
void UseADX(bool value) { m_useADX = value; }
void UseBands(bool value) { m_useBands = value; }
void UseCCI(bool value) { m_useCCI = value; }
void UseMACD(bool value) { m_useMACD = value; }
void UseMFI(bool value) { m_useMFI = value; }
void UseOBV(bool value) { m_useOBV = value; }
void UseRSI(bool value) { m_useRSI = value; }
void UseRVI(bool value) { m_useRVI = value; }
void UseSTOCH(bool value) { m_useSTOCH = value; }
void UseSAR(bool value) { m_useSAR = value; }
void UseWPR(bool value) { m_useWPR = value; }
};
//+------------------------------------------------------------------+
//| Constructor |
//+------------------------------------------------------------------+
CExpertSignalAIBase::CExpertSignalAIBase(void) :
ID("NULL"),
m_neuronsCount(0),
m_studyPeriod(10),
m_minTrainYear(1970),
m_optimizationAlgo(0),
m_initialNeuronsCount(1000),
m_outputNeuronsCount(3),
m_minNeuronsCount(20),
m_neuronsReduction(0.5),
m_hiddenLayersCount(4),
m_historyBars(14),
m_stopTrainWR(70),
m_fractalPeriods(5),
m_useVolumes(true),
m_useTime(true),
m_useATR(true),
m_useAC(false),
m_useAlligator(false),
m_useAO(false),
m_useBWMFI(false),
m_useGator(false),
m_useAD(false),
m_useADX(false),
m_useBands(false),
m_useCCI(false),
m_useMACD(false),
m_useMFI(false),
m_useOBV(false),
m_useRSI(false),
m_useRVI(false),
m_useSTOCH(false),
m_useSAR(false),
m_useWPR(false),
Net(NULL),
TempData(NULL),
dError(-1),
dUndefine(0),
dForecast(0),
dPrevSignal(0),
dtStudied(0),
bEventStudy(false),
m_isInitialized(false)
{
}
//+------------------------------------------------------------------+
//| Destructor |
//+------------------------------------------------------------------+
CExpertSignalAIBase::~CExpertSignalAIBase(void)
{
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CExpertSignalAIBase::InitIndicators(CIndicators *indicators)
{
PurgeChart();
if(!InitOpen(indicators))
return false;
if(!InitClose(indicators))
return false;
if(!InitLow(indicators))
return false;
if(!InitHigh(indicators))
return false;
m_neuronsCount = 3; // set by default for price data
if(m_useVolumes)
{
m_neuronsCount++;
if(!InitVolumes(indicators))
return false;
}
if(m_useTime)
{
m_neuronsCount += 6;
if(!InitTime(indicators))
return false;
}
if(m_useATR)
{
//already init in the base class
m_neuronsCount++;
}
if(m_useAC)
{
if(!InitAC(indicators))
return false;
m_neuronsCount++;
}
if(m_useAlligator)
{
if(!InitAlligator(indicators))
return false;
m_neuronsCount += 12;
}
if(m_useAO)
{
if(!InitAO(indicators))
return false;
m_neuronsCount++;
}
if(m_useBWMFI)
{
if(!InitBWMFI(indicators))
return false;
m_neuronsCount++;
}
if(m_useGator)
{
if(!InitGator(indicators))
return false;
m_neuronsCount += 8;
}
if(m_useAD)
{
if(!InitAD(indicators))
return false;
m_neuronsCount++;
}
if(m_useADX)
{
if(!InitADX(indicators))
return false;
m_neuronsCount += 3;
}
if(m_useBands)
{
if(!InitBands(indicators))
return false;
m_neuronsCount += 12;
}
if(m_useCCI)
{
if(!InitCCI(indicators))
return false;
m_neuronsCount++;
}
if(m_useMACD)
{
if(!InitMACD(indicators))
return false;
m_neuronsCount += 2;
}
if(m_useMFI)
{
if(!InitMFI(indicators))
return false;
m_neuronsCount++;
}
if(m_useOBV)
{
if(!InitOBV(indicators))
return false;
m_neuronsCount++;
}
if(m_useRSI)
{
if(!InitRSI(indicators))
return false;
m_neuronsCount++;
}
if(m_useRVI)
{
if(!InitRVI(indicators))
return false;
m_neuronsCount += 2;
}
if(m_useSTOCH)
{
if(!InitSTOCH(indicators))
return false;
m_neuronsCount += 2;
}
if(m_useSAR)
{
if(!InitSAR(indicators))
return false;
m_neuronsCount += 4;
}
if(m_useWPR)
{
if(!InitWPR(indicators))
return false;
m_neuronsCount++;
}
if(!FolderCreate(m_folderPath, FILE_COMMON))
{
if(GetLastError() != 5010) // If the error is not because the folder already exists
{
Print("Failed to create folder: " + m_folderPath);
}
else
{
ResetLastError(); // Reset the error code
}
}
return true;
}
//+------------------------------------------------------------------+
//| Training and Signal Methods |
//+------------------------------------------------------------------+
void CExpertSignalAIBase::Train(datetime StartTrainBar = 0)
{
int count = 0;
//---
MqlDateTime start_time;
TimeCurrent(start_time);
start_time.year -= m_studyPeriod;
if(start_time.year <= 0 || start_time.year < m_minTrainYear)
start_time.year = m_minTrainYear;
datetime st_time = StructToTime(start_time);
dtStudied = MathMax(StartTrainBar, st_time);
double prev_up = -1;
double prev_for = -1;
double prev_er = -1;
datetime bar_time = 0;
bool stop = IsStopped();
//---
do
{
int bars = (int)MathMin(Bars(m_symbol.Name(), PERIOD_CURRENT, dtStudied, TimeCurrent()) + m_historyBars, Bars(m_symbol.Name(), PERIOD_CURRENT));
prev_up = dUndefine;
prev_for = dForecast;
prev_er = dError;
ENUM_SIGNAL bar = Neutral;
long tick = 0;
if(!ResizeBuffers(bars) || !RefreshData())
{
break;
return;
}
stop = IsStopped();
bool add_loop = false;
// Iterate over the bars
for(int i = (int)(bars - MathMax(m_historyBars, 0) - 1); i >= 0 && !stop; i--)
{
string s;
if(m_outputNeuronsCount == 1)
{
s = StringFormat(
ID + " : Study -> Era %d\n" +
"Bar %d of %d -> %.2f%%\n" +
"Accuracy: %.2f%%\n" +
"Mean Squared Error: %.2f\n" +
"Recent Average Error: %.2f\n" +
"Signal Neuron: %.5f\n" +
"Forecast: %s -> %.2f",
count,
bars - i + 1, bars, (double)(bars - i + 1.0) / bars * 100,
dForecast,
dError,
Net.getRecentAverageError(),
TempData[0],
EnumToString(DoubleToSignal(dPrevSignal)), dPrevSignal
);
}
else
if(m_outputNeuronsCount == 3)
{
s = StringFormat(
ID + " : Study -> Era %d\n" +
"Bar %d of %d -> %.2f%%\n" +
"Accuracy: %.2f%%\n" +
"Mean Squared Error: %.2f\n" +
"Recent Average Error: %.2f\n" +
"Buy Neuron: %.5f - Sell Neuron: %.5f - Neutral Neuron: %.5f\n" +
"Forecast: %s -> %.2f",
count,
bars - i + 1, bars, (double)(bars - i + 1.0) / bars * 100,
dForecast,
dError,
Net.getRecentAverageError(),
TempData[0], TempData[1], TempData[2],
EnumToString(DoubleToSignal(dPrevSignal)), dPrevSignal
);
}
else
{
s = "Invalid neuron count!";
}
Comment(s);
if(add_loop && i < (int)(bars - MathMax(m_historyBars, 0) - 1) && i > 1 && m_Time.GetData(i) > dtStudied && dPrevSignal != -2)
{
TempData.Clear();
bool sell = false, buy = false;
for(int j = 1; j <= m_fractalPeriods; j++)
{
if(m_Low.GetData(i) < m_Low.GetData(i + j) && m_Low.GetData(i) < m_Low.GetData(i - j))
buy = true;
if(m_High.GetData(i) > m_High.GetData(i + j) && m_High.GetData(i) > m_High.GetData(i - j))
sell = true;
}
if(m_outputNeuronsCount == 1)
{
TempData.Add(buy && !sell ? 1 : !buy && sell ? -1 : 0);
}
else
if(m_outputNeuronsCount == 3)
{
TempData.Add(buy ? 1.0 : 0.0); // 1 for buy
TempData.Add(sell ? 1.0 : 0.0); // 1 for sell
TempData.Add((!buy && !sell) ? 1.0 : 0.0); // 1 for neutral
}
if(TempData.Total() > 0)
{
Net.backProp(TempData);
if(DoubleToSignal(dPrevSignal) == Buy || DoubleToSignal(dPrevSignal) == Sell || DoubleToSignal(dPrevSignal) == Neutral)
{
if(DoubleToSignal(dPrevSignal) == DoubleToSignal(TempData.At(0)))
dForecast += (100 - dForecast) / Net.recentAverageSmoothingFactor;
else
dForecast -= dForecast / Net.recentAverageSmoothingFactor;
dUndefine -= dUndefine / Net.recentAverageSmoothingFactor;
}
else
{
if((buy && sell))
dUndefine += (100 - dUndefine) / Net.recentAverageSmoothingFactor;
}
}
}
TempData.Clear();
int r = i + (int)m_historyBars;
if(r > bars)
continue;
for(int b = 0; b < (int)m_historyBars; b++)
{
int bar_t = r + b;
if(!BufferTempData(bar_t))
{
break;
return;
}
}
if(TempData.Total() >= (int)m_historyBars * m_neuronsCount)
{
add_loop = true;
Net.feedForward(TempData);
Net.getResults(TempData);
if(m_outputNeuronsCount == 1)
dPrevSignal = TempData[0];
else
if(m_outputNeuronsCount == 3)
{
double sum = 0;
for(int res = 0; res < 3; res++)
{
double temp = exp(TempData.At(res));
sum += temp;
TempData.Update(res, temp);
}
for(int res = 0; res < 3; res++)
TempData.Update(res, TempData.At(res) / sum);
//---
switch(TempData.Maximum(0, 3))
{
case 0:
dPrevSignal = TempData[0]; // Buy signal
break;
case 1:
dPrevSignal = -TempData[1]; // Sell signal
break;
case 2:
dPrevSignal = 0; // Neutral signal
break;
default:
dPrevSignal = 0; // Default to Neutral if something goes wrong
break;
}
}
bar_time = m_Time.GetData(i);
if(i > 0)
{
if(DoubleToSignal(dPrevSignal) == Neutral)
DeleteObject(bar_time);
else
DrawObject(bar_time, dPrevSignal, m_High.GetData(i), m_Low.GetData(i));
}
}
stop = IsStopped();
}
if(add_loop)
count++;
if(!stop)
{
dError = Net.getRecentAverageError();
if(add_loop)
{
Net.Save(m_fileName + ".nnw", dError, dUndefine, dForecast, dtStudied, true);
}
}
}
while((dForecast < m_stopTrainWR || dError >= 0.1 || MathAbs(dError - prev_er) >= 0.01 || MathAbs(dUndefine - prev_up) >= 0.1 || MathAbs(dForecast - prev_for) >= 0.1) && !stop);
if(count > 0)
{
dtStudied = bar_time;
}
}
//+------------------------------------------------------------------+
//| Converts a double to ENUM_SIGNAL |
//+------------------------------------------------------------------+
ENUM_SIGNAL CExpertSignalAIBase::DoubleToSignal(double value)
{
value = NormalizeDouble(value, 2); // Round 'value' to two decimal places
if(value > 0.50 && value <= 1.0)
return Buy;
else
if(value < -0.50 && value >= -1.0)
return Sell;
else
if(value >= -0.50 && value <= 0.50)
return Neutral;
else
return Undefine;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CExpertSignalAIBase::DrawObject(datetime time, double signal, double high, double low)
{
double price = 0;
int arrow = 0;
color clr = 0;
ENUM_ARROW_ANCHOR anch = ANCHOR_BOTTOM;
switch(DoubleToSignal(signal))
{
case Buy:
price = low;
arrow = 217;
clr = clrBlue;
anch = ANCHOR_TOP;
break;
case Sell:
price = high;
arrow = 218;
clr = clrRed;
anch = ANCHOR_BOTTOM;
break;
}
if(price == 0 || arrow == 0)
return;
string name = TimeToString(time);
if(ObjectFind(0, name) < 0)
{
ResetLastError();
if(!ObjectCreate(0, name, OBJ_ARROW, 0, time, 0))
{
printf("Error of creating object %d", GetLastError());
return;
}
}
ObjectSetDouble(0, name, OBJPROP_PRICE, price);
ObjectSetInteger(0, name, OBJPROP_ARROWCODE, arrow);
ObjectSetInteger(0, name, OBJPROP_COLOR, clr);
ObjectSetInteger(0, name, OBJPROP_ANCHOR, anch);
ObjectSetString(0, name, OBJPROP_TOOLTIP, EnumToString(DoubleToSignal(signal)) + " " + DoubleToString(signal, 5));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CExpertSignalAIBase::DeleteObject(datetime time)
{
string name = TimeToString(time);
if(ObjectFind(0, name) >= 0)
ObjectDelete(0, name);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CExpertSignalAIBase::ResizeBuffers(int barIndex)
{
if(!m_Open.BufferResize(barIndex) || !m_Close.BufferResize(barIndex) || !m_High.BufferResize(barIndex) || !m_Low.BufferResize(barIndex))
return false;
if(m_useVolumes)
{
if(!m_Volumes.BufferResize(barIndex))
return false;
}
if(m_useTime)
{
if(!m_Time.BufferResize(barIndex))
return false;
}
if(m_useATR)
{
if(!m_ATR.BufferResize(barIndex))
return false;
}
if(m_useAC)
{
if(!m_AC.BufferResize(barIndex))
return false;
}
if(m_useAlligator)
{
if(!m_Alligator.BufferResize(barIndex))
return false;
}
if(m_useAO)
{
if(!m_AO.BufferResize(barIndex))
return false;
}
if(m_useBWMFI)
{
if(!m_BWMFI.BufferResize(barIndex))
return false;
}
if(m_useGator)
{
if(!m_Gator.BufferResize(barIndex))
return false;
}
if(m_useAD)
{
if(!m_AD.BufferResize(barIndex))
return false;
}
if(m_useADX)
{
if(!m_ADX.BufferResize(barIndex))
return false;
}
if(m_useBands)
{
if(!m_Bands.BufferResize(barIndex))
return false;
}
if(m_useCCI)
{
if(!m_CCI.BufferResize(barIndex))
return false;
}
if(m_useMACD)
{
if(!m_MACD.BufferResize(barIndex))
return false;
}
if(m_useMFI)
{
if(!m_MFI.BufferResize(barIndex))
return false;
}
if(m_useOBV)
{
if(!m_OBV.BufferResize(barIndex))
return false;
}
if(m_useRSI)
{
if(!m_RSI.BufferResize(barIndex))
return false;
}
if(m_useRVI)
{
if(!m_RVI.BufferResize(barIndex))
return false;
}
if(m_useSTOCH)
{
if(!m_STOCH.BufferResize(barIndex))
return false;
}
if(m_useSAR)
{
if(!m_SAR.BufferResize(barIndex))
return false;
}
if(m_useWPR)
{
if(!m_WPR.BufferResize(barIndex))
return false;
}
return true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CExpertSignalAIBase::RefreshData()
{
m_Open.Refresh(OBJ_ALL_PERIODS);
m_Close.Refresh(OBJ_ALL_PERIODS);
m_High.Refresh(OBJ_ALL_PERIODS);
m_Low.Refresh(OBJ_ALL_PERIODS);
if(m_useVolumes)
{
m_Volumes.Refresh(OBJ_ALL_PERIODS);
}
if(m_useTime)
{
m_Time.Refresh(OBJ_ALL_PERIODS);
}
if(m_useATR)
{
m_ATR.Refresh(OBJ_ALL_PERIODS);
}
if(m_useAC)
{
m_AC.Refresh(OBJ_ALL_PERIODS);
}
if(m_useAlligator)
{
m_Alligator.Refresh(OBJ_ALL_PERIODS);
}
if(m_useAO)
{
m_AO.Refresh(OBJ_ALL_PERIODS);
}
if(m_useBWMFI)
{
m_BWMFI.Refresh(OBJ_ALL_PERIODS);
}
if(m_useGator)
{
m_Gator.Refresh(OBJ_ALL_PERIODS);
}
if(m_useAD)
{
m_AD.Refresh(OBJ_ALL_PERIODS);
}
if(m_useADX)
{
m_ADX.Refresh(OBJ_ALL_PERIODS);
}
if(m_useBands)
{
m_Bands.Refresh(OBJ_ALL_PERIODS);
}
if(m_useCCI)
{
m_CCI.Refresh(OBJ_ALL_PERIODS);
}
if(m_useMACD)
{
m_MACD.Refresh(OBJ_ALL_PERIODS);
}
if(m_useMFI)
{
m_MFI.Refresh(OBJ_ALL_PERIODS);
}
if(m_useOBV)
{
m_OBV.Refresh(OBJ_ALL_PERIODS);
}
if(m_useRSI)
{
m_RSI.Refresh(OBJ_ALL_PERIODS);
}
if(m_useRVI)
{
m_RVI.Refresh(OBJ_ALL_PERIODS);
}
if(m_useSTOCH)
{
m_STOCH.Refresh(OBJ_ALL_PERIODS);
}
if(m_useSAR)
{
m_SAR.Refresh(OBJ_ALL_PERIODS);
}
if(m_useWPR)
{
m_WPR.Refresh(OBJ_ALL_PERIODS);
}
return true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CExpertSignalAIBase::BufferTempData(int idx)
{
double open = m_Open.GetData(idx);
double close = m_Close.GetData(idx);
double high = m_High.GetData(idx);
double low = m_Low.GetData(idx);
MqlDateTime sTime;
TimeToStruct(m_Time.GetData(idx), sTime);
if(open == EMPTY_VALUE)
return false;
if(!TempData.Add(close - open) ||
!TempData.Add(high - open) ||
!TempData.Add(low - open))
{
return false;
}
if(m_useVolumes)
{
if(!TempData.Add(m_Volumes.Main(idx) - m_Volumes.Main(idx + 1)))
return false;
}
if(m_useTime)
{
// Normalize time (cyclical encoding)
if(!TempData.Add(sin(2 * M_PI * sTime.hour / 24.0)))
return false;
if(!TempData.Add(cos(2 * M_PI * sTime.hour / 24.0)))
return false;
if(!TempData.Add(sin(2 * M_PI * sTime.day_of_week / 7.0)))
return false;
if(!TempData.Add(cos(2 * M_PI * sTime.day_of_week / 7.0)))
return false;
if(!TempData.Add(sin(2 * M_PI * sTime.mon / 12.0)))
return false;
if(!TempData.Add(cos(2 * M_PI * sTime.mon / 12.0)))
return false;
}
if(m_useATR)
{
if(!TempData.Add(m_ATR.Main(idx)))
return false;
}
if(m_useAC)
{
if(!TempData.Add(m_AC.Main(idx)))
return false;
}
if(m_useAlligator)
{
if(!TempData.Add(m_Alligator.Jaw(idx) - open)
||
(!TempData.Add(m_Alligator.Teeth(idx) - open))
||
(!TempData.Add(m_Alligator.Lips(idx) - open))
||
(!TempData.Add(m_Alligator.Jaw(idx) - close))
||
(!TempData.Add(m_Alligator.Teeth(idx) - close))
||
(!TempData.Add(m_Alligator.Lips(idx) - close))
||
(!TempData.Add(m_Alligator.Jaw(idx) - high))
||
(!TempData.Add(m_Alligator.Teeth(idx) - high))
||
(!TempData.Add(m_Alligator.Lips(idx) - high))
||
(!TempData.Add(m_Alligator.Jaw(idx) - low))
||
(!TempData.Add(m_Alligator.Teeth(idx) - low))
||
(!TempData.Add(m_Alligator.Lips(idx) - low)))
return false;
}
if(m_useAO)
{
if(!TempData.Add(m_AO.Main(idx)))
return false;
}
if(m_useBWMFI)
{
if(!TempData.Add(m_BWMFI.Main(idx)))
return false;
}
if(m_useGator)
{
if(!TempData.Add(m_Gator.Upper(idx) - open) ||
(!TempData.Add(m_Gator.Lower(idx) - open)) ||
(!TempData.Add(m_Gator.Upper(idx) - close)) ||
(!TempData.Add(m_Gator.Lower(idx) - close)) ||
(!TempData.Add(m_Gator.Upper(idx) - high)) ||
(!TempData.Add(m_Gator.Lower(idx) - high)) ||
(!TempData.Add(m_Gator.Upper(idx) - low)) ||
(!TempData.Add(m_Gator.Lower(idx) - low)))
return false;
}
if(m_useAD)
{
if(!TempData.Add(m_AD.Main(idx) - m_AD.Main(idx + 1)))
return false;
}
if(m_useADX)
{
if(!TempData.Add(m_ADX.Main(idx)) || !TempData.Add(m_ADX.Plus(idx)) || !TempData.Add(m_ADX.Minus(idx)))
return false;
}
if(m_useBands)
{
if(!TempData.Add(m_Bands.Upper(idx) - open) ||
(!TempData.Add(m_Bands.Base(idx) - open)) ||
(!TempData.Add(m_Bands.Lower(idx) - open)) ||
(!TempData.Add(m_Bands.Upper(idx) - close)) ||
(!TempData.Add(m_Bands.Base(idx) - close)) ||
(!TempData.Add(m_Bands.Lower(idx) - close)) ||
(!TempData.Add(m_Bands.Upper(idx) - high)) ||
(!TempData.Add(m_Bands.Base(idx) - high)) ||
(!TempData.Add(m_Bands.Lower(idx) - high)) ||
(!TempData.Add(m_Bands.Upper(idx) - low)) ||
(!TempData.Add(m_Bands.Base(idx) - low)) ||
(!TempData.Add(m_Bands.Lower(idx) - low)))
return false;
}
if(m_useCCI)
{
if(!TempData.Add(m_CCI.Main(idx)))
return false;
}
if(m_useMACD)
{
if(!TempData.Add(m_MACD.Main(idx)) || !TempData.Add(m_MACD.Signal(idx)))
return false;
}
if(m_useMFI)
{
if(!TempData.Add(m_MFI.Main(idx)))
return false;
}
if(m_useOBV)
{
if(!TempData.Add(m_OBV.Main(idx) - m_OBV.Main(idx + 1)))
return false;
}
if(m_useRSI)
{
if(!TempData.Add(m_RSI.Main(idx)))
return false;
}
if(m_useRVI)
{
if(!TempData.Add(m_RVI.Main(idx)) || !TempData.Add(m_RVI.Signal(idx)))
return false;
}
if(m_useSTOCH)
{
if(!TempData.Add(m_STOCH.Main(idx)) || !TempData.Add(m_STOCH.Signal(idx)))
return false;
}
if(m_useSAR)
{
if(!TempData.Add(m_SAR.Main(idx) - open) ||
(!TempData.Add(m_SAR.Main(idx) - close)) ||
(!TempData.Add(m_SAR.Main(idx) - high)) ||
(!TempData.Add(m_SAR.Main(idx) - low)))
return false;
}
if(m_useWPR)
{
if(!TempData.Add(m_WPR.Main(idx)))
return false;
}
return true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CExpertSignalAIBase::SaveTopologyConfiguration(string fileName, int initialNeuronsCount, int hiddenLayersCount, double neuronsReduction, int minNeuronsCount, int optimizationAlgo, int historyBars, int outputNeuronsCount, int neuronsCount, int studyPeriod, int minTrainYear, bool isInitialized, int stopTrainWR, int fractalPeriods)
{
string configFileName = fileName + ".cfg";
int handle = FileOpen(configFileName, FILE_WRITE | FILE_BIN | FILE_COMMON);
if(handle == INVALID_HANDLE)
{
Print("Error: Unable to open file ", configFileName);
ResetLastError();
return false;
}
if(FileWriteInteger(handle, initialNeuronsCount) < sizeof(int))
{
Print("Error writing initialNeuronsCount : Error code: ", GetLastError());
ResetLastError();
return false;
}
if(FileWriteInteger(handle, hiddenLayersCount) < sizeof(int))
{
Print("Error writing hiddenLayersCount : Error code: ", GetLastError());
ResetLastError();
return false;
}
if(FileWriteDouble(handle, neuronsReduction) < sizeof(double))
{
Print("Error writing neuronsReduction : Error code: ", GetLastError());
ResetLastError();
return false;
}
if(FileWriteInteger(handle, minNeuronsCount) < sizeof(int))
{
Print("Error writing minNeuronsCount : Error code: ", GetLastError());
ResetLastError();
return false;
}
if(FileWriteInteger(handle, optimizationAlgo) < sizeof(int))
{
Print("Error writing optimizationAlgo : Error code: ", GetLastError());
ResetLastError();
return false;
}
if(FileWriteInteger(handle, historyBars) < sizeof(int))
{
Print("Error writing historyBars : Error code: ", GetLastError());
ResetLastError();
return false;
}
if(FileWriteInteger(handle, outputNeuronsCount) < sizeof(int))
{
Print("Error writing outputNeuronsCount : Error code: ", GetLastError());
ResetLastError();
return false;
}
if(FileWriteInteger(handle, neuronsCount) < sizeof(int))
{
Print("Error writing neuronsCount : Error code: ", GetLastError());
ResetLastError();
return false;
}
if(FileWriteInteger(handle, studyPeriod) < sizeof(int))
{
Print("Error writing studyPeriod : Error code: ", GetLastError());
ResetLastError();
return false;
}
if(FileWriteInteger(handle, minTrainYear) < sizeof(int))
{
Print("Error writing minTrainYear : Error code: ", GetLastError());
ResetLastError();
return false;
}
if(FileWriteInteger(handle, isInitialized) < sizeof(int))
{
Print("Error writing isInitialized : Error code: ", GetLastError());
ResetLastError();
return false;
}
if(FileWriteInteger(handle, stopTrainWR) < sizeof(int))
{
Print("Error writing stopTrainWR : Error code: ", GetLastError());
ResetLastError();
return false;
}
if(FileWriteInteger(handle, fractalPeriods) < sizeof(int))
{
Print("Error writing fractalPeriods : Error code: ", GetLastError());
ResetLastError();
return false;
}
FileClose(handle);
return true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CExpertSignalAIBase::LoadAndCompareTopologyConfiguration(string fileName, int initialNeuronsCount, int hiddenLayersCount, double neuronsReduction, int minNeuronsCount, int optimizationAlgo, int historyBars, int outputNeuronsCount, int neuronsCount, int studyPeriod, int minTrainYear, bool isInitialized, int stopTrainWR, int fractalPeriods)
{
string configFileName = fileName + ".cfg";
if(!FileIsExist(configFileName, FILE_COMMON))
{
Print("Error: File ", configFileName, " does not exist");
return false;
}
int handle = FileOpen(configFileName, FILE_READ | FILE_BIN | FILE_COMMON);
if(handle == INVALID_HANDLE)
{
Print("Error: Unable to open file ", configFileName);
return false;
}
int savedInitialNeurons = FileReadInteger(handle);
int savedHiddenLayers = FileReadInteger(handle);
double savedReductionFactor = FileReadDouble(handle);
int savedMinNeurons = FileReadInteger(handle);
int savedOptimizationAlgo = FileReadInteger(handle);
int savedHistoryBars = FileReadInteger(handle);
int savedOutputNeuronsCount = FileReadInteger(handle);
int savedNeuronsCount = FileReadInteger(handle);
int savedStudyPeriod = FileReadInteger(handle);
int savedMinTrainYear = FileReadInteger(handle);
bool savedIsInitialized = FileReadInteger(handle);
int savedStopTrainWR = FileReadInteger(handle);
int savedFractalPeriods = FileReadInteger(handle);
FileClose(handle);
if(savedInitialNeurons != initialNeuronsCount ||
savedHiddenLayers != hiddenLayersCount ||
savedReductionFactor != neuronsReduction ||
savedMinNeurons != minNeuronsCount ||
savedOptimizationAlgo != optimizationAlgo ||
savedHistoryBars != historyBars ||
savedOutputNeuronsCount != outputNeuronsCount ||
savedNeuronsCount != neuronsCount ||
savedStudyPeriod != studyPeriod ||
savedMinTrainYear != minTrainYear ||
savedIsInitialized != isInitialized ||
savedStopTrainWR != stopTrainWR ||
savedFractalPeriods != fractalPeriods)
{
Print("Error: Configuration mismatch. Deleting file ", configFileName);
FileDelete(configFileName, FILE_COMMON);
return false;
}
return true;
}
//+------------------------------------------------------------------+
//| PurgeChart - Removes all visual objects from the current chart |
//+------------------------------------------------------------------+
void CExpertSignalAIBase::PurgeChart(void)
{
// Specify the current chart ID (0 for the current chart)
long chartID = 0;
// Remove all objects of any type (use specific type if needed)
ObjectsDeleteAll(chartID);
}
//+------------------------------------------------------------------+
//| Initialize Open indicators. |
//+------------------------------------------------------------------+
bool CExpertSignalAIBase::InitOpen(CIndicators * indicators)
{
//--- check pointer
if(indicators == NULL)
return (false);
//--- add object to collection
if(!indicators.Add(GetPointer(m_Open)))
{
printf(__FUNCTION__ + ": error adding object");
return (false);
}
//--- initialize object
if(!m_Open.Create(m_symbol.Name(), m_period))
{
printf(__FUNCTION__ + ": error initializing object");
return (false);
}
//--- ok
return (true);
}
//+------------------------------------------------------------------+
//| Initialize Close indicators. |
//+------------------------------------------------------------------+
bool CExpertSignalAIBase::InitClose(CIndicators * indicators)
{
//--- check pointer
if(indicators == NULL)
return (false);
//--- add object to collection
if(!indicators.Add(GetPointer(m_Close)))
{
printf(__FUNCTION__ + ": error adding object");
return (false);
}
//--- initialize object
if(!m_Close.Create(m_symbol.Name(), m_period))
{
printf(__FUNCTION__ + ": error initializing object");
return (false);
}
//--- ok
return (true);
}
//+------------------------------------------------------------------+
//| Initialize High indicators. |
//+------------------------------------------------------------------+
bool CExpertSignalAIBase::InitHigh(CIndicators * indicators)
{
//--- check pointer
if(indicators == NULL)
return (false);
//--- add object to collection
if(!indicators.Add(GetPointer(m_High)))
{
printf(__FUNCTION__ + ": error adding object");
return (false);
}
//--- initialize object
if(!m_High.Create(m_symbol.Name(), m_period))
{
printf(__FUNCTION__ + ": error initializing object");
return (false);
}
//--- ok
return (true);
}
//+------------------------------------------------------------------+
//| Initialize Low indicators. |
//+------------------------------------------------------------------+
bool CExpertSignalAIBase::InitLow(CIndicators * indicators)
{
//--- check pointer
if(indicators == NULL)
return (false);
//--- add object to collection
if(!indicators.Add(GetPointer(m_Low)))
{
printf(__FUNCTION__ + ": error adding object");
return (false);
}
//--- initialize object
if(!m_Low.Create(m_symbol.Name(), m_period))
{
printf(__FUNCTION__ + ": error initializing object");
return (false);
}
//--- ok
return (true);
}
//+------------------------------------------------------------------+
//| Initialize Time indicators. |
//+------------------------------------------------------------------+
bool CExpertSignalAIBase::InitTime(CIndicators * indicators)
{
//--- check pointer
if(indicators == NULL)
return (false);
//--- add object to collection
if(!indicators.Add(GetPointer(m_Time)))
{
printf(__FUNCTION__ + ": error adding object");
return (false);
}
//--- initialize object
if(!m_Time.Create(m_symbol.Name(), m_period))
{
printf(__FUNCTION__ + ": error initializing object");
return (false);
}
//--- ok
return (true);
}
//+------------------------------------------------------------------+
//| Initialize Volumes indicators. |
//+------------------------------------------------------------------+
bool CExpertSignalAIBase::InitVolumes(CIndicators * indicators)
{
//--- check pointer
if(indicators == NULL)
return (false);
//--- add object to collection
if(!indicators.Add(GetPointer(m_Volumes)))
{
printf(__FUNCTION__ + ": error adding object");
return (false);
}
//--- initialize object
if(!m_Volumes.Create(m_symbol.Name(), m_period, VolumeData))
{
printf(__FUNCTION__ + ": error initializing object");
return (false);
}
//--- ok
return (true);
}
//+------------------------------------------------------------------+
//| Initialize AC indicators. |
//+------------------------------------------------------------------+
bool CExpertSignalAIBase::InitAC(CIndicators * indicators)
{
//--- check pointer
if(indicators == NULL)
return (false);
//--- add object to collection
if(!indicators.Add(GetPointer(m_AC)))
{
printf(__FUNCTION__ + ": error adding object");
return (false);
}
//--- initialize object
if(!m_AC.Create(m_symbol.Name(), m_period))
{
printf(__FUNCTION__ + ": error initializing object");
return (false);
}
//--- ok
return (true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CExpertSignalAIBase::InitAlligator(CIndicators * indicators)
{
//--- check pointer
if(indicators == NULL)
return (false);
int idx = StartIndex();
//--- add object to collection
if(!indicators.Add(GetPointer(m_Alligator)))
{
printf(__FUNCTION__ + ": error adding object");
return (false);
}
//--- initialize object
if(!m_Alligator.Create(m_symbol.Name(), m_period, 13, idx, 8, idx, 5, idx, MODE_SMMA, PRICE_MEDIAN))
{
printf(__FUNCTION__ + ": error initializing object");
return (false);
}
//--- ok
return (true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CExpertSignalAIBase::InitAO(CIndicators * indicators)
{
//--- check pointer
if(indicators == NULL)
return (false);
//--- add object to collection
if(!indicators.Add(GetPointer(m_AO)))
{
printf(__FUNCTION__ + ": error adding object");
return (false);
}
//--- initialize object
if(!m_AO.Create(m_symbol.Name(), m_period))
{
printf(__FUNCTION__ + ": error initializing object");
return (false);
}
//--- ok
return (true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CExpertSignalAIBase::InitBWMFI(CIndicators * indicators)
{
//--- check pointer
if(indicators == NULL)
return (false);
//--- add object to collection
if(!indicators.Add(GetPointer(m_BWMFI)))
{
printf(__FUNCTION__ + ": error adding object");
return (false);
}
//--- initialize object
if(!m_BWMFI.Create(m_symbol.Name(), m_period, VolumeData))
{
printf(__FUNCTION__ + ": error initializing object");
return (false);
}
//--- ok
return (true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CExpertSignalAIBase::InitGator(CIndicators * indicators)
{
//--- check pointer
if(indicators == NULL)
return (false);
int idx = StartIndex();
//--- add object to collection
if(!indicators.Add(GetPointer(m_Gator)))
{
printf(__FUNCTION__ + ": error adding object");
return (false);
}
//--- initialize object
if(!m_Gator.Create(m_symbol.Name(), m_period, 13, idx, 8, idx, 5, idx, MODE_SMMA, PRICE_MEDIAN))
{
printf(__FUNCTION__ + ": error initializing object");
return (false);
}
//--- ok
return (true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CExpertSignalAIBase::InitAD(CIndicators * indicators)
{
//--- check pointer
if(indicators == NULL)
return (false);
//--- add object to collection
if(!indicators.Add(GetPointer(m_AD)))
{
printf(__FUNCTION__ + ": error adding object");
return (false);
}
//--- initialize object
if(!m_AD.Create(m_symbol.Name(), m_period, VolumeData))
{
printf(__FUNCTION__ + ": error initializing object");
return (false);
}
//--- ok
return (true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CExpertSignalAIBase::InitADX(CIndicators * indicators)
{
//--- check pointer
if(indicators == NULL)
return (false);
//--- add object to collection
if(!indicators.Add(GetPointer(m_ADX)))
{
printf(__FUNCTION__ + ": error adding object");
return (false);
}
//--- initialize object
if(!m_ADX.Create(m_symbol.Name(), m_period, 14))
{
printf(__FUNCTION__ + ": error initializing object");
return (false);
}
//--- ok
return (true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CExpertSignalAIBase::InitBands(CIndicators * indicators)
{
int idx = StartIndex();
//--- check pointer
if(indicators == NULL)
return (false);
//--- add object to collection
if(!indicators.Add(GetPointer(m_Bands)))
{
printf(__FUNCTION__ + ": error adding object");
return (false);
}
//--- initialize object
if(!m_Bands.Create(m_symbol.Name(), m_period, 20, idx, 2.0, PRICE_CLOSE))
{
printf(__FUNCTION__ + ": error initializing object");
return (false);
}
//--- ok
return (true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CExpertSignalAIBase::InitCCI(CIndicators * indicators)
{
//--- check pointer
if(indicators == NULL)
return (false);
//--- add object to collection
if(!indicators.Add(GetPointer(m_CCI)))
{
printf(__FUNCTION__ + ": error adding object");
return (false);
}
//--- initialize object
if(!m_CCI.Create(m_symbol.Name(), m_period, 14, PRICE_TYPICAL))
{
printf(__FUNCTION__ + ": error initializing object");
return (false);
}
//--- ok
return (true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CExpertSignalAIBase::InitMACD(CIndicators * indicators)
{
//--- check pointer
if(indicators == NULL)
return (false);
//--- add object to collection
if(!indicators.Add(GetPointer(m_MACD)))
{
printf(__FUNCTION__ + ": error adding object");
return (false);
}
//--- initialize object
if(!m_MACD.Create(m_symbol.Name(), m_period, 12, 26, 9, PRICE_CLOSE))
{
printf(__FUNCTION__ + ": error initializing object");
return (false);
}
//--- ok
return (true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CExpertSignalAIBase::InitMFI(CIndicators * indicators)
{
//--- check pointer
if(indicators == NULL)
return (false);
//--- add object to collection
if(!indicators.Add(GetPointer(m_MFI)))
{
printf(__FUNCTION__ + ": error adding object");
return (false);
}
//--- initialize object
if(!m_MFI.Create(m_symbol.Name(), m_period, 14, VolumeData))
{
printf(__FUNCTION__ + ": error initializing object");
return (false);
}
//--- ok
return (true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CExpertSignalAIBase::InitOBV(CIndicators * indicators)
{
//--- check pointer
if(indicators == NULL)
return (false);
//--- add object to collection
if(!indicators.Add(GetPointer(m_OBV)))
{
printf(__FUNCTION__ + ": error adding object");
return (false);
}
//--- initialize object
if(!m_OBV.Create(m_symbol.Name(), m_period, VolumeData))
{
printf(__FUNCTION__ + ": error initializing object");
return (false);
}
//--- ok
return (true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CExpertSignalAIBase::InitRSI(CIndicators * indicators)
{
//--- check pointer
if(indicators == NULL)
return (false);
//--- add object to collection
if(!indicators.Add(GetPointer(m_RSI)))
{
printf(__FUNCTION__ + ": error adding object");
return (false);
}
//--- initialize object
if(!m_RSI.Create(m_symbol.Name(), m_period, 14, PRICE_CLOSE))
{
printf(__FUNCTION__ + ": error initializing object");
return (false);
}
//--- ok
return (true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CExpertSignalAIBase::InitRVI(CIndicators * indicators)
{
//--- check pointer
if(indicators == NULL)
return (false);
//--- add object to collection
if(!indicators.Add(GetPointer(m_RVI)))
{
printf(__FUNCTION__ + ": error adding object");
return (false);
}
//--- initialize object
if(!m_RVI.Create(m_symbol.Name(), m_period, 10))
{
printf(__FUNCTION__ + ": error initializing object");
return (false);
}
//--- ok
return (true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CExpertSignalAIBase::InitSTOCH(CIndicators * indicators)
{
//--- check pointer
if(indicators == NULL)
return (false);
//--- add object to collection
if(!indicators.Add(GetPointer(m_STOCH)))
{
printf(__FUNCTION__ + ": error adding object");
return (false);
}
//--- initialize object
if(!m_STOCH.Create(m_symbol.Name(), m_period, 5, 3, 3, MODE_SMA, STO_LOWHIGH))
{
printf(__FUNCTION__ + ": error initializing object");
return (false);
}
//--- ok
return (true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CExpertSignalAIBase::InitSAR(CIndicators * indicators)
{
//--- check pointer
if(indicators == NULL)
return (false);
//--- add object to collection
if(!indicators.Add(GetPointer(m_SAR)))
{
printf(__FUNCTION__ + ": error adding object");
return (false);
}
//--- initialize object
if(!m_SAR.Create(m_symbol.Name(), m_period, 0.02, 0.2))
{
printf(__FUNCTION__ + ": error initializing object");
return (false);
}
//--- ok
return (true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CExpertSignalAIBase::InitWPR(CIndicators * indicators)
{
//--- check pointer
if(indicators == NULL)
return (false);
//--- add object to collection
if(!indicators.Add(GetPointer(m_WPR)))
{
printf(__FUNCTION__ + ": error adding object");
return (false);
}
//--- initialize object
if(!m_WPR.Create(m_symbol.Name(), m_period, 14))
{
printf(__FUNCTION__ + ": error initializing object");
return (false);
}
//--- ok
return (true);
}
//+------------------------------------------------------------------+