From 62228cca17bf3228ddd05979742bb560d86deeb3 Mon Sep 17 00:00:00 2001 From: Toh4iem9 Date: Mon, 13 Jul 2026 07:39:05 +0200 Subject: [PATCH] refactor: Added static EnsureHTFDataReady and automated OnTimerUpdate helper for MTF suites --- Include/MyIncludes/DataSync_Tools.mqh | 43 ++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/Include/MyIncludes/DataSync_Tools.mqh b/Include/MyIncludes/DataSync_Tools.mqh index faec093..c31b68c 100644 --- a/Include/MyIncludes/DataSync_Tools.mqh +++ b/Include/MyIncludes/DataSync_Tools.mqh @@ -1,18 +1,20 @@ //+------------------------------------------------------------------+ //| DataSync_Tools.mqh | -//| Helper for synchronizing Multi-Symbol Multi-TF data. | -//| VERSION 1.01: Fixed type conversion warnings (uint). | -//| Copyright 2026, xxxxxxxx | +//| Copyright 2026, xxxxxxxx| //+------------------------------------------------------------------+ #property copyright "Copyright 2026, xxxxxxxx" +#property version "1.10" // Added static EnsureHTFDataReady and automated OnTimerUpdate helper for MTF suites + +#ifndef DATA_SYNC_TOOLS_MQH +#define DATA_SYNC_TOOLS_MQH //+------------------------------------------------------------------+ -//| | +//| Class CDataSync | //+------------------------------------------------------------------+ class CDataSync { public: - //--- Ensures that data for Symbol/TF is loaded and up-to-date + //--- Ensures that data for Symbol/TF is loaded and up-to-date (Legacy Support) //--- Returns true if success, false if timeout static bool EnsureDataReady(string symbol, ENUM_TIMEFRAMES tf, int bars_needed = 2, uint timeout_ms = 3000) { @@ -24,10 +26,10 @@ public: datetime time_last = (datetime)SeriesInfoInteger(symbol, tf, SERIES_LASTBAR_DATE); // 3. Retry loop - uint start_tick = GetTickCount(); // Fixed type: uint + uint start_tick = GetTickCount(); int available_bars = 0; - while(GetTickCount() - start_tick < timeout_ms) // Comparison is now uint vs uint + while(GetTickCount() - start_tick < timeout_ms) { available_bars = Bars(symbol, tf); @@ -46,5 +48,32 @@ public: Print(StringFormat("DataSync Timeout: %s on %s. Bars: %d", symbol, EnumToString(tf), available_bars)); return false; } + + //--- NEW: Stateless High-Performance HTF Data Ready Checker (Used by MTF indicators) + static bool EnsureHTFDataReady(const string symbol, const ENUM_TIMEFRAMES timeframe, const int required_bars) + { + ResetLastError(); + if(!SymbolInfoInteger(symbol, SYMBOL_SELECT)) + { + SymbolSelect(symbol, true); + } + datetime times[]; + int copied = CopyTime(symbol, timeframe, 0, required_bars, times); + return (copied >= required_bars); + } + + //--- NEW: Automated MTF Timer-driven Synchronisation & Redraw Daemon + static void OnTimerUpdate(const string symbol, const ENUM_TIMEFRAMES timeframe, const int required_bars, bool &data_synced) + { + if(!data_synced) + { + if(EnsureHTFDataReady(symbol, timeframe, required_bars)) + { + data_synced = true; + ChartRedraw(); // Force MetaTrader 5 to execute OnCalculate immediately + } + } + } }; +#endif // DATA_SYNC_TOOLS_MQH //+------------------------------------------------------------------+