Loading…

Slippage Expectation Model

MQL4's order history does not retain the price a trader actually requested - only OrderOpenPrice(), the price the broker actually filled at. There is no function, and no combination of functions, that recovers "how much did I get slipped, on average, trading EURUSD around the London open" from an account's existing trade history, because the requested price only ever existed for the instant between calling OrderSend() and it returning. Any library claiming to calibrate slippage expectations purely from history is quietly making up the requested-price half of the comparison. This library is built around that constraint rather than around it: every sample must be handed in by the caller at the moment of the trade - the requested price captured immediately before OrderSend(), the filled price read from OrderOpenPrice() immediately after. Each sample is recorded into a (symbol, hour-of-day) bucket using Welford's online algorithm, so the running mean and standard deviation update in O(1) per sample with no stored history array, however many trades feed the model over time. SemGetExpectation() reads back the mean, standard deviation, worst observed adverse slippage and sample count for a bucket. SemExpectedFillPrice() applies the recorded mean slippage to a planned entry price, useful for a sanity check on whether a stop is realistically placed once expected slippage is accounted for, not just the raw requested price. Notes and limitations - This is a live-data collector, not a historical back-fill tool. It cannot produce any statistics for a symbol/hour it has not itself been fed samples for during the current run (or a run that reloaded saved data the caller chose to persist itself - this header does not persist anything to disk on its own). - Bucketing is by server-time hour-of-day only, not by day-of-week or news-calendar proximity - a broker that shifts server time relative to a trader's local session will bucket accordingly, which is usually what is wanted for "what does MY broker's server-time hour X look like". - The internal table is a linear array searched per call. Fine for the realistic size of this problem (a handful of symbols times 24 hours), not intended for thousands of distinct buckets. - SemExpectedFillPrice() applies the MEAN slippage only - it does not attempt to model the distribution's tail, which SemGetExpectation()'s worstAdversePoints and stdDevPoints outputs are there to inform separately.

Script · MT4 · v1.0 · Free

About Slippage Expectation Model

Slippage Expectation Model is a script for MetaTrader 4. MQL source code included.

Files in the download: SlippageExpectationModel.mqh, SlippageExpectationModel.txt, SlippageExpectationModelDemo.mq4.

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: Trading Sessions · EURUSD · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria