Loading…

Correlation Stability Study

A single headline number like "EURUSD and GBPUSD are 0.8 correlated" hides the more useful question: has that 0.8 held steady, or has the relationship swung between strongly positive and strongly negative across different periods. A correlation that is only true on average across ten years of mixed regimes is not something to size a hedge or a cross-pair filter around. This script computes a rolling Pearson correlation of daily returns between the chart symbol and a second symbol the user names, using a fixed window length, walked bar by bar across the whole shared history. It then reports the mean and standard deviation of that ENTIRE rolling series, plus a year-by-year breakdown, rather than a single number for the whole period. A high standard deviation of the rolling correlation is the actual answer to "can this relationship be relied on" - a low one means the correlation has been consistent across the sampled history, a high one means it has not. The second symbol is selected into Market Watch automatically if it is not already there, and the script waits briefly for its daily history to finish syncing before starting, rather than reading a partially loaded series. Notes and limitations - Daily timeframe only (PERIOD_D1), matched by bar TIME between the two symbols, not by bar index - a symbol with a gap or a different session calendar still lines up correctly because each pair of returns is read independently by shift on its own series. - If InpSecondSymbol has never been charted in this terminal, the history sync can occasionally take longer than InpSyncWaitMs on a slow connection; the script reports plainly when this happens rather than running against an incomplete series. Open a chart on the second symbol once first if this occurs, then re-run. - The year-by-year breakdown buckets each rolling-window value by the calendar year of the window's most recent bar, not its oldest one. - This is a descriptive stability measure, not a cointegration or causality test - a stable correlation does not imply either symbol predicts the other.

Script · MT5 · v1.0 · Free

About Correlation Stability Study

Correlation Stability Study is a script for MetaTrader 5. It can alert you with a pop-up, sound, email or push message. It has 5 settings, such as Second symbol, Window bars and Max bars. MQL source code included.

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

Settings

SettingDefaultWhat it does
Second symbolGBPUSDSecond symbol to correlate against
Window bars20Rolling window length, trading days
Max bars3000Daily bars to scan (0 = all available)
Sync wait ms2000Max wait for the second symbol's history to sync
Output fileCorrelationStabilityStudy.csvOutput file (in MQL5\Files)

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: Hedging · EURUSD · GBPUSD · Alerts · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria