Loading…

Mean Reversion Half Life Estimator

"Is this pair mean-reverting" is usually answered by eye, from a chart that happens to look range-bound. This script answers it with the same regression technique used by this catalogue's HalfLifeReversionSystem Expert Advisor (E341), offered here as a standalone measurement tool with no trading attached - point it at any symbol and timeframe to check before building a strategy around the assumption. It regresses the bar-to-bar price change against the price level over the lookback window - dx[t] = a + b*x[t], the standard discrete-time Ornstein-Uhlenbeck-to-AR(1) discretisation - using plain double-precision sums. The slope b maps to a mean-reversion speed theta = -ln(1+b), and theta to a half-life in bars, ln(2)/theta. The naive version of this stops at one lookback window and reports its number as though it were a fixed property of the instrument. This script fits InpNumWindows consecutive, non-overlapping windows walking backward from the current bar and reports each one separately - a half-life that holds roughly steady across windows is a genuinely different situation from one that swings from 8 bars to invalid to 40 bars, even though a single-window study could report either. Every rejection is explicit and explained: a non-negative slope (no reversion measured), an implied reversion speed with no real solution, or a half-life that is non-positive or longer than the fitted window itself are all reported by name rather than silently producing a number. Notes and limitations This script only measures and reports; it never opens, modifies or closes a position. Use its output to decide whether a mean-reversion strategy is worth building for a given symbol/timeframe, not as a signal on its own. A half-life is a property of the fitted window, not a fixed constant of the instrument. A regime change (a range turning into a trend, or the reverse) will show up as later windows failing to fit or reporting a very different half-life from earlier ones - that instability is itself the useful information this script surfaces by fitting several windows instead of one. R-squared is reported per window but is not used as a pass/fail guard here, unlike the trading EA this technique is shared with - a weak but genuinely negative slope is still shown, with its low R-squared visible, so the reader can judge the fit's trustworthiness themselves rather than have it hidden by a threshold.

Script · MT5 · v1.0 · Free

About Mean Reversion Half Life Estimator

Mean Reversion Half Life Estimator is a script for MetaTrader 5. It can alert you with a pop-up, sound, email or push message. It has 3 settings, such as Lookback bars, Num windows and Save csv. MQL source code included.

Files in the download: MeanReversionHalfLife.ex5, MeanReversionHalfLife.mq5, MeanReversionHalfLife.txt.

Settings

SettingDefaultWhat it does
Lookback bars150Bars used to fit the AR(1) regression
Num windows5How many consecutive lookback windows to fit, walking back in time
Save csvtrueWrite the per-window results to a CSV file

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 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: Alerts · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria