Cost Model Calibrator
Nearly every backtest cost assumption in a published strategy is a guess: "3 pip spread, 1 pip slippage" typed into an input once and never checked against what the account actually experienced. This library measures the three real cost components separately, because MQL4's own data model supports each of them very differently - and says so honestly instead of faking the ones it cannot do. Spread is fully calibratable, but only live, going forward. MQL4's closed- order history stores the price that was filled, never the opposite-side quote at the same instant - there is no function that hands back "what was the spread when this order filled" after the fact. RecordSpreadSample() must be called repeatedly while an EA runs (every tick or every new bar) and builds a running mean, standard deviation, minimum and maximum per symbol using Welford's online algorithm, so no raw sample history needs to be kept in memory. Slippage cannot be reconstructed from history at all, and this library does not pretend otherwise. OrderOpenPrice() on a historical order is the price that was filled, not the price that was requested - MQL4 simply never stores the second number anywhere. Genuine slippage calibration needs the caller to record its own requested price at the moment it calls OrderSend, then report both prices here via RecordSlippageSample() once the ticket confirms. Anyone claiming to calibrate slippage from OrdersHistoryTotal() alone is calibrating against data that does not exist. Swap is the one model genuinely derivable from stored history alone. Every closed order keeps its own real OrderSwap() charge, and MarketInfo(symbol, MODE_SWAPLONG/MODE_SWAPSHORT) gives the broker's currently advertised nightly rate. CalibrateSwapFromHistory() walks closed orders on a symbol, sums the actual charges, and compares them against what the advertised rate times lots times nights held would predict - useful for catching triple-swap weekend charges, broker rate changes since the trade closed, or rounding that a naive backtest cost model would otherwise miss. Notes and limitations - The slippage part of the demo uses synthetic requested/filled price pairs to show the API's shape - it does not place a real order (this library never calls OrderSend), so there is no real requested price to obtain honestly inside a demo script. Real slippage numbers only come from your own EA recording its own requested price. - Spread and slippage statistics live in memory only, reset when the attached script/EA/chart stops. Persist them yourself (a file, a GlobalVariable-backed summary) if a calibration needs to survive a restart. - CalibrateSwapFromHistory()'s "nights held" is whole days between open and close time, rounded down. It does not model the exact rollover moment, which is broker-server-time-dependent and not exposed by MQL4 - close to correct for most symbols, not exact for one held exactly across a single rollover boundary. - Swap rate comparison uses the CURRENT MODE_SWAPLONG/MODE_SWAPSHORT value for every historical order, even one whose broker rate has since changed - this is a genuine limitation, not an oversight: MQL4 does not store the swap rate that was in effect on a given historical date, only the actual charge (OrderSwap()) and today's rate. - A symbol using SYMBOL_SWAP_MODE reopen-style rollover mechanics (where the broker closes and reopens the position at a marked price instead of charging a plain per-night fee) will not calibrate meaningfully against a fixed nightly-rate model at all - the underlying swap mechanism is different in kind, not just in rate.
Script · MT4 · v1.0 · Free
About Cost Model Calibrator
Cost Model Calibrator is a script for MetaTrader 4. It has 3 settings, such as Spread samples, Sample delay ms and Max history orders. MQL source code included.
Files in the download: CostModelCalibrator.mqh, CostModelCalibrator.txt, CostModelCalibratorDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Spread samples | 5 | Live spread samples to take, one per loop pass |
| Sample delay ms | 200 | Delay between spread samples, milliseconds |
| Max history orders | 200 | Cap on closed orders examined for swap calibration |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT4 Script · MQL4 Source Code