203 lines
6.4 KiB
MQL5
203 lines
6.4 KiB
MQL5
//+------------------------------------------------------------------+
| |
//| 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"
| |
#include "Chapters/Chapter3.mqh"
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
class System
| |
{
| |
private:
| |
string symbol;
| |
ENUM_TIMEFRAMES timeframe;
| |
| |
//persistance launch
| |
int candles;
| |
bool initialized;
| |
| |
datetime lastProcessedTime;
| |
CommonHelper com;
| |
| |
| |
Chapter2 chapter2;
| |
Chapter3 chapter3;
| |
StructureLevels* levels;
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
int OnInitChapter(AbstractChapter &chapter, string prefix, int lr = 3, int look = 1)
| |
{
| |
return chapter.OnInit(levels, prefix, timeframe, com);
| |
}
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
| |
void OnCalculateChapter(AbstractChapter &chapter, int i)
| |
{
| |
chapter.OnCalculate(i, com);
| |
}
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
| |
bool InitChapters()
| |
{
| |
if(OnInitChapter(chapter2, "chapter2") != INIT_SUCCEEDED)
| |
{
| |
Print("SmcSystem: failed to init Chapter2");
| |
return false;
| |
}
| |
chapter2.SetEventBus(&bus);
| |
| |
if(OnInitChapter(chapter3, "chapter3") != INIT_SUCCEEDED)
| |
{
| |
Print("SmcSystem: failed to init Chapter3");
| |
return false;
| |
}
| |
chapter3.SetEventBus(&bus);
| |
| |
return true;
| |
}
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
| |
void RunAllChapters(int i)
| |
{
| |
OnCalculateChapter(chapter2, i);
| |
OnCalculateChapter(chapter3, i);
| |
}
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
| |
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;
| |
| |
createVert("j", com.TimeAt(0));
| |
}
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
| |
| |
public:
| |
System();
| |
~System();
| |
| |
virtual int OnInit(string s, ENUM_TIMEFRAMES tf, int lr = 3, int look = 1);
| |
virtual void OnCalculate();
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
| |
| |
};
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
System::System()
| |
{
| |
| |
}
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
System::~System()
| |
{
| |
delete levels;
| |
}
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
int System::OnInit(string s, ENUM_TIMEFRAMES tf, int lr = 3, int look = 1)
| |
{
| |
symbol = s;
| |
timeframe = tf;
| |
initialized = false;
| |
candles = lr;
| |
levels = new StructureLevels();
| |
| |
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();
| |
| |
}
| |
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");
| |
}
| |
//+------------------------------------------------------------------+
|