Article-17457-MQL5-Optimiza.../Article-17457-MQL5-Optimization-Results-Visualizer.mq5

238 lines
10 KiB
MQL5
Raw Permalink Normal View History

2026-03-24 15:30:54 +07:00
//+------------------------------------------------------------------+
2026-03-24 15:14:39 +07:00
//| Article-17457-MQL5-Optimization-Results-Visualizer.mq5 |
//| Copyright 2026, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
2026-03-24 15:30:54 +07:00
//| Include |
//+------------------------------------------------------------------+
#include <Expert\Expert.mqh>
#include <Expert\Signal\SignalMA.mqh>
#include <Expert\Trailing\TrailingMA.mqh>
#include <Expert\Money\MoneyNone.mqh>
//+------------------------------------------------------------------+
//| Inputs |
//+------------------------------------------------------------------+
//--- inputs for expert
input string InpExpertTitle = "ExpertMAMA";
int Expert_MagicNumber = 12003;
bool Expert_EveryTick = false;
//--- inputs for signal
input int InpSignalMAPeriod = 12;
input int InpSignalMAShift = 6;
input ENUM_MA_METHOD InpSignalMAMethod = MODE_SMA;
input ENUM_APPLIED_PRICE InpSignalMAApplied = PRICE_CLOSE;
//--- inputs for trailing
input int InpTrailingMAPeriod = 12;
input int InpTrailingMAShift = 0;
input ENUM_MA_METHOD InpTrailingMAMethod = MODE_SMA;
input ENUM_APPLIED_PRICE InpTrailingMAApplied= PRICE_CLOSE;
//+------------------------------------------------------------------+
//| Global expert object |
//+------------------------------------------------------------------+
CExpert ExtExpert;
2026-03-24 15:14:39 +07:00
//+------------------------------------------------------------------+
2026-03-24 15:30:54 +07:00
//| Initialization function of the expert |
//+------------------------------------------------------------------+
int OnInit(void)
2026-03-24 15:14:39 +07:00
{
2026-03-24 15:30:54 +07:00
//--- Initializing expert
if(!ExtExpert.Init(Symbol(),Period(),Expert_EveryTick,Expert_MagicNumber))
{
//--- failed
printf(__FUNCTION__+": error initializing expert");
ExtExpert.Deinit();
return(-1);
}
//--- Creation of signal object
CSignalMA *signal=new CSignalMA;
if(signal==NULL)
{
//--- failed
printf(__FUNCTION__+": error creating signal");
ExtExpert.Deinit();
return(-2);
}
//--- Add signal to expert (will be deleted automatically))
if(!ExtExpert.InitSignal(signal))
{
//--- failed
printf(__FUNCTION__+": error initializing signal");
ExtExpert.Deinit();
return(-3);
}
//--- Set signal parameters
signal.PeriodMA(InpSignalMAPeriod);
signal.Shift(InpSignalMAShift);
signal.Method(InpSignalMAMethod);
signal.Applied(InpSignalMAApplied);
//--- Check signal parameters
if(!signal.ValidationSettings())
{
//--- failed
printf(__FUNCTION__+": error signal parameters");
ExtExpert.Deinit();
return(-4);
}
//--- Creation of trailing object
CTrailingMA *trailing=new CTrailingMA;
if(trailing==NULL)
{
//--- failed
printf(__FUNCTION__+": error creating trailing");
ExtExpert.Deinit();
return(-5);
}
//--- Add trailing to expert (will be deleted automatically))
if(!ExtExpert.InitTrailing(trailing))
{
//--- failed
printf(__FUNCTION__+": error initializing trailing");
ExtExpert.Deinit();
return(-6);
}
//--- Set trailing parameters
trailing.Period(InpTrailingMAPeriod);
trailing.Shift(InpTrailingMAShift);
trailing.Method(InpTrailingMAMethod);
trailing.Applied(InpTrailingMAApplied);
//--- Check trailing parameters
if(!trailing.ValidationSettings())
{
//--- failed
printf(__FUNCTION__+": error trailing parameters");
ExtExpert.Deinit();
return(-7);
}
//--- Creation of money object
CMoneyNone *money=new CMoneyNone;
if(money==NULL)
{
//--- failed
printf(__FUNCTION__+": error creating money");
ExtExpert.Deinit();
return(-8);
}
//--- Add money to expert (will be deleted automatically))
if(!ExtExpert.InitMoney(money))
{
//--- failed
printf(__FUNCTION__+": error initializing money");
ExtExpert.Deinit();
return(-9);
}
//--- Set money parameters
//--- Check money parameters
if(!money.ValidationSettings())
{
//--- failed
printf(__FUNCTION__+": error money parameters");
ExtExpert.Deinit();
return(-10);
}
//--- Tuning of all necessary indicators
if(!ExtExpert.InitIndicators())
{
//--- failed
printf(__FUNCTION__+": error initializing indicators");
ExtExpert.Deinit();
return(-11);
}
//--- succeed
2026-03-24 15:14:39 +07:00
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
2026-03-24 15:30:54 +07:00
//| Deinitialization function of the expert |
2026-03-24 15:14:39 +07:00
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
2026-03-24 15:30:54 +07:00
ExtExpert.Deinit();
2026-03-24 15:14:39 +07:00
}
//+------------------------------------------------------------------+
2026-03-24 15:30:54 +07:00
//| Function-event handler "tick" |
2026-03-24 15:14:39 +07:00
//+------------------------------------------------------------------+
2026-03-24 15:30:54 +07:00
void OnTick(void)
2026-03-24 15:14:39 +07:00
{
2026-03-24 15:30:54 +07:00
ExtExpert.OnTick();
2026-03-24 15:14:39 +07:00
}
//+------------------------------------------------------------------+
2026-03-24 15:30:54 +07:00
//| Function-event handler "trade" |
2026-03-24 15:14:39 +07:00
//+------------------------------------------------------------------+
2026-03-24 15:30:54 +07:00
void OnTrade(void)
2026-03-24 15:14:39 +07:00
{
2026-03-24 15:30:54 +07:00
ExtExpert.OnTrade();
2026-03-24 15:14:39 +07:00
}
//+------------------------------------------------------------------+
2026-03-24 15:30:54 +07:00
//| Function-event handler "timer" |
2026-03-24 15:14:39 +07:00
//+------------------------------------------------------------------+
2026-03-24 15:30:54 +07:00
void OnTimer(void)
2026-03-24 15:14:39 +07:00
{
2026-03-24 15:30:54 +07:00
ExtExpert.OnTimer();
}
//+------------------------------------------------------------------+
//| Код, необходимый для визуализации оптимизации |
//+------------------------------------------------------------------+
//--- При отладке, если во время оптимизации нажать "Стоп", то следующий запуск оптимизации продолжит незавершённые проходы с места остановки
//--- Чтобы каждый новый запуск оптимизации начинался заново, определим директиву препроцессора
#property tester_no_cache
//--- Определим макроподстановки
#define REPLAY_DELAY_MS 100 // Задержка воспроизведения оптимизации в миллисекундах
#define STAT_LINES 1 // Количество отображаемых линий статистики оптимизации
#define SELECTED_LINE_WD 3 // Толщина линии выбранного прохода оптимизации
#define SELECTED_LINE_CLR clrDodgerBlue // Цвет линии выбранного прохода оптимизации
//--- Подключим код для работы с результатами оптимизации просмотровщиком фреймов
#include "FrameViewer.mqh"
//--- Объявим объект просмотровщика фреймов
CFrameViewer fw;
//+------------------------------------------------------------------+
//| Tester function |
//+------------------------------------------------------------------+
double OnTester()
{
//--- тут нужно вставить свою функцию для вычисления критерия оптимизации
double TesterCritetia=MathAbs(TesterStatistics(STAT_SHARPE_RATIO)*TesterStatistics(STAT_PROFIT));
TesterCritetia=TesterStatistics(STAT_PROFIT)>0?TesterCritetia:(-TesterCritetia);
//--- вызываем на каждом окончании тестирования и передаем в качестве параметра критерий оптимизации
fw.OnTester(TesterCritetia);
2026-03-24 15:14:39 +07:00
//---
2026-03-24 15:30:54 +07:00
return(TesterCritetia);
2026-03-24 15:14:39 +07:00
}
//+------------------------------------------------------------------+
2026-03-24 15:30:54 +07:00
//| TesterInit function |
2026-03-24 15:14:39 +07:00
//+------------------------------------------------------------------+
2026-03-24 15:30:54 +07:00
void OnTesterInit()
2026-03-24 15:14:39 +07:00
{
2026-03-24 15:30:54 +07:00
//--- подготавливаем график для отображения линий баланса
//--- STAT_LINES задает количество линий баланса на графике,
//--- SELECTED_LINE_WD - толщину, SELECTED_LINE_CLR - цвет линии выбранного прохода
fw.OnTesterInit(STAT_LINES, SELECTED_LINE_WD, SELECTED_LINE_CLR);
2026-03-24 15:14:39 +07:00
}
//+------------------------------------------------------------------+
2026-03-24 15:30:54 +07:00
//| TesterDeinit function |
2026-03-24 15:14:39 +07:00
//+------------------------------------------------------------------+
2026-03-24 15:30:54 +07:00
void OnTesterDeinit()
2026-03-24 15:14:39 +07:00
{
2026-03-24 15:30:54 +07:00
//--- завершение оптимизации
fw.OnTesterDeinit();
}
//+------------------------------------------------------------------+
//| TesterPass function |
//+------------------------------------------------------------------+
void OnTesterPass()
{
//--- обрабатываем полученные результаты тестирования и выводим графику
fw.OnTesterPass();
}
//+------------------------------------------------------------------+
//| Обработка событий на графике |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)
{
//--- запускает воспроизведение фреймов по окончании оптимизации при нажатии на шапке
fw.OnChartEvent(id,lparam,dparam,sparam,REPLAY_DELAY_MS); // REPLAY_DELAY_MS - пауза в ms между кадрами воспроизведения
2026-03-24 15:14:39 +07:00
}
//+------------------------------------------------------------------+