Loading…

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

SettingDefaultWhat it does
Reference symbolGBPUSDReference symbol read for the correlation signal
Corr period100Bars used to compute the rolling correlation
Min correlation0.60Minimum |correlation| required before trading is allowed
Divergence period20Bars used to measure the cumulative return divergence
Entry std dev2.0Divergence z-score that opens a trade
Exit std dev0.5Divergence z-score that closes a trade (convergence target)
Atr period14ATR period for the stop and take profit
Stop atr multiple2.0Stop loss distance from entry, in ATR multiples
Take profit atr mult3.0Take profit distance from entry, in ATR multiples (0 = none)
Use risk sizingfalseSize by percent risk instead of a fixed lot
Risk percent1.0Percent of equity risked per trade, when risk sizing is on
Fixed lots0.10Lot size when risk sizing is off, or as a fallback
Magic number20260706Magic number for orders opened by this EA
Slippage pips3Slippage tolerance, in pips
Max retries5Attempts per trade action before giving up
Retry wait ms500Pause between attempts, milliseconds

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Experts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
  6. Test it first on a demo account or in the Strategy Tester.

Download the PDF guide

Tags: Divergence · Buy/Sell Arrows · Risk & Lot Size · ATR · MT4 Expert Advisor · MQL4 Source Code

Expert advisors (EA) for MT4 & MT5 · PolyMaria