Pair Spread And Z Score Engine
Pairs trading lives or dies on one question: is the spread between two instruments actually mean reverting, and how stretched is it right now? A raw price difference between two symbols answers neither question, because two instruments almost never trade at comparable scales or volatility - EURUSD minus USDJPY is not a meaningful number, and neither is a small-cap stock minus the index it belongs to. This library fits an ordinary least squares hedge ratio (beta) of one series on the other first, so the spread it builds - y minus (beta times x plus intercept) - is the residual a statistician would actually call a spread, not an arbitrary difference of two unrelated price scales. PSZ_HedgeRatio returns 0.0 rather than dividing by zero when the independent series has no variance over the sample window, which happens more often than expected on a short or unusually quiet lookback. From there, PSZ_RollingMean and PSZ_RollingStdDev slide a fixed window across the spread, and PSZ_ZScore standardises the latest spread reading against that window - the number a pairs strategy actually watches for an entry or exit threshold. A window with zero variance (a spread that has not moved at all) is guarded to a clean 0.0 rather than an infinite or NaN score, which would otherwise poison any signal built on top of it. PSZ_BuildZScoreSeries ties the whole pipeline together in one call: fit the hedge ratio over the full sample, build the spread, and produce a full rolling z score series alongside it, with the warm-up bars before the first full window set to 0.0 instead of left uninitialised. Notes and limitations - Arrays are treated as plain, chronologically ordered data (index 0 = oldest) on both platforms. Chart price series fetched as MQL4 timeseries, or MQL5 arrays via CopyClose without ArraySetAsSeries(false), can have index 0 as the newest bar - reverse or re-order them before passing in. - The hedge ratio is fitted once over the whole sample passed in. For a ratio that adapts as the relationship drifts, refit periodically on a recent rolling window rather than the full history. - A z score threshold (2.0, 2.5, whatever a strategy chooses) is a statistical convenience, not a guarantee of reversion - a spread can keep widening well past any fixed threshold when the underlying relationship between the two instruments has genuinely broken, not merely stretched. - This library does not size positions or manage the trade once a signal fires, on either platform; it only measures the spread and how unusual its current value is.
Script · MT5 · v1.0 · Free
About Pair Spread And Z Score Engine
Pair Spread And Z Score Engine is a script for MetaTrader 5. It has 3 settings, such as Series length, Z score period and True beta. MQL source code included.
Files in the download: PairSpreadZScore.mq5.mqh, PairSpreadZScore.txt, PairSpreadZScoreDemo.ex5, PairSpreadZScoreDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Series length | 60 | Number of synthetic bars to generate |
| Z score period | 20 | Rolling window used for the z score |
| True beta | 1.35 | Hedge ratio baked into the synthetic data |
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 → Scripts.
- 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 script onto a chart to run it once.
Tags: Hedging · Arbitrage · EURUSD · USDJPY · MT5 Script · MQL5 Source Code