CLatencyMonitor - Inter-Tick latency tracking with ATR swing gate and GlobalVariable IPC for MQL5 EA


Latency Monitor is a lightweight MQL5 include library (#include) developed by DeeFX Precision Labs as part of the Lag Shield Professional product suite. It provides three tightly integrated functions in a single reusable class:
Drag and drop the file into MQL5/Include/DeeFX/ and add a single #include to any EA or service that requires latency awareness.
Execution-sensitive EAs may be at risk of silent fill degradation or calculation errors when the terminal's quotes lag behind the market. Standard MQL5 does not provide built-in mechanisms to:
Latency Monitor solves all three gaps with a dependency-free class.
Inter-tick delta <br/>Each time OnTick() is called, this class counts the number of milliseconds that have elapsed since the previous tick. The first tick is only used to seed the timer - no false alarms will be raised when started.
ATR Volatility Gate <br/>Before declaring a lag, this class checks whether the current ATR value exceeds AtrSmaMultiplier × SMA(ATR, AtrPeriod). SMA is calculated manually from the raw ATR buffer, making the thresholds instrument-independent and self-normalized across all currency pairs and timeframes. If the ATR handle is unavailable or there is insufficient history, the gate cannot be opened (hysteresis detection remains active), so the protection is never silently disabled.
Persistence and GlobalVariable IPC
The timer calls CheckPersistence() repeatedly. Once the lagging condition remains active for more than PersistenceSec seconds, the class performs a read-before-write check on the named global variable "Terminal_Lag_Detected" and sets it to 1.0 only if no other EA has declared it. Ownership is tracked internally; when lag is cleared, only the owning instance resets the flag.
cleaning life cycle
Init() resets all timing and hysteresis state, preventing stale values in OnDeinit / OnInit cycles. Deinit() releases the ATR indicator handle and deletes it only if the GlobalVariable is owned. The destructor is intentionally empty — explicit resource management follows the MQL5 standard.
#includeInput Oolong InpLagThresholdMs = 500 ; Input integer InpPersistenceSec = 3 ; Input integer InpAtrPeriod = 14 ; Input double InpAtrSma multiplier = 1.5 ; CLatencyMonitor g_latency; integer initialization () { if (!g_latency.Init(InpLagThresholdMs, InpPersistenceSec, InpAtrPeriod, InpAtrSmaMultiplier, _symbol , _period )) Return initialization failed ; event set timer ( 1 ); // 1 second timer for persistence check returns initialization successful ; } Blank solution initialization ( constant integer reason) { EventExpirationTimer (); g_latency.Deinit(); } Blank check () { Boolean newLag = g_latency.check (); if (new lag) comment ( "DeeFX | Lag detected — trade execution paused" ); } Blank timer () { boolean persistence = g_latency.CheckPersistence(); if (persistent) Alert ( "DeeFX | Persistence Hysteresis > " , InpPersistenceSec, "s — GV flag set" ); if (!g_latency.IsLagActive()) Comment ( "" ); }
Any other EA on the same terminal can independently read the shared flag:
// In the second EA - no need to include if ( global variable get ( "Terminal Delay Detected" ) >= 1.0 ) Print ( "Terminal lagging signal received - skip placing order." );
initialize() parameters
read-only accessor
Global variable IPC
Attachment download
📎CLatencyMonitor.mqh (7.43 KB)
Source: MQL5 #70046
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •