CLatency Monitor - Inter-Tick Latency Tracker with ATR Volatility Gate and Global Variable IPC for MT5 EAs
Info
The CLatency Monitor - Inter-Tick Latency Tracker with ATR Volatility Gate and Global Variable IPC for MT5 EAs is a Library for MetaTrader 5 that clatencymonitor is a versatile mql5 include-file library designed to address a critical issue in execution-sensitive expert advisors (eas): silent fill degradation or risk miscalculation due to tick feed lag. This library offers a comprehensive solution with three key features:Real-time Inter-Tick Delta Measurement: Utilizes GetTickCount64() to accurately measure the time elapsed between consecutive ticks, identifying abnormally large gaps.
Usage
This tool is typically used for enhancing chart analysis and decision making.
Platform
This Library works exclusively on MetaTrader 5 (both build 600+ and newer versions).
Setup
Place the downloaded file in MQL5/Libraries folder via File ? Open Data Folder in MetaTrader 5.
How to Install and Use CLatency Monitor - Inter-Tick Latency Tracker with ATR Volatility Gate and Global Variable IPC for MT5 EAs
1. Storage: Place library files in the MQL/Libraries directory to ensure they are accessible to your projects.
2. Implementation: Include the library in your code using the #import directive, ensuring you match the exact function names and parameters.
3. Compilation: Ensure the library is present in the directory before you compile your main EA or script, as the compiler links them during this phase.
4. Management: Keep libraries organized in sub-folders if you manage many custom functions to maintain a clean project structure.
Frequently Asked Questions
Q: What is a library file used for? A: Libraries store reusable code modules, allowing you to centralize common logic used by multiple EAs or indicators.
Q: Is a library executable? A: No, libraries are non-executable files containing functions; they must be imported into an EA, indicator, or script to function.
Q: Can I update a library while the platform is running? A: You should compile your EA or script after updating a library to ensure the latest code changes are integrated.
What this tool does
CLatencyMonitor is a versatile MQL5 include-file library designed to address a critical issue in execution-sensitive Expert Advisors (EAs): silent fill degradation or risk miscalculation due to tick feed lag.
Typical Use Case
This Library excels in automated trading and technical analysis on MetaTrader 5.
Compatible Platform & Setup
This Library works on MetaTrader 5. Place the file in the MQL5/Libraries folder and restart the terminal.
Description & Settings
Related: Time in microseconds (µs) since 01/01/1970 - another powerful library for MetaTrader 5 traders.
CLatencyMonitor is a versatile MQL5 include-file library designed to address a critical issue in execution-sensitive Expert Advisors (EAs): silent fill degradation or risk miscalculation due to tick feed lag.Also recommended: Log4mql (MT5) - similar library with strong performance on MetaTrader 5.
This library offers a comprehensive solution with three key features:
Real-time Inter-Tick Delta Measurement: Utilizes GetTickCount64() to accurately measure the time elapsed between consecutive ticks, identifying abnormally large gaps.
ATR Volatility Gate: Employs a self-normalizing Average True Range (ATR) volatility gate to differentiate genuine feed lag from natural tick spacing during high-volatility periods, ensuring alerts are not triggered by normal market behavior.
Cross-EA Inter-Process Communication (IPC): Utilizes a named GlobalVariable flag, "Terminal_Lag_Detected", to enable communication between EAs running on the same terminal. This allows coordinated responses to confirmed lag events, such as pausing, hedging, or alerting the trader.
The library is easy to integrate, requiring only a single #include statement in any EA or service that requires latency awareness. It is a lightweight and dependency-free solution, ensuring efficient performance.
How It Works
Inter-Tick Delta: On each OnTick() call, the class measures the time elapsed since the previous tick, seeding the timer on the first tick to avoid false alerts.
ATR Volatility Gate: The class compares the current ATR value with a self-normalizing threshold, calculated as AtrSmaMultiplier × SMA(ATR, AtrPeriod). This ensures that the lag detection is instrument-agnostic and adapts to varying market conditions across different pairs and timeframes.
Persistence and GlobalVariable IPC: A repeating timer triggers CheckPersistence(), which monitors the duration of lag conditions. If a lag persists beyond PersistenceSec seconds, the class sets the GlobalVariable flag, allowing other EAs to respond accordingly. The flag is reset when the lag clears, ensuring efficient resource management.
Clean Lifecycle: Init() and Deinit() methods ensure proper resource management, initializing and releasing the ATR indicator handle, and managing the GlobalVariable flag ownership. The destructor is intentionally empty, adhering to MQL5 standards.
Integration Example: Other EAs on the same terminal can independently monitor the GlobalVariable flag to detect and respond to lag events.
Parameters and Accessors
Init() Parameters:
- lagThresholdMs: Millisecond threshold for inter-tick delta to trigger a lag candidate (default: 500).
- persistenceSec: Seconds of lag persistence before setting the GlobalVariable flag (default: 3).
- atrPeriod: Lookback period for ATR and its SMA (default: 14).
- atrSmaMultiplier: Multiplier for ATR SMA to set the high-volatility threshold (default: 1.5).
- symbol: Chart symbol for iATR() (typically _Symbol).
- tf: Timeframe for iATR() (typically _Period).
Public Methods:
- bool Init(...): Initializes ATR handle and resets state, returning true even if ATR handle fails (non-fatal).
- bool OnTick(): Called from OnTick(), returns true when a new lag condition is triggered.
- bool CheckPersistence(): Called from OnTimer(), returns true when lag exceeds persistence threshold, managing the GlobalVariable flag.
- void Deinit(): Releases ATR handle and clears GlobalVariable if owned, called from OnDeinit().
Read-Only Accessors:
- ulong LastDeltaMs(): Elapsed milliseconds since the last tick (returns 0 before the first tick).
- bool IsLagActive(): Indicates if a lag condition is active.
- bool IsAtrValid(): Confirms ATR handle creation and volatility gate operation.
GlobalVariable IPC:
- Flag Name: "Terminal_Lag_Detected" (defined as GV_LAG_FLAG).
- Set to 1.0 for persistent lag, reset to 0.0 or deleted when lag clears.
- Read-before-write mechanism prevents flag overwrite by multiple EAs.
Changelog:
v1.0 - Initial release.
v1.1 - Code optimizations: Removed unused members, improved cross-compiler compatibility.
v1.2 - Lifecycle enhancements: Destructor cleared, Init() improved for EA reinitialization.
You may also like: Ozymandias Signal Module - excellent alternative for library users on MetaTrader 5.
⚠ Limitations & Risk Warning
- This tool is provided for educational and testing purposes only.
- Past performance does not guarantee future results.
- Trading involves substantial risk of loss. Use on a demo account first.
- Results may vary depending on market conditions, broker, and settings.
- We recommend thorough backtesting and forward testing before using with real funds.