808 lines
28 KiB
MQL5
808 lines
28 KiB
MQL5
//+------------------------------------------------------------------+
|
|
//| Warrior_EA |
|
|
//| AnimateDread |
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
//--- database classes
|
|
#include "Database\DatabaseManager.mqh"
|
|
//--- available custom classes
|
|
#include "Expert\ExpertCustom.mqh"
|
|
#include "System\PrintVerbose.mqh"
|
|
//--- available signals
|
|
#include "Signals\Signals.mqh"
|
|
//--- available trailing
|
|
#include "Trailing\Trailing.mqh"
|
|
//--- available money management
|
|
#include "Money\Money.mqh"
|
|
//--- Inputs
|
|
#include "Variables\Inputs.mqh"
|
|
//--- Variables
|
|
#include "Variables\Variables.mqh"
|
|
//
|
|
CExpertCustom Expert;
|
|
CDatabaseManager dbm();
|
|
// Helper function to pause execution for a random duration between 1 to 3 seconds
|
|
void RandomSleep()
|
|
{
|
|
Sleep(MathRand() % 2000 + 1000); // Sleeps between 1000ms (1s) and 3000ms (3s)
|
|
}
|
|
// Helper function to retry signal creation with error handling
|
|
template <typename TSignal>
|
|
TSignal* CreateSignalWithRetry(int maxRetries, bool enableFlag)
|
|
{
|
|
if(!enableFlag)
|
|
return NULL;
|
|
TSignal* signal = NULL;
|
|
for(int tries = 0; tries < maxRetries; ++tries)
|
|
{
|
|
signal = new TSignal;
|
|
if(signal == NULL)
|
|
{
|
|
{
|
|
delete signal; // Cleanup failed initialization
|
|
signal = NULL;
|
|
Print("Initialization of signal failed, retrying...");
|
|
RandomSleep();
|
|
}
|
|
}
|
|
else
|
|
break;
|
|
}
|
|
if(signal == NULL)
|
|
{
|
|
Print("Failed to create and initialize signal after retries");
|
|
}
|
|
return signal;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
int OnInit()
|
|
{
|
|
int maxRetryOnError = 5;
|
|
string functionName = __FUNCTION__;
|
|
// Initialize random seed based on the number of milliseconds since the system started
|
|
MathSrand(GetTickCount());
|
|
// Initialize expert
|
|
bool expertInitialized = false;
|
|
for(int tries = 0; !expertInitialized && tries < 5; ++tries)
|
|
{
|
|
if(!Expert.Init(Symbol(), Period(), Expert_EveryTick, Expert_MagicNumber))
|
|
{
|
|
Print(functionName + ": Failed initializing expert, retrying...");
|
|
RandomSleep();
|
|
}
|
|
else
|
|
{
|
|
expertInitialized = true;
|
|
break;
|
|
}
|
|
}
|
|
if(!expertInitialized)
|
|
{
|
|
Print(functionName + ": Failed to initialize expert after retries");
|
|
Expert.Deinit();
|
|
return INIT_FAILED;
|
|
}
|
|
Expert.OnChartEventProcess(true);
|
|
// Creating signal
|
|
PrintVerbose("Initializing Signal...");
|
|
CExpertSignalCustom* signal = NULL;
|
|
for(int tries = 0; signal == NULL && tries < 5; ++tries)
|
|
{
|
|
signal = new CExpertSignalCustom;
|
|
if(signal == NULL)
|
|
{
|
|
Print(functionName + ": Failed to initialize Signal, retrying...");
|
|
RandomSleep();
|
|
}
|
|
else
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
if(signal == NULL)
|
|
{
|
|
Print(functionName + ": Failed to initialize Signal after retries");
|
|
Expert.Deinit();
|
|
return INIT_FAILED;
|
|
}
|
|
InitializeSignal(signal);
|
|
// Initializing Database
|
|
if(UseDatabaseRanking)
|
|
{
|
|
bool dbInitialized = false;
|
|
string databaseFolderStructure[] = {eaName, "Databases", "Signals"};
|
|
const string dbName = Symbol() + "_" + IntegerToString(Period()) + ".db";
|
|
const string dbVersion = "2.0";
|
|
PrintVerbose("Initializing Database...");
|
|
for(int tries = 0; !dbInitialized && tries < 5; ++tries)
|
|
{
|
|
if(!dbm.Init(dbVersion, databaseFolderStructure, dbName))
|
|
{
|
|
Print(functionName + ": Failed to initialize Database, retrying...");
|
|
RandomSleep();
|
|
}
|
|
else
|
|
{
|
|
dbInitialized = true;
|
|
break;
|
|
}
|
|
}
|
|
if(!dbInitialized)
|
|
{
|
|
Print(functionName + ": Failed to initialize Database after retries");
|
|
Expert.Deinit();
|
|
return INIT_FAILED;
|
|
}
|
|
}
|
|
EnablePAI = (AIType == 0 || AIType == 3);
|
|
EnableCONV = (AIType == 1 || AIType == 3);
|
|
EnableLSTM = (AIType == 2 || AIType == 3);
|
|
// Creating instances of signals
|
|
CSignalPAI *PAI = CreateSignalWithRetry<CSignalPAI>(maxRetryOnError, EnablePAI);
|
|
CSignalCONV *CONV = CreateSignalWithRetry<CSignalCONV>(maxRetryOnError, EnableCONV);
|
|
CSignalLSTM *LSTM = CreateSignalWithRetry<CSignalLSTM>(maxRetryOnError, EnableLSTM);
|
|
CSignalNewsFilter *newsFilter = CreateSignalWithRetry<CSignalNewsFilter>(maxRetryOnError, EnableNewsFilter);
|
|
CSignalSessionFilter *sessionFilter = CreateSignalWithRetry<CSignalSessionFilter>(maxRetryOnError, EnableSessionFilter);
|
|
CSignalITF *ITF = CreateSignalWithRetry<CSignalITF>(maxRetryOnError, EnableITF);
|
|
/*
|
|
CSignalAC *AC = CreateSignalWithRetry<CSignalAC>(maxRetryOnError, EnableAC);
|
|
CSignalAO *AO = CreateSignalWithRetry<CSignalAO>(maxRetryOnError, EnableAO);
|
|
CSignalCCI *CCI = CreateSignalWithRetry<CSignalCCI>(maxRetryOnError, EnableCCI);
|
|
//CSignalDTDB *DTDB = CreateSignalWithRetry<CSignalDTDB>(maxRetryOnError, EnableChartPatternSignals);
|
|
//CSignalEB *EB = CreateSignalWithRetry<CSignalEB>(maxRetryOnError, EnableCandlePatternSignals);
|
|
//CSignalIB *IB = CreateSignalWithRetry<CSignalIB>(maxRetryOnError, EnableCandlePatternSignals);
|
|
CSignalMACD *MACD = CreateSignalWithRetry<CSignalMACD>(maxRetryOnError, EnableMACD);
|
|
CSignalMA *MA10 = CreateSignalWithRetry<CSignalMA>(maxRetryOnError, EnableMA10);
|
|
CSignalMA *MA20 = CreateSignalWithRetry<CSignalMA>(maxRetryOnError, EnableMA20);
|
|
CSignalMA *MA50 = CreateSignalWithRetry<CSignalMA>(maxRetryOnError, EnableMA50);
|
|
CSignalMA *MA100 = CreateSignalWithRetry<CSignalMA>(maxRetryOnError, EnableMA100);
|
|
CSignalMA *MA200 = CreateSignalWithRetry<CSignalMA>(maxRetryOnError, EnableMA200);
|
|
//CSignalPB *PB = CreateSignalWithRetry<CSignalPB>(maxRetryOnError, EnableCandlePatternSignals);
|
|
CSignalRSI *RSI = CreateSignalWithRetry<CSignalRSI>(maxRetryOnError, EnableRSI);
|
|
CSignalRVI *RVI = CreateSignalWithRetry<CSignalRVI>(maxRetryOnError, EnableRVI);
|
|
CSignalSAR *SAR = CreateSignalWithRetry<CSignalSAR>(maxRetryOnError, EnableSAR);
|
|
CSignalStoch *STOCH = CreateSignalWithRetry<CSignalStoch>(maxRetryOnError, EnableSTOCH);
|
|
CSignalWPR *WPR = CreateSignalWithRetry<CSignalWPR>(maxRetryOnError, EnableWPR);*/
|
|
// Check for NULL pointers if critical signals failed to initialize
|
|
/*
|
|
if((EnableAC && AC == NULL) || (EnableAO && AO == NULL) || (EnableCCI && CCI == NULL) ||
|
|
(EnableCandlePatternSignals && (EB == NULL || IB == NULL || PB == NULL)) || (EnableChartPatternSignals && (DTDB == NULL)) ||
|
|
(EnableITF && ITF == NULL) || (EnableMACD && MACD == NULL) ||
|
|
(EnableMA10 && MA10 == NULL) || (EnableMA20 && MA20 == NULL) ||
|
|
(EnableMA50 && MA50 == NULL) || (EnableMA100 && MA100 == NULL) ||
|
|
(EnableMA200 && MA200 == NULL) || (EnableNeuralNetworks && PAI == NULL) || (EnableNewsFilter && newsFilter == NULL) ||
|
|
(EnableRSI && RSI == NULL) || (EnableRVI && RVI == NULL) || (EnableSAR && SAR == NULL) ||
|
|
(EnableSessionFilter && sessionFilter == NULL) || (EnableSTOCH && STOCH == NULL) || (EnableWPR && WPR == NULL))
|
|
{
|
|
Print("Critical signal initialization failed, cannot proceed");
|
|
Expert.Deinit();
|
|
return INIT_FAILED;
|
|
}
|
|
*/
|
|
if((EnableITF && ITF == NULL) || (EnablePAI && PAI == NULL) || (EnableCONV && CONV == NULL) || (EnableLSTM && LSTM == NULL) || (EnableNewsFilter && newsFilter == NULL))
|
|
{
|
|
Print("Critical signal initialization failed, cannot proceed");
|
|
Expert.Deinit();
|
|
return INIT_FAILED;
|
|
}
|
|
// Set filter parameters
|
|
if(EnableSessionFilter)
|
|
{
|
|
sessionFilter.TradeLondonSession(SF_trade_LondonSession);
|
|
sessionFilter.TradeNewYorkSession(SF_trade_NewYorkSession);
|
|
sessionFilter.TradeTokyoSession(SF_trade_TokyoSession);
|
|
}
|
|
if(EnableITF)
|
|
{
|
|
ITF.GoodHourOfDay(ITF_GoodHourOfDay);
|
|
ITF.BadHoursOfDay(ITF_BadHoursOfDay);
|
|
ITF.GoodDayOfWeek(ITF_GoodDayOfWeek);
|
|
ITF.BadDaysOfWeek(ITF_BadDaysOfWeek);
|
|
}
|
|
if(EnableNewsFilter)
|
|
{
|
|
newsFilter.SetMinImpact(NF_MinImpact);
|
|
newsFilter.SetLookbackMinutes(NF_LookMinutes);
|
|
}
|
|
if(AIType == 0 || AIType == 3)
|
|
{
|
|
PAI.InitialNeuronsCount(InitialNeurons);
|
|
PAI.OutputNeuronsCount(OutputNeuronsCount);
|
|
PAI.MinNeuronsCount(MinNeuronsCount);
|
|
PAI.HiddenLayersCount(HiddenLayersCount);
|
|
PAI.NeuronsReduction(NeuronsReduction);
|
|
PAI.HistoryBars(ind_Periods);
|
|
PAI.StudyPeriod(StudyPeriods);
|
|
PAI.StopTrainWR(MinWR);
|
|
PAI.OptimizationAlgo(AIOptAlgo);
|
|
if(!UseDatabaseRanking)
|
|
PAI.Weight(1);
|
|
if(AIType == 3)
|
|
PAI.Pattern_0(10);
|
|
PAI.UseVolumes(EnableVolume);
|
|
PAI.UseTime(EnableTime);
|
|
PAI.UseATR(EnableATR);
|
|
PAI.UseAC(EnableAC);
|
|
PAI.UseAlligator(EnableAlligator);
|
|
PAI.UseAO(EnableAO);
|
|
PAI.UseBWMFI(EnableBWMFI);
|
|
PAI.UseGator(EnableGator);
|
|
PAI.UseAD(EnableAD);
|
|
PAI.UseADX(EnableADX);
|
|
PAI.UseBands(EnableBands);
|
|
PAI.UseCCI(EnableCCI);
|
|
PAI.UseMACD(EnableMACD);
|
|
PAI.UseMFI(EnableMFI);
|
|
PAI.UseOBV(EnableOBV);
|
|
PAI.UseRSI(EnableRSI);
|
|
PAI.UseRVI(EnableRVI);
|
|
PAI.UseSTOCH(EnableSTOCH);
|
|
PAI.UseSAR(EnableSAR);
|
|
PAI.UseWPR(EnableWPR);
|
|
}
|
|
if(AIType == 1 || AIType == 3)
|
|
{
|
|
CONV.InitialNeuronsCount(InitialNeurons);
|
|
CONV.OutputNeuronsCount(OutputNeuronsCount);
|
|
CONV.MinNeuronsCount(MinNeuronsCount);
|
|
CONV.HiddenLayersCount(HiddenLayersCount);
|
|
CONV.NeuronsReduction(NeuronsReduction);
|
|
CONV.HistoryBars(ind_Periods);
|
|
CONV.StudyPeriod(StudyPeriods);
|
|
CONV.StopTrainWR(MinWR);
|
|
CONV.OptimizationAlgo(AIOptAlgo);
|
|
if(!UseDatabaseRanking)
|
|
CONV.Weight(1);
|
|
if(AIType == 3)
|
|
CONV.Pattern_0(10);
|
|
CONV.UseVolumes(EnableVolume);
|
|
CONV.UseTime(EnableTime);
|
|
CONV.UseATR(EnableATR);
|
|
CONV.UseAC(EnableAC);
|
|
CONV.UseAlligator(EnableAlligator);
|
|
CONV.UseAO(EnableAO);
|
|
CONV.UseBWMFI(EnableBWMFI);
|
|
CONV.UseGator(EnableGator);
|
|
CONV.UseAD(EnableAD);
|
|
CONV.UseADX(EnableADX);
|
|
CONV.UseBands(EnableBands);
|
|
CONV.UseCCI(EnableCCI);
|
|
CONV.UseMACD(EnableMACD);
|
|
CONV.UseMFI(EnableMFI);
|
|
CONV.UseOBV(EnableOBV);
|
|
CONV.UseRSI(EnableRSI);
|
|
CONV.UseRVI(EnableRVI);
|
|
CONV.UseSTOCH(EnableSTOCH);
|
|
CONV.UseSAR(EnableSAR);
|
|
CONV.UseWPR(EnableWPR);
|
|
}
|
|
if(AIType == 2 || AIType == 3)
|
|
{
|
|
LSTM.InitialNeuronsCount(InitialNeurons);
|
|
LSTM.OutputNeuronsCount(OutputNeuronsCount);
|
|
LSTM.MinNeuronsCount(MinNeuronsCount);
|
|
LSTM.HiddenLayersCount(HiddenLayersCount);
|
|
LSTM.NeuronsReduction(NeuronsReduction);
|
|
LSTM.HistoryBars(ind_Periods);
|
|
LSTM.StudyPeriod(StudyPeriods);
|
|
LSTM.StopTrainWR(MinWR);
|
|
LSTM.OptimizationAlgo(AIOptAlgo);
|
|
if(!UseDatabaseRanking)
|
|
LSTM.Weight(1);
|
|
if(AIType == 3)
|
|
LSTM.Pattern_0(10);
|
|
LSTM.UseVolumes(EnableVolume);
|
|
LSTM.UseTime(EnableTime);
|
|
LSTM.UseATR(EnableATR);
|
|
LSTM.UseAC(EnableAC);
|
|
LSTM.UseAlligator(EnableAlligator);
|
|
LSTM.UseAO(EnableAO);
|
|
LSTM.UseBWMFI(EnableBWMFI);
|
|
LSTM.UseGator(EnableGator);
|
|
LSTM.UseAD(EnableAD);
|
|
LSTM.UseADX(EnableADX);
|
|
LSTM.UseBands(EnableBands);
|
|
LSTM.UseCCI(EnableCCI);
|
|
LSTM.UseMACD(EnableMACD);
|
|
LSTM.UseMFI(EnableMFI);
|
|
LSTM.UseOBV(EnableOBV);
|
|
LSTM.UseRSI(EnableRSI);
|
|
LSTM.UseRVI(EnableRVI);
|
|
LSTM.UseSTOCH(EnableSTOCH);
|
|
LSTM.UseSAR(EnableSAR);
|
|
LSTM.UseWPR(EnableWPR);
|
|
}
|
|
/*
|
|
if(EnableChartPatternSignals)
|
|
{
|
|
DTDB.LookBackPeriod(ind_Periods);
|
|
}
|
|
if(EnableMA10)
|
|
{
|
|
MA10.PeriodMA(10);
|
|
MA10.ID("MA10");
|
|
MA10.Method(MethodMA);
|
|
}
|
|
if(EnableMA20)
|
|
{
|
|
MA20.PeriodMA(20);
|
|
MA20.ID("MA20");
|
|
MA20.Method(MethodMA);
|
|
}
|
|
if(EnableMA50)
|
|
{
|
|
MA50.PeriodMA(50);
|
|
MA50.ID("MA50");
|
|
MA50.Method(MethodMA);
|
|
}
|
|
if(EnableMA100)
|
|
{
|
|
MA100.PeriodMA(100);
|
|
MA100.ID("MA100");
|
|
MA100.Method(MethodMA);
|
|
}
|
|
if(EnableMA200)
|
|
{
|
|
MA200.PeriodMA(200);
|
|
MA200.ID("MA200");
|
|
MA200.Method(MethodMA);
|
|
}
|
|
if(EnableCCI)
|
|
{
|
|
CCI.PeriodCCI(ind_Periods);
|
|
}
|
|
if(EnableRSI)
|
|
{
|
|
RSI.PeriodRSI(ind_Periods);
|
|
}
|
|
if(EnableSTOCH)
|
|
{
|
|
STOCH.PeriodK(ind_Periods);
|
|
}
|
|
if(EnableWPR)
|
|
{
|
|
WPR.PeriodWPR(ind_Periods);
|
|
}
|
|
if(EnableRVI)
|
|
{
|
|
RVI.PeriodRVI(ind_Periods);
|
|
}*/
|
|
// Add filters
|
|
PrintVerbose("Initializing Signal filters...");
|
|
bool filterSuccess = false;
|
|
for(int tries = 0; tries < maxRetryOnError; ++tries)
|
|
{
|
|
if(UseDatabaseRanking && !dbm.OpenDatabase())
|
|
{
|
|
Print(functionName + ": Error opening database, retrying...");
|
|
RandomSleep();
|
|
continue;
|
|
}
|
|
if(UseDatabaseRanking && !dbm.BeginTransaction())
|
|
{
|
|
Print(functionName + ": Error starting transaction, retrying...");
|
|
dbm.CloseDatabase(); // Ensure the database is closed before retry
|
|
RandomSleep();
|
|
continue;
|
|
}
|
|
// Attempt to add all necessary filters
|
|
bool filtersAdded = true;
|
|
filtersAdded &= (EnableITF ? AddFilterToSignal(signal, ITF) : true);
|
|
filtersAdded &= (EnableSessionFilter ? AddFilterToSignal(signal, sessionFilter) : true);
|
|
filtersAdded &= (EnableNewsFilter ? AddFilterToSignal(signal, newsFilter) : true);
|
|
filtersAdded &= (EnablePAI ? AddFilterToSignal(signal, PAI) : true);
|
|
filtersAdded &= (EnableCONV ? AddFilterToSignal(signal, CONV) : true);
|
|
filtersAdded &= (EnableLSTM ? AddFilterToSignal(signal, LSTM) : true);
|
|
//filtersAdded &= (EnableCandlePatternSignals ? AddFilterToSignal(signal, IB) : true);
|
|
//filtersAdded &= (EnableCandlePatternSignals ? AddFilterToSignal(signal, PB) : true);
|
|
//filtersAdded &= (EnableCandlePatternSignals ? AddFilterToSignal(signal, EB) : true);
|
|
//filtersAdded &= (EnableChartPatternSignals ? AddFilterToSignal(signal, DTDB) : true);
|
|
//filtersAdded &= (EnableAC ? AddFilterToSignal(signal, AC) : true);
|
|
//filtersAdded &= (EnableAO ? AddFilterToSignal(signal, AO) : true);
|
|
//filtersAdded &= (EnableCCI ? AddFilterToSignal(signal, CCI) : true);
|
|
//filtersAdded &= (EnableMA10 ? AddFilterToSignal(signal, MA10) : true);
|
|
//filtersAdded &= (EnableMA20 ? AddFilterToSignal(signal, MA20) : true);
|
|
//filtersAdded &= (EnableMA50 ? AddFilterToSignal(signal, MA50) : true);
|
|
//filtersAdded &= (EnableMA100 ? AddFilterToSignal(signal, MA100) : true);
|
|
//filtersAdded &= (EnableMA200 ? AddFilterToSignal(signal, MA200) : true);
|
|
//filtersAdded &= (EnableMACD ? AddFilterToSignal(signal, MACD) : true);
|
|
//filtersAdded &= (EnableRSI ? AddFilterToSignal(signal, RSI) : true);
|
|
//filtersAdded &= (EnableRVI ? AddFilterToSignal(signal, RVI) : true);
|
|
//filtersAdded &= (EnableSAR ? AddFilterToSignal(signal, SAR) : true);
|
|
//filtersAdded &= (EnableSTOCH ? AddFilterToSignal(signal, STOCH) : true);
|
|
//filtersAdded &= (EnableWPR ? AddFilterToSignal(signal, WPR) : true);
|
|
if(!filtersAdded)
|
|
{
|
|
Print(functionName + ": Error loading filters, retrying...");
|
|
RandomSleep();
|
|
continue;
|
|
}
|
|
if(UseDatabaseRanking && (!dbm.CommitTransaction() || !dbm.CloseDatabase()))
|
|
{
|
|
Print(functionName + ": Error committing transaction or closing database, retrying...");
|
|
RandomSleep();
|
|
continue;
|
|
}
|
|
filterSuccess = true;
|
|
break; // Success if all operations complete without error
|
|
}
|
|
if(!filterSuccess)
|
|
{
|
|
Print(functionName + ": Failed after all retries");
|
|
Expert.Deinit();
|
|
return INIT_FAILED; // Return failure if retries are exhausted
|
|
}
|
|
// Trailing logic
|
|
PrintVerbose("Initializing Trailing...");
|
|
bool trailingInitialized = false;
|
|
for(int tries = 0; !trailingInitialized && tries < maxRetryOnError; ++tries)
|
|
{
|
|
if(!InitializeTrailing())
|
|
{
|
|
Print(functionName + ": Failed to initialize Trailing, retrying...");
|
|
RandomSleep();
|
|
}
|
|
else
|
|
{
|
|
trailingInitialized = true;
|
|
break;
|
|
}
|
|
}
|
|
if(!trailingInitialized)
|
|
{
|
|
Print(functionName + ": Failed to initialize Trailing after retries");
|
|
Expert.Deinit();
|
|
return INIT_FAILED;
|
|
}
|
|
// Creation of money object
|
|
bool moneyManagementInitialized = false;
|
|
for(int tries = 0; !moneyManagementInitialized && tries < maxRetryOnError; ++tries)
|
|
{
|
|
if(!InitializeMoneyManagement())
|
|
{
|
|
Print(functionName + ": Failed to initialize Money Management, retrying...");
|
|
RandomSleep();
|
|
}
|
|
else
|
|
{
|
|
moneyManagementInitialized = true;
|
|
break;
|
|
}
|
|
}
|
|
if(!moneyManagementInitialized)
|
|
{
|
|
Print(functionName + ": Failed to initialize Money Management after retries");
|
|
Expert.Deinit();
|
|
return INIT_FAILED;
|
|
}
|
|
// Check all trading objects parameters
|
|
PrintVerbose("Validating settings...");
|
|
bool settingsValidated = false;
|
|
for(int tries = 0; !settingsValidated && tries < maxRetryOnError; ++tries)
|
|
{
|
|
if(!Expert.ValidationSettings())
|
|
{
|
|
Print(functionName + ": Failed to validate settings, retrying...");
|
|
RandomSleep();
|
|
}
|
|
else
|
|
{
|
|
settingsValidated = true;
|
|
break;
|
|
}
|
|
}
|
|
if(!settingsValidated)
|
|
{
|
|
Print(functionName + ": Failed to validate settings after retries");
|
|
Expert.Deinit();
|
|
return INIT_FAILED;
|
|
}
|
|
// Tuning of all necessary indicators
|
|
PrintVerbose("Initializing Indicators...");
|
|
bool indicatorsInitialized = false;
|
|
for(int tries = 0; !indicatorsInitialized && tries < maxRetryOnError; ++tries)
|
|
{
|
|
if(!Expert.InitIndicators())
|
|
{
|
|
Print(functionName + ": Failed to initialize Indicators, retrying...");
|
|
RandomSleep();
|
|
}
|
|
else
|
|
{
|
|
indicatorsInitialized = true;
|
|
break;
|
|
}
|
|
}
|
|
if(!indicatorsInitialized)
|
|
{
|
|
Print(functionName + ": Failed to initialize Indicators after retries");
|
|
Expert.Deinit();
|
|
return INIT_FAILED;
|
|
}
|
|
// setting timer for periodic optimisation of weights
|
|
if(UseDatabaseRanking)
|
|
{
|
|
Expert.OnTimerProcess(true);
|
|
bool timerSet = false;
|
|
int timerInterval = 3600;
|
|
for(int tries = 0; !timerSet && tries < maxRetryOnError; ++tries)
|
|
{
|
|
if(!EventSetTimer(timerInterval))
|
|
{
|
|
Print(functionName + ": Error creating timer, retrying...");
|
|
RandomSleep();
|
|
}
|
|
else
|
|
{
|
|
timerSet = true;
|
|
break;
|
|
}
|
|
}
|
|
if(!timerSet)
|
|
{
|
|
Print(functionName + ": Failed to set timer after retries");
|
|
Expert.Deinit();
|
|
return INIT_FAILED;
|
|
}
|
|
}
|
|
// Initialization successful
|
|
PrintVerbose("Initialization successful");
|
|
return INIT_SUCCEEDED;
|
|
}
|
|
// Called before optimization/backtesting in the strategy tester
|
|
int OnTesterInit()
|
|
{
|
|
//g_startTime = TimeCurrent();
|
|
IsBacktesting = true;
|
|
return(INIT_SUCCEEDED);
|
|
}
|
|
// Called after EA optimization in the strategy tester
|
|
void OnTesterDeinit()
|
|
{
|
|
dbm.Deinit();
|
|
IsBacktesting = false;
|
|
OnDeinit(0);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
void OnDeinit(const int reason)
|
|
{
|
|
EventKillTimer();
|
|
dbm.Deinit();
|
|
Expert.Deinit();
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
void OnTimer()
|
|
{
|
|
Expert.OnTimer();
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
void OnTick()
|
|
{
|
|
Expert.OnTick();
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
void OnChartEvent(const int id,
|
|
const long &lparam,
|
|
const double &dparam,
|
|
const string &sparam)
|
|
{
|
|
Expert.OnChartEvent(id, lparam, dparam, sparam);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
bool AddFilterToSignal(CExpertSignalCustom * signal, CExpertSignalCustom * filter)
|
|
{
|
|
if(filter == NULL)
|
|
{
|
|
Print(__FUNCTION__ + "Error creating filters");
|
|
Expert.Deinit();
|
|
return false;
|
|
}
|
|
return signal.AddFilter(filter);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
bool InitializeSignal(CExpertSignalCustom * signal_obj)
|
|
{
|
|
if(signal_obj == NULL)
|
|
{
|
|
Print(__FUNCTION__ + ": error creating signal");
|
|
Expert.Deinit();
|
|
return false;
|
|
}
|
|
Expert.InitSignal(signal_obj);
|
|
signal_obj.Entry_Multiplier(Entry_Multiplier);
|
|
signal_obj.Expiration(Signal_Expiration);
|
|
signal_obj.Periods(ind_Periods);
|
|
if(AIType == 3)
|
|
{
|
|
signal_obj.ThresholdOpen(20);
|
|
signal_obj.ThresholdClose(20);
|
|
}
|
|
else
|
|
{
|
|
signal_obj.ThresholdOpen(10);
|
|
signal_obj.ThresholdClose(10);
|
|
}
|
|
return true;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
// Initialize Trailing
|
|
bool InitializeTrailing()
|
|
{
|
|
if(TrailingStrategy == TRAILING_STRATEGY_NONE)
|
|
{
|
|
// No trailing strategy selected
|
|
return true;
|
|
}
|
|
else
|
|
if(TrailingStrategy == TRAILING_STRATEGY_MA_10 ||
|
|
TrailingStrategy == TRAILING_STRATEGY_MA_20 ||
|
|
TrailingStrategy == TRAILING_STRATEGY_MA_50 ||
|
|
TrailingStrategy == TRAILING_STRATEGY_MA_100 ||
|
|
TrailingStrategy == TRAILING_STRATEGY_MA_200)
|
|
{
|
|
// Moving Average Trailing Strategy
|
|
int period = 0;
|
|
if(TrailingStrategy == TRAILING_STRATEGY_MA_10)
|
|
period = 10;
|
|
else
|
|
if(TrailingStrategy == TRAILING_STRATEGY_MA_20)
|
|
period = 20;
|
|
else
|
|
if(TrailingStrategy == TRAILING_STRATEGY_MA_50)
|
|
period = 50;
|
|
else
|
|
if(TrailingStrategy == TRAILING_STRATEGY_MA_100)
|
|
period = 100;
|
|
else
|
|
if(TrailingStrategy == TRAILING_STRATEGY_MA_200)
|
|
period = 200;
|
|
CTrailingMA *trailing = new CTrailingMA;
|
|
if(trailing == NULL)
|
|
{
|
|
Print(__FUNCTION__ + ": error creating trailing");
|
|
Expert.Deinit();
|
|
return false;
|
|
}
|
|
// Trailing Parameters
|
|
trailing.Period(period);
|
|
trailing.Shift(0);
|
|
trailing.Method(MethodMA);
|
|
trailing.Applied(PRICE_CLOSE);
|
|
if(!Expert.InitTrailing(trailing))
|
|
{
|
|
Print(__FUNCTION__ + ": error initializing trailing");
|
|
Expert.Deinit();
|
|
return false;
|
|
}
|
|
}
|
|
else
|
|
if(TrailingStrategy == TRAILING_STRATEGY_ATR_x1 ||
|
|
TrailingStrategy == TRAILING_STRATEGY_ATR_x2 ||
|
|
TrailingStrategy == TRAILING_STRATEGY_ATR_x3)
|
|
{
|
|
// ATR Trailing Strategy
|
|
double multiplier = 0;
|
|
if(TrailingStrategy == TRAILING_STRATEGY_ATR_x1)
|
|
multiplier = 1;
|
|
else
|
|
if(TrailingStrategy == TRAILING_STRATEGY_ATR_x2)
|
|
multiplier = 2;
|
|
else
|
|
if(TrailingStrategy == TRAILING_STRATEGY_ATR_x3)
|
|
multiplier = 3;
|
|
CTrailingATR *trailing = new CTrailingATR;
|
|
if(trailing == NULL)
|
|
{
|
|
Print(__FUNCTION__ + ": error creating trailing");
|
|
Expert.Deinit();
|
|
return false;
|
|
}
|
|
// Set ATR Multiplier
|
|
trailing.Multiplier(multiplier);
|
|
if(!Expert.InitTrailing(trailing))
|
|
{
|
|
Print(__FUNCTION__ + ": error initializing trailing");
|
|
Expert.Deinit();
|
|
return false;
|
|
}
|
|
}
|
|
else
|
|
if(TrailingStrategy == TRAILING_STRATEGY_SAR)
|
|
{
|
|
// SAR Trailing Strategy
|
|
CTrailingPSAR *trailing = new CTrailingPSAR;
|
|
if(trailing == NULL)
|
|
{
|
|
Print(__FUNCTION__ + ": error creating trailing");
|
|
Expert.Deinit();
|
|
return false;
|
|
}
|
|
if(!Expert.InitTrailing(trailing))
|
|
{
|
|
Print(__FUNCTION__ + ": error initializing trailing");
|
|
Expert.Deinit();
|
|
return false;
|
|
}
|
|
}
|
|
// Add more trailing strategies if needed
|
|
return true;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
bool InitializeMoneyManagement()
|
|
{
|
|
string functionName = __FUNCTION__;
|
|
if(MM_STRATEGY == FIXED_RISK)
|
|
{
|
|
CMoneyFixedRisk *money = new CMoneyFixedRisk;
|
|
if(money == NULL)
|
|
{
|
|
Print(functionName + ": error creating money");
|
|
Expert.Deinit();
|
|
return false;
|
|
}
|
|
if(!Expert.InitMoney(money))
|
|
{
|
|
Print(functionName + ": error initializing money");
|
|
Expert.Deinit();
|
|
return false;
|
|
}
|
|
money.Percent(Money_Risk_Percent);
|
|
}
|
|
else
|
|
if(MM_STRATEGY == FIXED_LOT)
|
|
{
|
|
CMoneyFixedLot *money = new CMoneyFixedLot;
|
|
if(money == NULL)
|
|
{
|
|
Print(functionName + ": error creating money");
|
|
Expert.Deinit();
|
|
return false;
|
|
}
|
|
if(!Expert.InitMoney(money))
|
|
{
|
|
Print(functionName + ": error initializing money");
|
|
Expert.Deinit();
|
|
return false;
|
|
}
|
|
money.Lots(Money_FixLot_Lots);
|
|
}
|
|
else
|
|
if(MM_STRATEGY == INTELLIGENT)
|
|
{
|
|
CMoneyIntelligent *money = new CMoneyIntelligent;
|
|
if(money == NULL)
|
|
{
|
|
Print(functionName + ": error creating money");
|
|
Expert.Deinit();
|
|
return false;
|
|
}
|
|
if(!Expert.InitMoney(money))
|
|
{
|
|
Print(functionName + ": error initializing money");
|
|
Expert.Deinit();
|
|
return false;
|
|
}
|
|
money.Percent(Money_Risk_Percent);
|
|
}
|
|
// Add more money management strategies if needed
|
|
return true;
|
|
}
|
|
//+------------------------------------------------------------------+
|