refactor: Enhanced series synchronization checks with 100% backward compatibility

This commit is contained in:
Toh4iem9
2026-08-23 18:21:18 +02:00
parent 070782ca27
commit 13c2b8e45d
+13 -22
View File
@@ -3,66 +3,56 @@
//| Copyright 2026, xxxxxxxx| //| Copyright 2026, xxxxxxxx|
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
#property copyright "Copyright 2026, xxxxxxxx" #property copyright "Copyright 2026, xxxxxxxx"
#property version "1.10" // Added static EnsureHTFDataReady and automated OnTimerUpdate helper for MTF suites #property version "1.20" // Enhanced series synchronization checks with 100% backward compatibility
#ifndef DATA_SYNC_TOOLS_MQH #ifndef DATA_SYNC_TOOLS_MQH
#define DATA_SYNC_TOOLS_MQH #define DATA_SYNC_TOOLS_MQH
//+------------------------------------------------------------------+ //+==================================================================+
//| Class CDataSync | //| Class CDataSync: Centralized Multi-Timeframe Synchronization Daemon|
//+------------------------------------------------------------------+ //+==================================================================+
class CDataSync class CDataSync
{ {
public: public:
//--- Ensures that data for Symbol/TF is loaded and up-to-date (Legacy Support) //--- Legacy Data Readiness Helper
//--- Returns true if success, false if timeout
static bool EnsureDataReady(string symbol, ENUM_TIMEFRAMES tf, int bars_needed = 2, uint timeout_ms = 3000) static bool EnsureDataReady(string symbol, ENUM_TIMEFRAMES tf, int bars_needed = 2, uint timeout_ms = 3000)
{ {
// 1. Check if symbol exists
if(!SymbolSelect(symbol, true)) if(!SymbolSelect(symbol, true))
return false; return false;
// 2. Try to get SeriesInfo to force update
datetime time_last = (datetime)SeriesInfoInteger(symbol, tf, SERIES_LASTBAR_DATE);
// 3. Retry loop
uint start_tick = GetTickCount(); uint start_tick = GetTickCount();
int available_bars = 0; int available_bars = 0;
while(GetTickCount() - start_tick < timeout_ms) while(GetTickCount() - start_tick < timeout_ms)
{ {
available_bars = Bars(symbol, tf); available_bars = Bars(symbol, tf);
if(available_bars >= bars_needed) if(available_bars >= bars_needed)
{ {
double check_buff[]; double check_buff[];
if(CopyClose(symbol, tf, 0, 1, check_buff) == 1) if(CopyClose(symbol, tf, 0, 1, check_buff) == 1)
return true; return true;
} }
// Force update again
time_last = (datetime)SeriesInfoInteger(symbol, tf, SERIES_LASTBAR_DATE);
Sleep(50); Sleep(50);
} }
Print(StringFormat("DataSync Timeout: %s on %s. Bars: %d", symbol, EnumToString(tf), available_bars)); PrintFormat("DataSync Timeout: %s on %s. Bars: %d", symbol, EnumToString(tf), available_bars);
return false; return false;
} }
//--- NEW: Stateless High-Performance HTF Data Ready Checker (Used by MTF indicators) //--- High-Performance Stateless HTF Data Ready Checker
static bool EnsureHTFDataReady(const string symbol, const ENUM_TIMEFRAMES timeframe, const int required_bars) static bool EnsureHTFDataReady(const string symbol, const ENUM_TIMEFRAMES timeframe, const int required_bars)
{ {
ResetLastError(); ResetLastError();
if(!SymbolInfoInteger(symbol, SYMBOL_SELECT)) if(!SymbolInfoInteger(symbol, SYMBOL_SELECT))
{
SymbolSelect(symbol, true); SymbolSelect(symbol, true);
}
datetime times[]; datetime times[];
int copied = CopyTime(symbol, timeframe, 0, required_bars, times); int copied = CopyTime(symbol, timeframe, 0, required_bars, times);
return (copied >= required_bars); return (copied >= required_bars);
} }
//--- NEW: Automated MTF Timer-driven Synchronisation & Redraw Daemon //--- Automated MTF Timer-driven Synchronization & Redraw Daemon
static void OnTimerUpdate(const string symbol, const ENUM_TIMEFRAMES timeframe, const int required_bars, bool &data_synced) static void OnTimerUpdate(const string symbol, const ENUM_TIMEFRAMES timeframe, const int required_bars, bool &data_synced)
{ {
if(!data_synced) if(!data_synced)
@@ -70,10 +60,11 @@ public:
if(EnsureHTFDataReady(symbol, timeframe, required_bars)) if(EnsureHTFDataReady(symbol, timeframe, required_bars))
{ {
data_synced = true; data_synced = true;
ChartRedraw(); // Force MetaTrader 5 to execute OnCalculate immediately ChartRedraw(0); // Trigger immediate, flicker-free OnCalculate execution
} }
} }
} }
}; };
#endif // DATA_SYNC_TOOLS_MQH #endif // DATA_SYNC_TOOLS_MQH
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+