Loading…

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 · MT4 · v1.0 · Free

About Latency Compensated Stop Placer

Latency Compensated Stop Placer is a script for MetaTrader 4. It has 8 settings, such as Magic filter, Symbol filter and Volatility lookback. MQL source code included.

Files in the download: LatencyCompensatedStops.ex4, LatencyCompensatedStops.mq4, LatencyCompensatedStops.txt.

Settings

SettingDefaultWhat it does
Magic filter0Magic number whose orders get stop compensation (be specific - this modifies stops)
Symbol filterOnly manage this symbol; blank = every symbol
Volatility lookback14Bars used to estimate recent price velocity (ATR-style)
Fallback latency ms150Assumed round-trip latency before any real sample exists
Latency samples20Rolling window size for the measured-latency average
Max compensation pips10.0Hard cap on how far the stop can be moved, pips
Max retries3OrderModify attempts per ticket before giving up
Retry wait ms300Pause between retry attempts, milliseconds

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: Buy/Sell Arrows · Arbitrage · MT4 Expert Advisor · MQL4 Source Code

Expert advisors (EA) for MT4 & MT5 · PolyMaria