Cointegrated Basket System
A basket "spread" built from several independently estimated two-symbol hedge ratios does not jointly minimise anything - each ratio was fit ignoring every other leg. This EA instead treats InpBasketSymbols' first symbol as the dependent leg (A) and every other symbol as an independent leg, and every InpRegressionBars closed bars solves the REAL multiple-regression weights beta_2..beta_K that minimise the residual of A - beta_2*Leg2 - ... - beta_K*LegK jointly, by forming the normal equations (X'X)*beta = X'y on demeaned closes and solving them with Gauss-Jordan elimination with partial pivoting - the same linear algebra a statistics package would use, implemented directly because MQL4 has no matrix library. A near-singular system (independent legs too collinear to separate) is detected by pivot size and refused rather than silently returning a wild, ill-conditioned weight. The resulting basket spread is z-scored over InpZscoreBars exactly like a two-legged pair, entering when |z| clears InpEntryZ and exiting at InpExitZ. Every leg is sized off the A leg's ATR-based risk, scaled by its own regression weight, with each independent leg taking the side implied by the sign of its weight - a negative weight means that leg trades the SAME direction as A, not the opposite, because a negative coefficient means that leg moves opposite A in the fitted relationship. If any leg after the first fails to fill, every leg already opened is unwound immediately rather than left as a partial, un-hedged basket the risk sizing never approved. Notes and limitations - A least-squares basket weight assumes the legs' joint relationship is reasonably stable over InpRegressionBars; a basket that is not actually cointegrated still produces weights and a z-score, they simply will not mean-revert reliably. - Only the A leg carries an explicit stop-loss; the independent legs are hedges sized against it and are closed alongside A on exit, not stopped independently. - Regression is refused outright (weights unavailable that bar) when the independent legs are too collinear for Gauss-Jordan elimination to find a stable pivot - this is a real safeguard, not a rare edge case, if two independent legs are themselves highly correlated. - Opening a basket sends the legs sequentially; a brief window of partial exposure is possible between each OrderSend, and any leg failing to fill unwinds everything opened so far for that entry. - All legs share one InpMagicNumber and are identified by a common order-comment tag; running two instances of this EA on overlapping symbol sets under the same magic number will confuse leg matching.
Expert advisor · MT4 · v1.0 · Free
About Cointegrated Basket System
Cointegrated Basket System is an expert advisor (trading robot) for MetaTrader 4 based on ATR. It can size trades from your balance and risk percent. It has 13 settings, such as Basket symbols, Timeframe and Regression bars. MQL source code included.
Files in the download: CointegratedBasketSystem.ex4, CointegratedBasketSystem.mq4, CointegratedBasketSystem.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 |
|---|---|---|
| Basket symbols | EURUSD,GBPUSD,USDCHF,AUDUSD | First = dependent leg, rest = independent legs (3-6 symbols) |
| Timeframe | PERIOD_H1 | Timeframe the regression and z-score are measured on |
| Regression bars | 200 | Closed bars used to re-estimate the basket weights |
| Zscore bars | 100 | Bars of spread history used for the entry/exit z-score |
| Entry z | 2.0 | |z| at/above this opens the basket trade |
| Exit z | 0.5 | |z| at/below this closes the open basket trade |
| 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 |
| Risk percent | 1.5 | Risk requested on the A leg, percent of equity |
| Magic number | 20260909 | 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: Hedging · Multi-Currency · Risk & Lot Size · ATR · MT4 Expert Advisor · MQL4 Source Code