Cointegration Pair Trading EA
- Experts
- 버전: 1.1
- 업데이트됨: 8 11월 2026
- 활성화: 10
Cointegration Pair Trading EA — market-neutral statistical arbitrage for MetaTrader 5
Trade the relationship, not the direction. Cointegration Pair Trading EA runs a portfolio of linked currency pairs from one chart. For every pair it measures the spread between the two symbols, tests whether that spread really mean-reverts, and when it stretches far enough it buys the cheap leg and sells the expensive leg at the same time. The position is closed when the spread returns to its mean.
Statistics, re-tested on every new bar
- Hedge ratio from a rolling OLS regression of log prices, or an adaptive Kalman filter (switchable).
- Engle-Granger cointegration test: ADF test on the regression residuals with MacKinnon p-value. Shown for every pair; optional entry filter.
- Correlation of returns and half-life of mean reversion as entry filters (pairs whose spread does not revert are blocked).
- Spread z-score on the lookback window; the hedge is frozen at entry so exits use the same spread that was traded.
Entries and exits
- Entry when |z| reaches the entry level (default 2.0) — both legs at once, long or short spread.
- Exit at the mean (or at a chosen exit z), z-stop (default 4.5), maximum holding bars, optional half-life time stop, optional exit when cointegration breaks.
- Carry-aware entries: holding both legs of a spread pays swap on two positions. The default swap mode only opens spreads whose expected swap is zero or positive; a limit mode caps swap cost as a share of the expected reversion.
- Cooldown after each exit; long-only / short-only spread option.
Leg safety
- Both legs are sent back to back. The hedge leg is retried on requotes; if it still cannot be filled, the first leg is closed immediately — no unhedged positions.
- Orphan legs found later (manual close, stop-out) are closed automatically.
- One symbol belongs to one pair, so the EA is safe on netting and hedging accounts.
- Planned exits wait until the rollover window with its wide spreads is over; z-stop and money stops act immediately.
- Open spreads are restored after a terminal restart.
Risk management
- Risk profiles with one input: Conservative (0.5% risk per pair, default), Balanced (1.25%) and Aggressive (2%). Pair money stop, portfolio loss cap and exposure limit scale with the profile; Custom uses your own values.
- Beta-neutral, risk-based sizing: leg A is sized so that a move from entry to the z-stop loses the chosen % of equity; leg B = hedge ratio × leg A notional. The trade is skipped if lot rounding distorts the hedge by more than a set %.
- Small accounts: optional min-lot hedge, only if its risk stays within a set multiple of the target.
- Per-pair money stop, portfolio floating-loss cap (closes all and pauses entries for 24 h), max open pairs, max share of free margin.
- Currency exposure limit: net exposure per currency across all open spreads is capped (multiple of equity).
- Spread-cost filter: skips entries whose round-trip spread cost is too large versus the expected reversion.
- Weekend filter, rollover filter, optional close before the weekend. Free margin, volume limits, filling modes and all trade return codes are handled.
Pair screener panel
- Live table for every pair: correlation, cointegration p-value, half-life, hedge ratio, z-score, state (ready, signal, long, short, blocked with reason) and P/L.
- Spread z-score chart of the selected pair with entry, exit and stop bands.
- KPI cards: open pairs, floating P/L, closed spreads (win rate, profit factor), largest currency exposure.
- Pause / resume, close all (with confirmation), light and dark themes, DPI scaling, movable and minimisable, keeps candles clear of the panel.
Symbols and brokers
- Symbol names are resolved at runtime (suffixes such as .a, .m, -ECN; index aliases); the fully tradable variant is preferred.
- Default portfolio (H4): EURUSD/GBPUSD, AUDUSD/NZDUSD, EURJPY/GBPJPY, CADJPY/AUDJPY, EURCAD/GBPCAD, AUDCAD/NZDCAD, EURCHF/GBPCHF, AUDCHF/NZDCHF, EURNZD/GBPNZD, EURAUD/GBPAUD. Any other pairs (including metals and indices) can be entered as A/B.
- A pair whose symbol is missing is disabled silently; the rest keeps working.
Backtest (Strategy Tester)
Default 10-pair H4 portfolio, 2020.01 – 2026.10, 10 000 USD, raw-spread ECN account data, commission and swap included, 1-minute OHLC model:
| Profile | Recovery factor | Profit factor | Net profit | Max equity drawdown |
|---|---|---|---|---|
| Conservative (0.5%) | 3.20 | 1.23 | +2 836 USD (+28%) | 7.4% |
| Balanced (1.25%) | 1.97 | 1.17 | +6 354 USD (+64%) | 21.4% |
| Aggressive (2%) | 1.65 | 1.15 | +8 798 USD (+88%) | 31.8% |
Profit factor counts each leg as a separate trade. About 30 spreads per year — the EA waits for statistically stretched spreads. Monte Carlo resampling of the closed spreads (Conservative): 99.8% of 2 000 resampled paths ended in profit (Balanced: 98.4%). 2020 and 2025 were flat-to-negative years.
Presets
- Conservative / Balanced / Aggressive — the three risk profiles on the default 10-pair H4 portfolio.
- Strict cointegration — only pairs with p-value ≤ 0.5, entry z 2.5.
- Prop-firm conservative — 0.25% risk, max 2 open pairs, tighter loss caps, closes before the weekend.
How to start
- Attach the EA to any one chart (any symbol, any timeframe) — it runs the whole portfolio.
- Choose a risk profile (or load a preset); make sure the symbols are available with your broker.
- Enable Algo Trading. Test the free demo in the Strategy Tester first (multi-symbol mode, "Every tick based on real ticks").
Results shown are backtests. Past performance does not guarantee future results. Pair trading reduces, but does not remove, market risk: relationships between instruments can change.

