Loading…

Distribution Shift Detector

Traders regularly compare "this year versus last year" by eyeballing a mean and a standard deviation, but two distributions can have nearly identical means and standard deviations while having very different shapes - fatter tails, a skew flip, a bimodal split. A two-sample Kolmogorov-Smirnov test compares the whole empirical distribution rather than a single summary figure, which is what makes it able to catch a genuine regime change that a mean/stdev comparison misses. This script collects close-to-close percent returns for two user-chosen date ranges, sorts each sample set, and computes the exact KS D statistic - the largest vertical gap between the two empirical cumulative distribution functions - with a merge-scan over both sorted arrays. This is the genuine statistic, not an approximation: every sample point from both ranges is walked in value order, and both empirical CDFs are tracked simultaneously so ties are handled correctly. The result is compared against the standard asymptotic critical value formula, c(alpha) * sqrt((n1+n2)/(n1*n2)), at the 10%, 5% and 1% significance levels, using the textbook Kolmogorov distribution coefficients. A significant result says the two windows are unlikely to be drawn from the same distribution - it does not say why, or pinpoint the exact day the shift happened. The mean and standard deviation of each range are printed alongside the test so a trader can see whether a significant shift looks like a level change, a volatility change, or both. Notes and limitations - Requires at least 20 samples in each range to run; fewer than that makes any distributional test unreliable regardless of the result. - The critical value table used is the standard large-sample asymptotic approximation, accurate for the sample sizes typical of a return study; it is not the small-sample exact Kolmogorov distribution. - A significant D statistic identifies that the ranges differ - it does not identify the transition date, the cause, or which direction the shift went. Cross-check the printed mean/stdev to interpret the shift. - Returns are computed on percentage change, not price or money, so ranges from different symbols or price scales remain comparable.

Script · MT5 · v1.0 · Free

About Distribution Shift Detector

Distribution Shift Detector is a script for MetaTrader 5. It can alert you with a pop-up, sound, email or push message. It has 7 settings, such as Symbol, Timeframe and Range a from. MQL source code included.

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

Settings

SettingDefaultWhat it does
SymbolSymbol to study, blank = this chart's symbol
TimeframePERIOD_D1Timeframe whose returns are compared
Range a fromD'2018.01.01 00:00'Reference range start (older regime)
Range a toD'2019.12.31 23:59'Reference range end
Range b fromD'2023.01.01 00:00'Comparison range start (newer regime)
Range b toD'2024.12.31 23:59'Comparison range end
Save csvtrueWrite both sample sets 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