Spurious Correlation Demonstrator
"These two symbols are 0.85 correlated over the last 20 bars" sounds like a discovery. It usually is not one. With enough independent series and a short enough measurement window, high correlations turn up routinely from pure noise - no shared driver, no real relationship, just the arithmetic of small samples. This script does not merely assert that. It builds a chosen number of completely independent random-walk series using the built-in random generator (Box-Muller transformed into a normal-ish increment, since MQL4 has no built-in normal generator), then measures the Pearson correlation of every unique pair over a short window and a long window. It counts how many pairs exceeded a chosen "looks strong" threshold at each window length and reports the percentage - typically far higher on the short window, from noise alone, since every series here is independent by construction. Alongside the simulated counts it prints a rough analytic significance cutoff (the standard two-sided 5% approximation, 1.96/sqrt(window-1)) so the user can see how many of the short-window "strong" correlations would not even have cleared a real significance test, and how tightly that cutoff closes once the window gets long enough to mean something. The lesson transfers directly to markets: a correlation measured over a handful of bars between two symbols is measuring sampling noise more often than it is measuring a genuine relationship, and needs either a longer window or a proper significance test before being used to size a hedge or a cross-pair filter. Notes and limitations - All series are synthetic random walks generated inside the script - it does not read or use any real market data, deliberately, so every correlation it finds is guaranteed to be coincidental by construction. - MQL4 cannot declare a genuinely dynamic two-dimensional array (only the leftmost dimension of a multi-dimensional array is resizable at runtime; further dimensions must be a fixed compile-time size). Since both the series count and window length here are runtime inputs, all generated series are stored in one flat 1D array with manual row-major indexing (row * stride + column) rather than a true 2D array - a reimplementation of the "many parallel series" pattern noted in the house style guide, adapted for a runtime-sized case. - The printed significance cutoff is a rough two-sided 5% approximation for illustration, not a rigorous statistical test - do not treat it as a substitute for a proper correlation significance calculation in a real analysis. - With a small InpSeriesCount the percentage figures are noisy themselves (few pairs to average over); increase InpSeriesCount for a more stable demonstration.
Script · MT4 · v1.0 · Free
About Spurious Correlation Demonstrator
Spurious Correlation Demonstrator is a script for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 5 settings, such as Series count, Short window and Long window. MQL source code included.
Files in the download: SpuriousCorrelationDemo.mq4, SpuriousCorrelationDemo.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Series count | 30 | Number of independent random-walk series to generate |
| Short window | 20 | Short window length, bars |
| Long window | 250 | Long window length, bars, for comparison |
| Alert threshold | 0.70 | |correlation| above this counts as "strong" in the tally |
| Random seed | 0 | Seed for MathSrand (0 = use GetTickCount, different every run) |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Alerts · MT4 Script · MQL4 Source Code