Loading…

Stationarity And Unit Root Tests

A series that has wandered back toward where it started looks mean-reverting on a chart. A genuine random walk does exactly that constantly, with zero actual mean reversion - the eye cannot tell the two apart, and a strategy built on the difference cannot survive not being able to tell them apart either. These two tests exist to replace that eyeball judgement with a real statistic before capital depends on the answer. The Augmented Dickey-Fuller test regresses the change in a series against its own lagged level, with several lagged differences included to absorb short-run autocorrelation the plain Dickey-Fuller test would otherwise mistake for evidence either way. If the level's own coefficient comes back reliably negative - measured as a t-statistic well below the tabulated critical value - the series is pulling back toward its mean rather than drifting freely, which is the definition of stationary used here. The regression is solved by plain OLS via the normal equations, using a small self-contained Gaussian elimination with partial pivoting; there is no external matrix dependency and the routine is sized for the handful of regressors an ADF test actually needs, not general linear algebra. The variance ratio test asks a complementary, more direct question: for a true random walk, the variance of a k-bar return must equal almost exactly k times the variance of a 1-bar return, because independent steps add variance linearly. A ratio well below k-normalised 1.0 means moves are cancelling each other out at that horizon - mean reversion. A ratio above 1.0 means moves are reinforcing each other - momentum. Lo and MacKinlay's overlapping-sample estimator and its asymptotic standard error under the homoskedastic random-walk null turn that ratio into a z-statistic that can actually be judged rather than read as a vibe. Both tests report standard tabulated critical values (MacKinnon's constant-only ADF thresholds; the 1.96 two-sided 5% z-threshold for the variance ratio) rather than computing an exact finite-sample distribution. That is adequate for a trading decision on which spread or instrument deserves further work - it is not a substitute for a full statistical package if publication-grade precision is required. The demo runs both tests against three synthetic series with known properties - a pure random walk, a strongly mean-reverting AR(1), and a drifting/trending walk - so the reported numbers can be checked against what each series actually is before trusting the tests on real, unlabelled data. Notes and limitations - Raw price is almost always a unit-root process. Finding "non-stationary" on outright price is the expected, uninteresting result - these tests are far more informative applied to a SPREAD between two correlated instruments, or to a strategy's own equity curve, than to price directly. - Critical values used here are the commonly tabulated asymptotic constants for the constant-only ADF specification, not an exact finite-sample distribution computed for your specific sample size. Treat a result close to the threshold as inconclusive rather than a hard yes/no. - ADF assumes the lag order you specify is adequate to whiten the residuals. Too few lags can leave autocorrelation that biases the test toward finding stationarity that is not really there; try more than one lag count on anything that matters. - The variance ratio test's standard error here assumes homoskedasticity. Markets are not homoskedastic - volatility clusters - which can inflate the z-statistic and produce more apparent rejections of the random walk than a heteroskedasticity-robust version would. Treat a marginal result with appropriate scepticism. - VarianceRatioTest requires every price strictly positive (it works in log price); a spread that can be negative or zero must be tested with AdfTest instead, or shifted to strictly positive first. - Both tests need a meaningfully sized sample - the demo's defaults (around 300 observations) are close to a practical minimum, not a generous one.

Script · MT4 · v1.0 · Free

About Stationarity And Unit Root Tests

Stationarity And Unit Root Tests is a script for MetaTrader 4. It has 4 settings, such as Series length, Adf lags and Vr horizon. MQL source code included.

Files in the download: StationarityTests.mqh, StationarityTests.txt, StationarityTestsDemo.mq4.

Settings

SettingDefaultWhat it does
Series length300Length of each synthetic test series
Adf lags1Lagged-difference terms in the ADF regression
Vr horizon4k horizon for the variance ratio test
Price bars300Bars of live Close used in the price test

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

MQL4 & MQL5 scripts · PolyMaria