Loading…

Cointegration Spread Z-Score

The naive version of a pairs-trading spread is just priceA minus priceB, and it quietly breaks the moment the two legs trade at different price scales - a spread between a 5000-point instrument and a 1-point instrument is meaningless without a hedge ratio to bring them onto the same footing. This indicator estimates that ratio properly, by ordinary least-squares regression of the second symbol's closes against the chart symbol's closes over a rolling window, and only then computes the residual spread and standardises it into a z-score. The regression is recalculated from scratch for every bar using a trailing window of that bar's own closed history, so the hedge ratio adapts as the relationship between the two instruments drifts, rather than being fixed once at the start. The z-score answers the actual pairs-trading question directly: how many standard deviations is today's spread away from its own recent mean. Dotted reference lines at +/-InpEntryZ mark where a spread is conventionally considered stretched enough to trade, alongside the zero line. Both closes are read at the same bar shift index on both symbols, which is the standard simplification for pairs trading on the same broker feed and timeframe - see NOTES for when that assumption weakens. Notes and limitations - Both legs are read at the same bar shift index, which assumes they share the chart's timeframe and closely aligned session hours - true for most forex pairs on the same broker feed, approximate for a forex pair against an instrument with different session hours (an index or a metal, for instance). - The hedge ratio is estimated by simple linear regression without an intercept correction beyond what z-scoring removes; it is a hedge ratio for spread trading, not a statement about cointegration in the formal econometric sense - no stationarity test is run on the residual, so a pair can look mean-reverting in-sample and simply not be. - Every value is computed from closed bars only (the window always starts one bar in the past relative to the bar being evaluated), so nothing plotted here can repaint on a later tick. - A symbol with insufficient history for the configured lookback shows no value (empty) rather than a misleading zero.

Indicator · MT4 · v1.0 · Free

About Cointegration Spread Z-Score

Cointegration Spread Z-Score is an indicator for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 6 settings, such as Second symbol, Symbol suffix and Lookback bars. MQL source code included.

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

Settings

SettingDefaultWhat it does
Second symbolGBPUSDSecond leg of the pair (the chart's own symbol is the first leg)
Symbol suffixBroker suffix appended to InpSecondSymbol, leave blank if none
Lookback bars100Regression and z-score window, in closed bars
Entry z2.0|Z| beyond this is flagged as a stretched, tradeable spread
Exit z0.5|Z| back inside this is flagged as reverted to fair value
Alert on crosstrueAlert once when a closed bar first crosses InpEntryZ or InpExitZ

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 → Indicators.
  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 indicator from the Navigator onto a chart and adjust the settings.

Download the PDF guide

Tags: Hedging · Alerts · MT4 Indicator · MQL4 Source Code

MetaTrader 4 & 5 indicators · PolyMaria