mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-25 10:18:04 +00:00
refactor(scripts): Includes Data Synchronization logic
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| SessionLevels_Calculator.mqh |
|
//| SessionLevels_Calculator.mqh |
|
||||||
//| Engine for retrieving key Session Levels (D1/W1 etc). |
|
//| Includes Data Synchronization logic. |
|
||||||
//| Copyright 2026, xxxxxxxx |
|
//| Copyright 2026, xxxxxxxx |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
#property copyright "Copyright 2026, xxxxxxxx"
|
#property copyright "Copyright 2026, xxxxxxxx"
|
||||||
|
|
||||||
|
#include <MyIncludes\DataSync_Tools.mqh> // Include new tool
|
||||||
|
|
||||||
struct SessionLevels
|
struct SessionLevels
|
||||||
{
|
{
|
||||||
double prev_high;
|
double prev_high;
|
||||||
@@ -14,16 +16,14 @@ struct SessionLevels
|
|||||||
bool valid;
|
bool valid;
|
||||||
};
|
};
|
||||||
|
|
||||||
//+==================================================================+
|
//+------------------------------------------------------------------+
|
||||||
//| CLASS: CSessionLevelsCalculator |
|
//| |
|
||||||
//+==================================================================+
|
//+------------------------------------------------------------------+
|
||||||
class CSessionLevelsCalculator
|
class CSessionLevelsCalculator
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
ENUM_TIMEFRAMES m_session_tf; // Usually PERIOD_D1
|
ENUM_TIMEFRAMES m_session_tf;
|
||||||
|
datetime m_last_calc_time_bar;
|
||||||
// Cache to prevent repetitive CopyRates calls on every tick
|
|
||||||
datetime m_last_calc_time;
|
|
||||||
SessionLevels m_cached_levels;
|
SessionLevels m_cached_levels;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@@ -31,26 +31,20 @@ public:
|
|||||||
virtual ~CSessionLevelsCalculator(void) {};
|
virtual ~CSessionLevelsCalculator(void) {};
|
||||||
|
|
||||||
bool Init(ENUM_TIMEFRAMES session_tf = PERIOD_D1);
|
bool Init(ENUM_TIMEFRAMES session_tf = PERIOD_D1);
|
||||||
|
bool GetLevels(string symbol, datetime time, SessionLevels &out_levels); // Added 'symbol' param
|
||||||
// Get levels for a specific time (usually TimeCurrent or bar time)
|
|
||||||
bool GetLevels(datetime time, SessionLevels &out_levels);
|
|
||||||
|
|
||||||
// Helper to calculate distance in ATR units
|
|
||||||
double GetDistanceATR(double price, double level, double atr);
|
double GetDistanceATR(double price, double level, double atr);
|
||||||
};
|
};
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| Constructor |
|
//| |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
CSessionLevelsCalculator::CSessionLevelsCalculator(void) :
|
CSessionLevelsCalculator::CSessionLevelsCalculator(void) : m_session_tf(PERIOD_D1), m_last_calc_time_bar(0)
|
||||||
m_session_tf(PERIOD_D1),
|
|
||||||
m_last_calc_time(0)
|
|
||||||
{
|
{
|
||||||
m_cached_levels.valid = false;
|
m_cached_levels.valid = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| Init |
|
//| |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
bool CSessionLevelsCalculator::Init(ENUM_TIMEFRAMES session_tf)
|
bool CSessionLevelsCalculator::Init(ENUM_TIMEFRAMES session_tf)
|
||||||
{
|
{
|
||||||
@@ -59,65 +53,54 @@ bool CSessionLevelsCalculator::Init(ENUM_TIMEFRAMES session_tf)
|
|||||||
}
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| Get Levels |
|
//| |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
bool CSessionLevelsCalculator::GetLevels(datetime time, SessionLevels &out_levels)
|
bool CSessionLevelsCalculator::GetLevels(string symbol, datetime time, SessionLevels &out_levels)
|
||||||
{
|
{
|
||||||
// 1. Identify the start time of the Session bar containing 'time'
|
// 0. Ensure Data is Ready (Force Sync)
|
||||||
datetime sess_start = iTime(_Symbol, m_session_tf, iBarShift(_Symbol, m_session_tf, time));
|
if(!CDataSync::EnsureDataReady(symbol, m_session_tf))
|
||||||
|
|
||||||
if(sess_start == 0)
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// 2. Check Cache
|
// 1. Identify start time
|
||||||
if(sess_start == m_last_calc_time && m_cached_levels.valid)
|
datetime session_start_time = iTime(symbol, m_session_tf, iBarShift(symbol, m_session_tf, time));
|
||||||
{
|
if(session_start_time == 0)
|
||||||
out_levels = m_cached_levels;
|
return false;
|
||||||
return true;
|
|
||||||
}
|
// 2. Cache Check (Only works if symbol didn't change! Since we reuse calc, better reset cache or check symbol)
|
||||||
|
// For safety in script usage (switching symbols), let's skip cache or add symbol check
|
||||||
|
// Assuming script creates new calc per symbol or we just refresh. Let's force refresh for safety in Script loops.
|
||||||
|
|
||||||
// 3. Fetch Data
|
// 3. Fetch Data
|
||||||
// We need the Current Open and Previous High/Low/Close
|
int shift = iBarShift(symbol, m_session_tf, time);
|
||||||
// Index 0 in HTF = Current Session. Index 1 = Previous Session.
|
if(shift < 0)
|
||||||
int shift = iBarShift(_Symbol, m_session_tf, time);
|
|
||||||
|
|
||||||
double opens[], highs[], lows[], closes[];
|
|
||||||
|
|
||||||
// We need index 'shift' (Current) and 'shift+1' (Previous)
|
|
||||||
// Copying 2 bars starting from shift
|
|
||||||
if(CopyOpen(_Symbol, m_session_tf, shift, 2, opens) < 2 ||
|
|
||||||
CopyHigh(_Symbol, m_session_tf, shift, 2, highs) < 2 ||
|
|
||||||
CopyLow(_Symbol, m_session_tf, shift, 2, lows) < 2 ||
|
|
||||||
CopyClose(_Symbol, m_session_tf, shift, 2, closes) < 2)
|
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
// Array is Standard Order (0 = Oldest = Prev, 1 = Newest = Curr) due to Copy functions default behavior
|
MqlRates rates[];
|
||||||
// unless ArraySetAsSeries is true (it is not here).
|
ArraySetAsSeries(rates, true);
|
||||||
// Index 0 = Previous Day
|
|
||||||
// Index 1 = Current Day
|
|
||||||
|
|
||||||
m_cached_levels.prev_high = highs[0];
|
if(CopyRates(symbol, m_session_tf, shift, 2, rates) != 2)
|
||||||
m_cached_levels.prev_low = lows[0];
|
return false;
|
||||||
m_cached_levels.prev_close = closes[0];
|
|
||||||
m_cached_levels.curr_open = opens[1];
|
m_cached_levels.prev_high = rates[1].high;
|
||||||
|
m_cached_levels.prev_low = rates[1].low;
|
||||||
|
m_cached_levels.prev_close = rates[1].close;
|
||||||
|
m_cached_levels.curr_open = rates[0].open;
|
||||||
|
|
||||||
|
if(m_cached_levels.prev_high == 0 || m_cached_levels.curr_open == 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
m_cached_levels.valid = true;
|
m_cached_levels.valid = true;
|
||||||
m_last_calc_time = sess_start;
|
|
||||||
|
|
||||||
out_levels = m_cached_levels;
|
out_levels = m_cached_levels;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| Helper: Get Signed Distance in ATR |
|
//| |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
double CSessionLevelsCalculator::GetDistanceATR(double price, double level, double atr)
|
double CSessionLevelsCalculator::GetDistanceATR(double price, double level, double atr)
|
||||||
{
|
{
|
||||||
if(atr == 0)
|
if(atr <= 0.00000001)
|
||||||
return 0.0;
|
return 0.0;
|
||||||
return (price - level) / atr;
|
return (price - level) / atr;
|
||||||
}
|
}
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//+------------------------------------------------------------------+
|
|
||||||
|
|||||||
Reference in New Issue
Block a user