mql5/Experts/Motivated/SystemEngine.mqh

203 lines
6.4 KiB
MQL5

2026-06-09 05:44:15 +02:00
//+------------------------------------------------------------------+
//| System.mqh |
//| Copyright 2025, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#include "Chapters/Chapter2.mqh"
2026-06-23 21:44:37 +02:00
#include "Chapters/Chapter3.mqh"
2026-06-09 05:44:15 +02:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
2026-06-10 18:31:08 +02:00
class System
2026-06-09 05:44:15 +02:00
{
private:
string symbol;
ENUM_TIMEFRAMES timeframe;
//persistance launch
int candles;
bool initialized;
datetime lastProcessedTime;
CommonHelper com;
2026-06-10 18:31:08 +02:00
2026-06-09 05:44:15 +02:00
Chapter2 chapter2;
2026-06-23 21:44:37 +02:00
Chapter3 chapter3;
StructureLevels* levels;
2026-06-09 05:44:15 +02:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OnInitChapter(AbstractChapter &chapter, string prefix, int lr = 3, int look = 1)
{
2026-06-23 21:44:37 +02:00
return chapter.OnInit(levels, prefix, timeframe, com);
2026-06-09 05:44:15 +02:00
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnCalculateChapter(AbstractChapter &chapter, int i)
{
chapter.OnCalculate(i, com);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool InitChapters()
{
if(OnInitChapter(chapter2, "chapter2") != INIT_SUCCEEDED)
{
2026-06-23 21:44:37 +02:00
Print("SmcSystem: failed to init Chapter2");
2026-06-09 05:44:15 +02:00
return false;
}
chapter2.SetEventBus(&bus);
2026-06-23 21:44:37 +02:00
if(OnInitChapter(chapter3, "chapter3") != INIT_SUCCEEDED)
{
Print("SmcSystem: failed to init Chapter3");
return false;
}
chapter3.SetEventBus(&bus);
2026-06-09 05:44:15 +02:00
return true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void RunAllChapters(int i)
{
OnCalculateChapter(chapter2, i);
2026-06-23 21:44:37 +02:00
OnCalculateChapter(chapter3, i);
2026-06-09 05:44:15 +02:00
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int ReprocessStartIndex()
{
int bars = com.Bars();
if(bars <= 0)
return 0;
if(lastProcessedTime == 0)
return 0;
int lastIndex = com.TimeIndex(lastProcessedTime);
if(!com.IsValidIndex(lastIndex))
return 0;
int start = lastIndex - candles ;
if(start < 0)
start = 0;
if(start >= bars)
start = bars - 1;
return start;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void launch()
{
for(int i = 0; i < com.Bars(); i++)
RunAllChapters(i);
if(com.Bars() > 0)
lastProcessedTime = com.TimeAt(com.Bars() - 1);
initialized = true;
2026-06-23 21:44:37 +02:00
createVert("j", com.TimeAt(0));
2026-06-09 05:44:15 +02:00
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
public:
System();
~System();
virtual int OnInit(string s, ENUM_TIMEFRAMES tf, int lr = 3, int look = 1);
virtual void OnCalculate();
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
2026-06-10 18:31:08 +02:00
2026-06-09 05:44:15 +02:00
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
System::System()
{
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
System::~System()
{
2026-06-23 21:44:37 +02:00
delete levels;
2026-06-09 05:44:15 +02:00
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int System::OnInit(string s, ENUM_TIMEFRAMES tf, int lr = 3, int look = 1)
{
symbol = s;
timeframe = tf;
initialized = false;
2026-06-10 18:31:08 +02:00
candles = lr;
2026-06-23 21:44:37 +02:00
levels = new StructureLevels();
2026-06-09 05:44:15 +02:00
if(com.OnInit(s, tf, lr, look) == -1)
{
return INIT_FAILED;
}
if(!InitChapters())
return INIT_FAILED;
return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void System::OnCalculate()
{
if(!initialized)
{
com.UpdateBars(timeframe, symbol, true, 1);
launch();
2026-06-10 18:31:08 +02:00
2026-06-09 05:44:15 +02:00
}
else
{
if(!com.OnNewBar())
return;
com.UpdateBars(timeframe, symbol);
int start = ReprocessStartIndex();
int end = com.Bars() - 1;
for(int i = start; i <= end; i++)
RunAllChapters(i);
if(end >= 0)
lastProcessedTime = com.TimeAt(end);
}
// Print("[System] : OnCalculate");
}
//+------------------------------------------------------------------+