Pair Correlation Snapback System
Two symbols that normally move together - a "pair" in the statistical sense, not necessarily the same asset class - occasionally diverge for a session before reconverging. This EA measures that relationship directly instead of assuming it: a rolling Pearson correlation of the two symbols' bar-to-bar log returns confirms the pair is actually behaving like a pair right now, and a z-score of the log price ratio between them (its rolling mean and standard deviation over a shorter window) measures how far the current ratio sits from its recent normal range. A trade only opens when both conditions hold together: the correlation is above a minimum threshold AND the z-score is beyond an entry threshold. Trading a stretched z-score on an uncorrelated pair is just two unrelated directional bets wearing a matching-trade costume, so the correlation gate exists specifically to prevent that. The trade itself is two legs sent independently - buy the symbol that is cheap relative to the ratio, sell the symbol that is rich relative to it - and both legs are closed together, either when the z-score reverts inside the exit threshold or after a maximum holding period as a safety exit if the divergence never resolves. Each leg carries its own protective stop, sized in pips and normalised per-symbol, because two different symbols on the same account can have very different pip values and margin requirements. Notes and limitations - This project reimplements the catalogue's standard helper functions (retryable-error classification, pip/lot normalisation, position lookup by magic number) locally rather than including another project's file, per house style - each submission is self-contained. - Both legs use the same fixed lot size (InpFixedLots); this is not a beta-weighted or dollar-neutral pair trade, and two symbols with very different pip values will not be risk-balanced against each other. Size conservatively and monitor combined exposure. - If one leg fails to open after the other has filled, the EA closes the filled leg immediately rather than carry a naked single-leg exposure, but a fast market can still leave a brief window of unmatched risk between the two OrderSend calls. - Correlation is measured on log returns aligned by bar shift on the chart's own timeframe; two symbols with different trading sessions (e.g. very different market hours) can have misaligned bars, which will show up as an understated correlation reading rather than a crash. - Requires AutoTrading enabled (IsTradeAllowed()); if trading is disabled the EA logs it and skips the entry. - This is a genuine two-leg strategy, not a single-symbol EA - it will open and close trades on InpSymbolB even though the chart it is attached to is leg A.
Expert advisor · MT5 · v1.0 · Free
About Pair Correlation Snapback System
Pair Correlation Snapback System is an expert advisor (trading robot) for MetaTrader 5. It has 13 settings, such as Symbol b, Correlation period and Z score period. MQL source code included.
Files in the download: PairCorrelationSnapback.ex5, PairCorrelationSnapback.mq5, PairCorrelationSnapback.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 |
|---|---|---|
| Symbol b | GBPUSD | Second leg symbol (leg A is the chart symbol) |
| Correlation period | 60 | Bars of returns used to compute the correlation coefficient |
| Z score period | 20 | Bars used for the spread's rolling mean and standard deviation |
| Entry z score | 2.0 | Absolute z-score required to open a snapback trade |
| Exit z score | 0.4 | Absolute z-score at or below which an open trade is closed |
| Min correlation | 0.6 | Minimum absolute correlation required to trade the pair |
| Max holding bars | 150 | Safety exit: close after this many bars if z has not reverted |
| Stop loss pips | 80 | Protective stop loss on each leg, in pips |
| Fixed lots | 0.10 | Lot size per leg (both legs use the same fixed lot) |
| Magic number | 20260602 | 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 5 open File → Open Data Folder, then go to MQL5 → Experts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 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: Trading Sessions · MT5 Expert Advisor · MQL5 Source Code