-
Notifications
You must be signed in to change notification settings - Fork 2
/
AlgoMasterNNFX.mqh
563 lines (439 loc) · 16.5 KB
/
AlgoMasterNNFX.mqh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#define PROGRAM_NAME "NNFX_Am"
#include "Backtester/CompleteNNFXTester.mqh"
//#include "Backtester/ExternalFunctions.mqh"
#include "Program/Parameters.mqh"
#include "Symbols/Import.mqh"
#include "Graphics/EVZNewsGraphicImport.mqh"
#include "Backtester/CustomIndicators.mqh"
#ifdef __MQL5__
#resource "Indicators\\EuroFXVix.ex5"
#resource "Indicators\\NewsIndicator.ex5"
#resource "Indicators\\ATR.ex5"
#else
#resource "Indicators\\EuroFXVix.ex4"
#resource "Indicators\\NewsIndicator.ex4"
#resource "Indicators\\ATR.ex4"
#endif
CGraphicProgram* program;
CompleteNNFXTester *backtester;
string symbolsToTrade[];
int totalSymbols;
bool detectTicks = false;
bool isTester;
int InitEvent()
{
isTester = MQLInfoInteger(MQL_TESTER);
if (!isTester)
{
program = new CGraphicProgram();
if (!program.Create("AlgoMaster"))
return INIT_FAILED;
program.Run();
return INIT_SUCCEEDED;
}
else //isTester
{
if (!CheckExpertParameters()) return INIT_PARAMETERS_INCORRECT;
//double tradeValue = (riskPercent /2.0) * AccountInfoDouble(ACCOUNT_BALANCE)/100.0;
totalSymbols = CSymbolProcessorFactory::ProcessSymbols(
symbolString,
symbolsToTrade,
pairsPreset);
bool useStats = (optimizationMode != N_EQUITY_COMP); //(optimizationMode == N_DIST_VALUE || optimizationMode == N_DIST_SHAPE);
backtester = new CompleteNNFXTester(riskPercent/200.0, symbolsToTrade, applyPullbackRule, applyOneCandleRule, applyBridgeTooFar, scaleOut, optimizationMode==N_TOTAL_PIPS, useMainForExit, minimalPercent/100.0, stopLossAtr, takeProfitAtr, startMoveAtr, true, useStats);
backtester.SetDistanceBaseline(baselineAtr);
backtester.SetBridgeTooFarCount(bridgeTooFarCount);
backtester.SetMainCatchUp(applyCatchUp);
backtester.SetIndicator(MAIN_IND, mainBuffer, signalBuffer, crossLevel, invertOperative, widthFilter, indicatorMode);
#ifdef __MQL5__
backtester.SetIndicatorColors(MAIN_IND, colorBuy, colorSell);
#else
backtester.SetIndicatorColors(MAIN_IND, colorSell, colorSell);
#endif
if (use2Confirm)
{
backtester.SetIndicator(SECOND_IND, mainBuffer2nd, signalBuffer2nd, crossLevel2nd, invertOperative2nd, widthFilter2nd, indicatorMode2nd);
#ifdef __MQL5__
backtester.SetIndicatorColors(SECOND_IND, colorBuy2nd, colorSell2nd);
#else
backtester.SetIndicatorColors(SECOND_IND, colorSell2nd, colorSell2nd);
#endif
}
if (useExitIndicator)
{
backtester.SetIndicator(EXIT_IND, mainBufferExit, signalBufferExit, crossLevelExit, invertOperativeExit, widthFilterExit, indicatorModeExit);
backtester.SetExitOnlyAtSignal(useExitAtSignal);
#ifdef __MQL5__
backtester.SetIndicatorColors(EXIT_IND, colorBuyExit, colorSellExit);
#else
backtester.SetIndicatorColors(EXIT_IND, colorSellExit, colorSellExit);
#endif
}
if (useContIndicator == CONT_CUSTOM)
{
backtester.SetIndicator(CONTINUATION_IND, mainBufferCont, signalBufferCont, crossLevelCont, invertOperativeCont, widthFilterCont, indicatorModeCont);
#ifdef __MQL5__
backtester.SetIndicatorColors(CONTINUATION_IND, colorBuyCont, colorSellCont);
#else
backtester.SetIndicatorColors(CONTINUATION_IND, colorSellCont, colorSellCont);
#endif
}
else //Hacer despues de configurar exit y main
{
backtester.SetContinuationIndicator(useContIndicator);
}
if (useVolumeIndicator)
{
backtester.SetIndicator(VOLUME_IND, mainBufferVolume, signalBufferVolume, minimumLevelVolume);
if (indicatorModeVolume == OVER_LEVEL_BUY_SELL ||indicatorModeVolume == OVER_SIGNAL_BUY_SELL)
{
backtester.SetVolumeSettings(indicatorModeVolume, false, 0, volColorBuy, volColorSell);
}
else
{
backtester.SetVolumeSettings(indicatorModeVolume, false);
}
if (indicatorModeVolume == BIDIRECTIONAL_LEVEL)
{
backtester.SetVolumeBidirectionalWidth(widthLevelVolume);
}
}
if (useBaseline) backtester.SetIndicator(BASELINE_IND, baselineBuffer);
backtester.DrawIcons(displayIcons);
if (optimizationMode == N_EQUITY_CURVE) backtester.RecordEquityCurve();
if (optimizationMode == N_EQUITY_COMP)
{
backtester.RecordEquityCurve();
backtester.UseCompoundInterest();
}
GetIndicatorHandles();
if (use_advanced_TS)
{
GetTrailingStopHandles();
backtester.SetAdvancedTrailingStops(TS_buy_buffer, TS_sell_buffer);
}
if (useEvz) GetEVZHandle();
if (useExposure) backtester.UseCurrencyExposure();
if (useNews)
{
backtester.UseNewsFiltering(newsEUR, newsGBP, newsAUD, newsNZD, newsUSD, newsCAD, newsCHF, newsJPY);
}
#ifdef __MQL5__
//TODO news indicator in MT4? (unless it was removed for a reason)
if (showNewsIndicator && MQLInfoInteger(MQL_VISUAL_MODE))
{
int newsHandle = iCustom(Symbol(), PERIOD_CURRENT, NEWS_INDICATOR, 0, 1, true, false, false, false, false, newsEUR, newsGBP, newsAUD, newsNZD, newsUSD, newsCAD, newsCHF, newsJPY, newsIconDistance, newsIconBetween);
ChartIndicatorAdd(0, 0, newsHandle);
}
if (optimizationMode == N_REAL_TRADES)
{
backtester.SetRealTradeMode();
}
else
{
#endif
if (writeTradeJournal) backtester.RecordTradeJournal();
if (showExtendedSummary) backtester.UseExtendedSummary();
#ifdef __MQL5__
} //Close else (Real trade mode) in MQL5 only
#endif
return INIT_SUCCEEDED;
}
}
double TesterEvent()
{
if (!isTester) return -1.0;
Print(BIG_SEPARATOR_LINE);
WriteFilesMode fileMode = NO_WRITE;
if (writeToFile)
{
fileMode = MQLInfoInteger(MQL_OPTIMIZATION)? OPTIMIZE : SUMMARY;
}
double finalvalue = backtester.TesterResult(optimizationMode, fileMode, customOptimizationFormula);
Print(BIG_SEPARATOR_LINE);
if (detectTicks)
{
Print(BIG_SEPARATOR_LINE);
Print("WARNING: ");
Print("We have detected that this optimization was performed using multiple ticks per candle.");
Print("This backtester is more efficient when using the Modelling method \"OPEN PRICES ONLY\"");
Print(BIG_SEPARATOR_LINE);
Print(BIG_SEPARATOR_LINE);
}
return finalvalue;
}
void TickEvent()
{
if (isTester)
{
datetime arr[];
CopyTime(_Symbol, PERIOD_CURRENT, 0, 1, arr);
if (lastCandle != arr[0])
{
lastCandle = arr[0];
backtester.BacktesterTick();
//#ifdef __MQL4__ //Show news in MT4
//if (showNewsIndicator && MQLInfoInteger(MQL_VISUAL_MODE))
//{
// double getNews = iCustom(_Symbol, PERIOD_CURRENT, NEWS_INDICATOR, 0, 1, false, false, false, false, newsEUR, newsGBP, newsAUD, newsNZD, newsUSD, newsCAD, newsCHF, newsJPY, newsIconDistance, newsIconBetween, 0, DISPLACEMENT);
//}
//#endif
}
else
{
#ifdef __MQL4__
static int gapCount = 0;
if (gapCount>0) detectTicks = true; //Last candle repeats
gapCount++;
#else
detectTicks = true;
#endif
}
}
}
void ChartEvent_Event(const int id,const long& lparam,const double& dparam,const string& sparam)
{
program.ChartEvent(id, lparam, dparam, sparam);
}
datetime lastCandle = 0;
void DeInitEvent(int reason)
{
if (!isTester)
{
program.Destroy(reason);
delete program;
return;
}
delete backtester;
}
void TimerEvent()
{
//if (!isTester) //TODO
// program.OnTimerEvent();
}
#define SUBSTITUTE_PARAM(type, id) SubstituteOptimizationParameter(type##_param##id, type##_index##id, type##Params);
void GetIndicatorHandles()
{
//Process parameters
#ifdef __MQL5__
MqlParam mainParams[];
MqlParam secondParams[];
MqlParam exitParams[];
MqlParam volumeParams[];
MqlParam baselineParams[];
MqlParam contParams[];
#else
double mainParams[];
double secondParams[];
double exitParams[];
double volumeParams[];
double baselineParams[];
double contParams[];
#endif
CDictionary* opt_dict = new CDictionary();
CreateOptimizationDict(opt_dict);
ProcessParameters(indicatorName, indicatorParams, mainParams, opt_dict);
if (use2Confirm) ProcessParameters(indicatorName2nd, indicatorParams2nd, secondParams, opt_dict);
if (useExitIndicator) ProcessParameters(indicatorNameExit, indicatorParamsExit, exitParams, opt_dict);
if (useVolumeIndicator) ProcessParameters(indicatorNameVolume, indicatorParamsVolume, volumeParams, opt_dict);
if (useBaseline) ProcessParameters(indicatorNameBaseline, indicatorParamsBaseline, baselineParams, opt_dict);
if (useContIndicator == CONT_CUSTOM) ProcessParameters(indicatorNameCont, indicatorParamsCont, contParams, opt_dict);
delete opt_dict;
// Check Native indicators
#ifdef __MQL5__
string mainName, secondName, exitName, volumeName, baselineName, contName;
bool mainNative=false, secondNative=false, exitNative=false, volumeNative=false, baselineNative=false, contNative=false;
if (IS_NATIVE_IND(mainParams[0].string_value))
{
mainName = mainParams[0].string_value;
mainNative = true;
ArrayRemove(mainParams, 0, 1);
}
if (use2Confirm)
{
if (IS_NATIVE_IND(secondParams[0].string_value))
{
secondName = secondParams[0].string_value;
secondNative = true;
ArrayRemove(secondParams, 0, 1);
}
}
if (useExitIndicator)
{
if (IS_NATIVE_IND(exitParams[0].string_value))
{
exitName = exitParams[0].string_value;
exitNative = true;
ArrayRemove(exitParams, 0, 1);
}
}
if (useVolumeIndicator)
{
if (IS_NATIVE_IND(volumeParams[0].string_value))
{
volumeName = volumeParams[0].string_value;
volumeNative = true;
ArrayRemove(volumeParams, 0, 1);
}
}
if (useBaseline)
{
if (IS_NATIVE_IND(baselineParams[0].string_value))
{
baselineName = baselineParams[0].string_value;
baselineNative = true;
ArrayRemove(baselineParams, 0, 1);
}
}
if (useContIndicator == CONT_CUSTOM)
{
if (IS_NATIVE_IND(contParams[0].string_value))
{
contName = contParams[0].string_value;
contNative = true;
ArrayRemove(contParams, 0, 1);
}
}
//Get Handles
for (int i=0; i < totalSymbols; i++)
{
//int atrHandle = iATR(symbolsToTrade[i], PERIOD_CURRENT, 14);
int atrHandle = iCustom(symbolsToTrade[i], PERIOD_CURRENT, INVISIBLE_ATR, atrPeriod);
backtester.SetHandle(atrHandle, ATR_IND, i);
int mainHandle = GetIndicatorWithParameters(symbolsToTrade[i], mainParams, mainNative, mainName);
backtester.SetHandle(mainHandle, MAIN_IND, i);
if (use2Confirm)
{
int secondHandle = GetIndicatorWithParameters(symbolsToTrade[i], secondParams, secondNative, secondName);
backtester.SetHandle(secondHandle, SECOND_IND, i);
}
if (useExitIndicator)
{
int exitHandle = GetIndicatorWithParameters(symbolsToTrade[i], exitParams, exitNative, exitName);
backtester.SetHandle(exitHandle, EXIT_IND, i);
}
if (useVolumeIndicator)
{
int volumeHandle = GetIndicatorWithParameters(symbolsToTrade[i], volumeParams, volumeNative, volumeName);
backtester.SetHandle(volumeHandle, VOLUME_IND, i);
}
if (useBaseline)
{
int baselineHandle = GetIndicatorWithParameters(symbolsToTrade[i], baselineParams, baselineNative, baselineName);
backtester.SetHandle(baselineHandle, BASELINE_IND, i);
}
if (useContIndicator == CONT_CUSTOM)
{
int continuationHandle = GetIndicatorWithParameters(symbolsToTrade[i], contParams, contNative, contName);
backtester.SetHandle(continuationHandle, CONTINUATION_IND, i);
}
}
#else
if (IS_NATIVE_IND(indicatorName))
ProcessNativeParameters(indicatorName, mainParams);
double atrParams[1];
atrParams[0] = atrPeriod;
backtester.SetIndicatorProperties(ATR_IND, INVISIBLE_ATR, atrParams);
backtester.SetIndicatorProperties(MAIN_IND, indicatorName, mainParams);
if (use2Confirm)
{
if (IS_NATIVE_IND(indicatorName2nd))
ProcessNativeParameters(indicatorName2nd, secondParams);
backtester.SetIndicatorProperties(SECOND_IND, indicatorName2nd, secondParams);
}
if (useExitIndicator)
{
if (IS_NATIVE_IND(indicatorNameExit))
ProcessNativeParameters(indicatorNameExit, exitParams);
backtester.SetIndicatorProperties(EXIT_IND, indicatorNameExit, exitParams);
}
if (useVolumeIndicator)
{
if (IS_NATIVE_IND(indicatorNameVolume))
ProcessNativeParameters(indicatorNameVolume, volumeParams);
backtester.SetIndicatorProperties(VOLUME_IND, indicatorNameVolume, volumeParams);
}
if (useBaseline)
{
if (IS_NATIVE_IND(indicatorNameBaseline))
ProcessNativeParameters(indicatorNameBaseline, baselineParams);
backtester.SetIndicatorProperties(BASELINE_IND, indicatorNameBaseline, baselineParams);
}
if (useContIndicator)
{
if (IS_NATIVE_IND(indicatorNameCont))
ProcessNativeParameters(indicatorNameCont, contParams);
backtester.SetIndicatorProperties(CONTINUATION_IND, indicatorNameCont, contParams);
}
#endif
}
void GetTrailingStopHandles()
{
//Process parameters
#ifdef __MQL5__
MqlParam trailingParams[];
#else
double trailingParams[];
#endif
ProcessParameters(indicatorNameTrailing, indicatorParamsTrailing, trailingParams);
// Check Native indicators
#ifdef __MQL5__
string trailName;
bool trailNative=false;
if (IS_NATIVE_IND(trailingParams[0].string_value))
{
trailName = trailingParams[0].string_value;
trailNative = true;
ArrayRemove(trailingParams, 0, 1);
}
//Get Handles
for (int i=0; i < totalSymbols; i++)
{
int trailHandle = GetIndicatorWithParameters(symbolsToTrade[i], trailingParams, trailNative, trailName);
backtester.SetTrailingHandle(trailHandle, i);
}
#else
if (IS_NATIVE_IND(indicatorNameTrailing))
ProcessNativeParameters(indicatorNameTrailing, trailingParams);
backtester.SetTrailingIndicatorProperties(indicatorNameTrailing, trailingParams);
#endif
}
void GetEVZHandle()
{
#ifdef __MQL5__
MqlParam evzParams[];
ProcessParameters(EVZ_INDICATOR, evzParams);
int evzHandle = GetIndicatorWithParameters(Symbol(), evzParams);
#else
int evzHandle = 0; // Not used in MT4
#endif
backtester.SetEVZHandle(evzHandle, minimumEvz, halfRiskEvz, scaleOutHalfRisk);
}
bool CheckExpertParameters()
{
if (stopLossAtr<=0.0 || takeProfitAtr<=0.0)
{
Print("Stop Loss and Take Profit ATR need to be greater than 0.0");
return false;
}
if (minimumEvz > halfRiskEvz)
{
Print("Minimum EVZ cannot be greater than Half Risk EVZ");
return false;
}
return true;
}
#define SET_OPT_DICT(n) dict.Set<double>("#" +IntegerToString(n), opt_param_##n); //if (opt_param_##n!=0.0) { }
void CreateOptimizationDict(CDictionary* dict)
{
SET_OPT_DICT(1) SET_OPT_DICT(2) SET_OPT_DICT(3) SET_OPT_DICT(4) SET_OPT_DICT(5)
SET_OPT_DICT(6) SET_OPT_DICT(7) SET_OPT_DICT(8) SET_OPT_DICT(9) SET_OPT_DICT(10)
SET_OPT_DICT(11) SET_OPT_DICT(12) SET_OPT_DICT(13) SET_OPT_DICT(14) SET_OPT_DICT(15)
SET_OPT_DICT(16) SET_OPT_DICT(17) SET_OPT_DICT(18) SET_OPT_DICT(19) SET_OPT_DICT(20)
SET_OPT_DICT(21) SET_OPT_DICT(22) SET_OPT_DICT(23) SET_OPT_DICT(24) SET_OPT_DICT(25)
SET_OPT_DICT(26) SET_OPT_DICT(27) SET_OPT_DICT(28) SET_OPT_DICT(29) SET_OPT_DICT(30)
SET_OPT_DICT(31) SET_OPT_DICT(32) SET_OPT_DICT(33) SET_OPT_DICT(34) SET_OPT_DICT(35)
SET_OPT_DICT(36) SET_OPT_DICT(37) SET_OPT_DICT(38) SET_OPT_DICT(39) SET_OPT_DICT(40)
SET_OPT_DICT(41) SET_OPT_DICT(42) SET_OPT_DICT(43) SET_OPT_DICT(44) SET_OPT_DICT(45)
SET_OPT_DICT(46) SET_OPT_DICT(47) SET_OPT_DICT(48) SET_OPT_DICT(49) SET_OPT_DICT(50)
}