Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

CExecutionSafety - Ping + Execution Delay Protection for MQL5 EA - MetaTrader 5 Library

author EAcpu | 5 reads | 0 comments |

CExecutionSafety - Ping + Execution Latency Guard for MQL5 EAs - library for MetaTrader 5

CExecutionSafety - Ping + Execution Latency Guard for MQL5 EAs - library for MetaTrader 5

CExecutionSafety is a lightweight MQL5 included class designed to protect Expert Advisors from executing transactions under poor network or execution conditions.
It measures two latency components: real-time terminal ping and real wall-clock execution latency, and blocks transaction attempts when their combined value exceeds a configurable threshold.

Most EAs will execute blindly regardless of network conditions. Spikes in ping or broker-side execution latency during critical orders can result in slippage, requotes, or execution failures.
CExecutionSafety introduces a pre-trade safety gate that blocks execution if conditions deteriorate.

This class uses two data sources:

The two values ​​are added and compared to your configured threshold. If the combined latency exceeds the threshold, CheckExecutionSafety() returns false and prints a diagnostic log entry.
CExecutionSafety - Ping + Execution Latency Guard for MQL5 EAs - library for MetaTrader 5

TERMINAL_PING_LAST returns -1 when the terminal has no server contact. The class detects this and automatically returns false, preventing transaction attempts on disconnected terminals.

Include the file and instantiate the class in your EA:

#include CExecutionSafety safety; int OnInit() { safety.Init(150); // 150ms combined threshold return INIT_SUCCEEDED; } void OnTick() { if(!safety.CheckExecutionSafety()) return; } ulong t0 = GetTickCount64(); trade.PositionClose(ticket); safety.RecordExecDelay(GetTickCount64() - t0); }

The following read-only accessors can be used for dashboard display or external logic:


Attachment download

📎 CExecutionSafety.mqh (3.76 KB)

Source: MQL5 #70033

Verification code Refresh