Rolling Correlation Monitor
Seven open positions across seven symbols looks like diversification until five of those symbols move together. A correlation matrix on its own does not settle the question either, because a high correlation between two positions can mean either thing: a long and a short on correlated symbols is a hedge, while two longs on the same correlated symbols is the same bet placed twice. This EA computes a rolling Pearson correlation of bar-to-bar returns across a configurable watch list, then, for every pair of symbols that currently have open positions, multiplies the raw correlation by the sign of each position's net direction before comparing it against the warning threshold. A positive sign-adjusted value means the two positions reinforce one directional view; a strongly negative value means one offsets the other. Only the reinforcing case is flagged as clustered risk - genuine hedges are reported but left alone, which is what separates this from simply printing a correlation table and leaving the interpretation to the trader. The correlation window recalculates once per new bar of the chosen calculation timeframe rather than every tick, because a full pairwise matrix across a ten-symbol watch list is real work and does not need sub-second freshness to be useful. Notes and limitations - This tool is read-only. It never places, modifies or closes an order - it only reports on positions that already exist. - A symbol never charted in this terminal before may not have enough history loaded yet for the rolling window; the report says so per pair rather than guessing with a partial sample. - Correlation is computed on simple bar-to-bar returns, not log returns or a decay-weighted average. That is a deliberate simplicity choice for a monitoring tool; it is not a substitute for a proper risk model on a large book. - The sign-adjustment uses each symbol's net position (buys minus sells in lots) to decide long or short. On a symbol with exactly offsetting buy and sell tickets, net is zero and is treated as long for sign purposes - a genuinely flat net exposure on that symbol makes the clustering question moot anyway.
Expert advisor · MT4 · v1.0 · Free
About Rolling Correlation Monitor
Rolling Correlation Monitor is a script for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 6 settings, such as Watch symbols, Timeframe and Lookback bars. MQL source code included.
Files in the download: RollingCorrelationMonitor.ex4, RollingCorrelationMonitor.mq4, RollingCorrelationMonitor.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Watch symbols | EURUSD,GBPUSD,USDJPY,AUDUSD,USDCHF,NZDUS | Comma separated watch list (max 10) |
| Timeframe | PERIOD_H1 | Timeframe used for the correlation calculation |
| Lookback bars | 100 | Number of bars/returns in the rolling window |
| Warn threshold | 0.75 | |sign-adjusted correlation| above this triggers a warning |
| Alert on warn | true | Pop an Alert() the first time a pair crosses the threshold |
| Max symbols | 10 | Safety cap on how many watch-list symbols are processed |
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: Hedging · Alerts · MT4 Expert Advisor · MQL4 Source Code