Loading…

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 · MT5 · v1.0 · Free

About Cointegrated Basket System

Cointegrated Basket System 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 Basket symbols, Timeframe and Regression bars. MQL source code included.

Files in the download: CointegratedBasketSystem.ex5, CointegratedBasketSystem.mq5, CointegratedBasketSystem.txt.

Trading robots can lose money. Check every setting and test on a demo account before using real funds.

Settings

SettingDefaultWhat it does
Basket symbolsEURUSD,GBPUSD,USDCHF,AUDUSDFirst = dependent leg, rest = independent legs (3-6 symbols)
TimeframePERIOD_H1Timeframe the regression and z-score are measured on
Regression bars200Closed bars used to re-estimate the basket weights
Zscore bars100Bars of spread history used for the entry/exit z-score
Entry z2.0|z| at/above this opens the basket trade
Exit z0.5|z| at/below this closes the open basket trade
Atr period14ATR period on the A leg, used to size the stop/risk
Stop atr multiple3.0Stop distance on the A leg, in ATR multiples
Risk percent1.5Risk requested on the A leg, percent of equity
Magic number20260909Magic number for positions 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 5 open File → Open Data Folder, then go to MQL5 → Experts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 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: Hedging · Multi-Currency · Risk & Lot Size · ATR · MT5 Expert Advisor · MQL5 Source Code

Expert advisors (EA) for MT4 & MT5 · PolyMaria