Correlation Divergence EA
This is not a pairs trade - it never opens a position on the reference symbol, only reads its prices. That makes it a single-leg strategy with none of a pairs trade's two-sided execution risk, at the cost of not being market-neutral: the chart symbol's own directional risk is fully present. A rolling Pearson correlation between the two symbols' bar-to-bar returns is recalculated every bar over InpCorrPeriod bars. Trading is gated on |correlation| >= InpMinCorrelation - a divergence between two symbols that are not presently correlated is just two unrelated symbols doing unrelated things, not a mean-reversion setup, so no signal fires below that threshold regardless of how large the raw divergence looks. The divergence itself is each symbol's cumulative return over InpDivergencePeriod bars, A minus B, turned into a z-score against that divergence series' own recent mean and standard deviation. Using a z-score rather than a fixed percentage means the entry threshold self-calibrates to how much these two specific symbols normally drift apart, instead of using one number across symbol pairs with very different typical spreads. Trade direction depends on the sign of the correlation: for a positively correlated reference, the chart symbol lagging behind (negative divergence) is bought, expecting it to catch up; for a negatively correlated reference, the same divergence reading trades the other way, because the pair's normal relationship is inverted. Stops and targets are ATR multiples from entry, sized the same way as the single-symbol EAs in this catalogue. Notes and limitations - The correlation gate uses a rolling window and can flip on or off as markets change regime; a trade can be entered while correlated and then managed while the reading has since dropped below the threshold - it is only checked at entry, not while a position is open. - Divergence exit is convergence-based (z back inside InpExitStdDev); the ATR stop and target still apply independently and can close the trade first in either direction. - Signals are evaluated once per closed bar on the chart symbol's timeframe, not on every tick. - One position at a time on the chart symbol, tracked by InpMagicNumber. - Requires AutoTrading enabled. MQL4 checks IsTradeAllowed(); MQL5 checks TERMINAL_TRADE_ALLOWED, ACCOUNT_TRADE_ALLOWED and ACCOUNT_TRADE_EXPERT, since MQL5 has no single equivalent call. If trading is disabled the EA logs it and skips the entry.
Expert advisor · MT4 · v1.0 · Free
About Correlation Divergence EA
Correlation Divergence EA is an expert advisor (trading robot) for MetaTrader 4 based on ATR. It can size trades from your balance and risk percent. It has 16 settings, such as Reference symbol, Corr period and Min correlation. MQL source code included.
Files in the download: CorrelationEa.ex4, CorrelationEa.mq4, CorrelationEa.txt.
Trading robots can lose money. Check every setting and test on a demo account before using real funds.
Settings
| Setting | Default | What it does |
|---|---|---|
| Reference symbol | GBPUSD | Reference symbol read for the correlation signal |
| Corr period | 100 | Bars used to compute the rolling correlation |
| Min correlation | 0.60 | Minimum |correlation| required before trading is allowed |
| Divergence period | 20 | Bars used to measure the cumulative return divergence |
| Entry std dev | 2.0 | Divergence z-score that opens a trade |
| Exit std dev | 0.5 | Divergence z-score that closes a trade (convergence target) |
| Atr period | 14 | ATR period for the stop and take profit |
| Stop atr multiple | 2.0 | Stop loss distance from entry, in ATR multiples |
| Take profit atr mult | 3.0 | Take profit distance from entry, in ATR multiples (0 = none) |
| Use risk sizing | false | Size by percent risk instead of a fixed lot |
| Risk percent | 1.0 | Percent of equity risked per trade, when risk sizing is on |
| Fixed lots | 0.10 | Lot size when risk sizing is off, or as a fallback |
| Magic number | 20260706 | Magic number for orders opened by this EA |
| Slippage pips | 3 | Slippage tolerance, in pips |
| Max retries | 5 | Attempts per trade action before giving up |
| Retry wait ms | 500 | Pause between attempts, milliseconds |
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 → Experts.
- 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 expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
- Test it first on a demo account or in the Strategy Tester.
Tags: Divergence · Buy/Sell Arrows · Risk & Lot Size · ATR · MT4 Expert Advisor · MQL4 Source Code