Pairs Portfolio Manager
Most "pairs trading" EAs compare a raw price difference or a fixed ratio between two symbols, which is not cointegration and drifts as soon as the two instruments' price scales diverge. This EA estimates a real hedge ratio for every A:B pair in InpPairsList by ordinary least squares over InpRegressionBars closed bars - beta = Cov(A,B) / Var(B) - so the spread A - beta*B is the residual of A regressed on B, not a guessed multiple. Beta is re-estimated every tick from the latest closed bars, so a hedge ratio that fit six months ago on a relationship that has since broken down does not go on being used. The spread's own mean and standard deviation over InpZscoreBars give the z-score that drives entries and exits: a position opens when |z| clears InpEntryZ (long the spread when it is unusually low, short it when unusually high) and closes when |z| falls back to InpExitZ - both relative to the pair's own recent behaviour, not a fixed distance that would mean something different on a tight pair versus a volatile one. Every pair's B leg is sized by beta against the A leg's ATR-based risk, so each trade is an actual hedge rather than two unrelated fixed lots dressed up as a spread trade. A single InpMaxPortfolioRiskPercent budget is shared across every pair currently open; a new pair whose requested risk would exceed the remaining headroom is skipped rather than opened anyway - the common failure mode of running several independent pair strategies that each assume they alone own the account's risk. If the B leg of a new spread fails to fill after the A leg already has, the EA unwinds the A leg immediately rather than leave an accidental, un-hedged directional position that the portfolio cap never approved. Notes and limitations - A least-squares hedge ratio assumes the pair's relationship is reasonably stable over InpRegressionBars; a pair that is not actually cointegrated will still produce a beta and a z-score, they simply will not mean-revert reliably - this is a mechanical implementation of the math, not a guarantee any given pair is a genuine cointegrated relationship. - Only the A leg carries an explicit stop-loss; the B leg is a hedge sized against it and is closed alongside the A leg on exit, not stopped independently. - If the B leg fails to fill after the A leg opens, the A leg is unwound immediately - a brief window of single-leg exposure is possible between the two OrderSend calls. - The portfolio risk cap only accounts for positions this EA itself opened under InpMagicNumber; it does not see risk from other EAs or manual trades on the same account.
Expert advisor · MT5 · v1.0 · Free
About Pairs Portfolio Manager
Pairs Portfolio Manager is an expert advisor (trading robot) for MetaTrader 5 based on ATR. It can size trades from your balance and risk percent. It has 13 settings, such as Pairs list, Regression bars and Zscore bars. MQL source code included.
Files in the download: PairsPortfolioManager.ex5, PairsPortfolioManager.mq5, PairsPortfolioManager.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 |
|---|---|---|
| Pairs list | EURUSD:GBPUSD,AUDUSD:NZDUSD,USDCAD:USDCH | Comma-separated A:B pairs |
| Regression bars | 200 | Bars used to estimate the hedge ratio (beta) |
| Zscore bars | 100 | Bars used to compute the spread's mean/stddev for the z-score |
| Entry z | 2.0 | |z| at/above this opens a position in that pair |
| Exit z | 0.5 | |z| at/below this closes an open position in that pair |
| Max portfolio risk percent | 6.0 | Total risk budget across all open pairs, percent of equity |
| Risk per pair percent | 1.5 | Risk budget requested per pair, percent of equity (capped by the portfolio total) |
| Atr period | 14 | ATR period on the A leg, used to size the stop/risk |
| Stop atr multiple | 3.0 | Stop distance on the A leg, in ATR multiples |
| Magic number | 20260903 | 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: Hedging · Arbitrage · Risk & Lot Size · ATR · MT5 Expert Advisor · MQL5 Source Code