Latency Compensated Stop Placer
A stop-loss placed the instant a signal fires already sits behind however long the order took to reach the broker and come back confirmed. On a fast-moving market that gap alone can eat into the stop's intended cushion before the position is even fully live - the market can move further during the round trip than the stop distance accounted for. This program measures its own round-trip latency honestly rather than assuming a number: every time it applies a compensation via OrderModify, it times that exact call with GetTickCount() before and after, and feeds the result into a rolling average. The very act of compensating a stop is what produces the next latency sample, so the estimate is built from this terminal's real conditions and improves the longer the EA runs. Before any real sample exists it falls back to a conservative fixed estimate. The compensation size itself is not the raw latency figure - it is the latency multiplied by a recent price-velocity estimate (average true range over a configurable lookback, spread across that window's real elapsed time rather than assumed bar length), so a slow, quiet market gets a small nudge and a fast, volatile one gets a larger one, capped at a hard maximum so a bad velocity read cannot blow the stop distance out. Each order is compensated once only, the first time it is seen with a stop-loss already set - this is a one-time correction applied shortly after an order appears, not a continuous trailing mechanism. Notes and limitations - This program modifies stop-loss values on orders matching the configured magic number, including orders it did not open itself. Only point InpMagicFilter at a magic you actively manage. - Compensation happens once per ticket, the first time the order is seen with a stop already set. It does not re-run if the owning EA moves the stop again later (trailing, breakeven, etc.) - only the very first stop value on a newly-seen ticket is compensated. - An order with no stop-loss at all is left untouched; there is nothing to compensate. - The latency figure is this terminal's own OrderModify round trip, not the round trip of whatever EA originally sent the order - it is a reasonable proxy assuming both run on the same connection, not a direct measurement of the original OrderSend. - A compensation that would land inside the broker's stop or freeze level is skipped and logged rather than sent, since the broker would reject it anyway.
Expert advisor · MT5 · v1.0 · Free
About Latency Compensated Stop Placer
Latency Compensated Stop Placer is an expert advisor (trading robot) for MetaTrader 5. It has a news filter and looks for price differences (arbitrage). It has 8 settings, such as Magic filter, Symbol filter and Volatility lookback. MQL source code included.
Files in the download: LatencyCompensatedStops.ex5, LatencyCompensatedStops.mq5, LatencyCompensatedStops.txt.
Trading robots can lose money. Check every setting and test on a demo account before using real funds.
Settings
| Setting | Default | What it does |
|---|---|---|
| Magic filter | 0 | Magic number whose positions get stop compensation (be specific - this modifies stops) |
| Symbol filter | Only manage this symbol; blank = every symbol | |
| Volatility lookback | 14 | Bars used to estimate recent price velocity (ATR-style) |
| Fallback latency ms | 150 | Assumed round-trip latency before any real sample exists |
| Latency samples | 20 | Rolling window size for the measured-latency average |
| Max compensation pips | 10.0 | Hard cap on how far the stop can be moved, pips |
| Max retries | 3 | PositionModify attempts per ticket before giving up |
| Retry wait ms | 300 | Pause between retry attempts, milliseconds |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Experts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
- Test it first on a demo account or in the Strategy Tester.
Tags: Buy/Sell Arrows · Arbitrage · News Trading · MT5 Expert Advisor · MQL5 Source Code