Comprehensive Stationarity Testing Suite for MetaTrader

Comprehensive Stationarity Testing Suite for MetaTrader
Download ALL MT5 scripts (411)
YouTube Video Thumbnail



Similar MetaTrader Tools

Comprehensive Stationarity Testing Suite for MetaTrader

Info

The Comprehensive Stationarity Testing Suite for MetaTrader is a Script for MetaTrader 5 that stationarity testing suite this suite includes five tests for distinguishing random walks from mean-reverting series, augmenting the standard augmented dickey-fuller test. The included tests are: - KPSS (Null Hypothesis: Stationarity) - Phillips-Perron - DF-GLS (Elliott, Rothenberg, and Stock): Offers higher power near a unit root compared to ADF.

Usage

This tool is typically used for enhancing chart analysis and decision making.

Platform

This Script works exclusively on MetaTrader 5 (both build 600+ and newer versions).

Setup

Place the downloaded file in MQL5/Scripts folder via File ? Open Data Folder in MetaTrader 5.


How to Install and Use Comprehensive Stationarity Testing Suite for MetaTrader

1. Installation: Move your script file into the MQL/Scripts directory and restart the platform.

2. Execution: Drag the script onto a chart; it will perform a one-time action, such as closing all open orders or clearing chart objects.

3. Editing: Use MetaEditor (F4) to modify code, click "Compile," and verify no errors appear in the terminal before running.

4. Removing: Scripts stop automatically, but you can remove them manually by right-clicking the chart and choosing "Remove Script."

Frequently Asked Questions

Q: How are scripts different from EAs? A: Scripts execute a single action and then stop; EAs monitor the market and trade continuously.

Q: Can I assign a hotkey to a script? A: Yes, right-click the script in the Navigator, select "Set Hotkey," and define your preferred keyboard shortcut.

Q: Why did my script stop? A: Scripts are designed to stop immediately after finishing their programmed command; this is normal behavior.

What this tool does

Stationarity Testing Suite This suite includes five tests for distinguishing random walks from mean-reverting series, augmenting the standard Augmented Dickey-Fuller test.

Typical Use Case

This Script excels in automated trading and technical analysis on MetaTrader 5.

Compatible Platform & Setup

This Script works on MetaTrader 5. Place the file in the MQL5/Scripts folder and restart the terminal.

Description & Settings

Related: portfolio scorer: comprehensive multi-ea correlation and coverage analysis - another powerful script for MetaTrader 5 traders.

Stationarity Testing Suite This suite includes five tests for distinguishing random walks from mean-reverting series, augmenting the standard Augmented Dickey-Fuller test. The included tests are: - KPSS (Null Hypothesis: Stationarity) - Phillips-Perron - DF-GLS (Elliott, Rothenberg, and Stock): Offers higher power near a unit root compared to ADF. - Zivot-Andrews: Tests for a unit root against a stationary series with one structural break, identifying the break date. - Lo-MacKinlay Variance Ratio Each test provides its statistic, p-value, critical values (1%, 5%, 10%), and the selected lags or bandwidth. No external DLLs are required. Usage Example: #include vector y; // Example: log prices // ... fill y ... SStationarityResult r; if(CStationarity::KPSS(y, r, ST_TREND_CONSTANT)) PrintFormat("KPSS %.3f, p-value %.3f, bandwidth %d", r.stat, r.pvalue, r.lags); if(CStationarity::DFGLS(y, r, ST_TREND_LINEAR)) PrintFormat("DF-GLS %.3f, p-value %.3f, %d lags", r.stat, r.pvalue, r.lags); if(CStationarity::PhillipsPerron(y, r)) PrintFormat("Phillips-Perron %.3f, p-value %.3f", r.stat, r.pvalue); if(CStationarity::ZivotAndrews(y, r, ZA_BREAK_BOTH)) PrintFormat("Zivot-Andrews %.3f, p-value %.3f, break at bar %d", r.stat, r.pvalue, (int)r.extra); if(CStationarity::VarianceRatio(y, r, 10)) PrintFormat("VR(10) %.3f, p-value %.3f", r.extra, r.pvalue);

Also recommended: Copula Analysis: Gaussian, Student t, Clayton, Gumbel, and Frank Copulas with Tail Dependence - similar script with strong performance on MetaTrader 5.


Detailed Test Descriptions

KPSS: Tests for stationarity with constant or trend. Bandwidth selection can be automatic (Hobijn, Franses, and Ooms method), a calculated value of 12*(n/100)^(1/4), or user-defined. P-values are derived through interpolation of simulated quantiles. Phillips-Perron: Available for no constant, constant, or trend specifications. Uses Z-tau or Z-rho statistics, Newey-West long-run variance estimation, and MacKinnon's p-values and critical values. DFGLS: Tests for unit root with constant or trend. The series is detrended using GLS with c = -7 or -13.5. Lags are selected via AIC, BIC, or t-statistic on the OLS-detrended series. ZivotAndrews: Detects a structural break in level, trend, or both. Allows testing at any date between trimmed ends (default 15%). Efficiently reuses cross-products of non-varying columns for regression, achieving approximately 0.1 seconds for 2,800 bars. VarianceRatio: Compares the q-period variance to the 1-period variance. Supports overlapping or non-overlapping periods and debiased calculations. Uses the heteroskedasticity-robust variance from Lo and MacKinlay. A ratio below 1 indicates mean reversion; a ratio above 1 suggests momentum. The ratio is stored in r.extra. Auxiliary Functions: SelectLags: Aids in selecting the lag length for Dickey-Fuller regressions. DickeyFullerP / DickeyFullerCrit: Public functions for t-statistics and normalized bias (z) statistics. The p-value and critical value tables are located in StationarityTables.mqh. Example Report: The script StationarityReport.mq5 performs these tests on the log price and returns of a selected symbol. It outputs each result with a 5% significance verdict, the most likely break date, and variance ratios for periods 2 to 60. For EURUSD Daily, the price acts as a random walk, and returns are stationary, as expected. A likely break date identified was November 2026. Verification: Cross-checked against R's `arch.unitroot` (version 8.0) on six diverse series: a random walk, an AR(1) stationary series, a trend-stationary series, a series with a level break, gold log prices (H4), and BTCUSD log prices (D1). This involved 204 tests across all trend options, lag rules, and parameters. Results showed: - Identical lags, bandwidths, observation counts, break dates, and critical values. - Statistics accurate to within 3e-11. - P-values accurate to within 2e-11. File Placement: - Stationarity.mqh and StationarityTables.mqh: Place in MQL5\Include\Stationarity - StationarityReport.mq5: Place in MQL5\Scripts\Stationarity

You may also like: symbol summary viewer - excellent alternative for script users on MetaTrader 5.

Source Code

#property copyright "RobotFX"
#property version     "1.00"
#property script_show_inputs
#include <Stationarity\Stationarity.mqh>
input string          InpSymbol = "";
input ENUM_TIMEFRAMES InpPeriod = PERIOD_D1;
input int             InpBars   = 1500;
input string          InpCsv    = "";
void Line(const string name, const SStationarityResult &r, const bool nullStationary)
  {

.......

Leave your opinion, ask a question, share some knowledge

⚠ Limitations & Risk Warning

  • This tool is provided for educational and testing purposes only.
  • Past performance does not guarantee future results.
  • Trading involves substantial risk of loss. Use on a demo account first.
  • Results may vary depending on market conditions, broker, and settings.
  • We recommend thorough backtesting and forward testing before using with real funds.
© ROBOTFX Free educational tools by RobotFX. Use entirely at your own risk; we are not liable for any financial losses incurred.