Loading…

Cluster Neutral Book Builder

A hand-picked list of "these pairs move together" grouping goes stale the moment correlations shift, and a book that is long-short by symbol count alone is not actually neutral if the two legs carry very different volatility or currency exposure. On each rebalance pass this EA builds a real Pearson correlation matrix from daily close-to-close returns across the whole candidate list, then clusters symbols using threshold union-find: any two symbols whose absolute correlation clears InpClusterThreshold are merged into the same cluster, and that merging propagates transitively (if A clusters with B and B clusters with C, all three end up in one cluster even if A and C alone do not clear the threshold). A symbol whose correlation to everything else falls short is left in a cluster of one and stays flat - there is nothing to pair it against. Inside a cluster of two or more, members are ranked by recent momentum. The weaker half is bought and the stronger half is sold, on the working assumption that a correlated cluster's members converge back toward each other over time. Each leg is first sized to an equal percentage-of-equity risk using its own ATR, then the short leg is scaled so the cluster's total short money-risk matches its total long money-risk as closely as lot stepping allows. Because every cluster is built to be individually neutral this way, the whole book - being a sum of neutral clusters - stays close to flat in aggregate without a separate whole-book balancing pass bolted on afterward. Existing positions are tracked purely by this EA's own magic number, ticket-collected on each pass; a symbol that drops out of a real cluster (now a singleton) has its position closed, and a symbol whose target side flips between passes is closed and reopened rather than left stale. Notes and limitations - Correlation and clustering are recomputed from scratch every rebalance pass, not incrementally - a symbol's cluster membership can genuinely change from one pass to the next as its recent returns change. - Clustering uses at most 100 correlation bars internally regardless of InpCorrelationBars, to keep the per-pass correlation matrix a bounded size; values above 100 are silently capped. - Cluster neutrality is money-risk neutral (ATR-weighted), not simple lot- count neutral - a cluster can hold more short lots than long lots (or vice versa) while still being risk-balanced, by design. - An odd-sized cluster is not perfectly balanced member-for-member; the scaling step balances risk, not headcount, between the two sides. - A symbol without enough loaded history for the configured InpCorrelationBars is excluded from clustering for that pass and stays in its own singleton (flat) rather than blocking the whole rebalance. - Existing same-direction positions are not resized between rebalances even if the target lot size has drifted; resizing only happens on the next full pass, or immediately on a direction flip. - No stop-loss is placed by design - risk control here is the ATR-based sizing and the risk-parity balancing, not a price stop.

Expert advisor · MT5 · v1.0 · Free

About Cluster Neutral Book Builder

Cluster Neutral Book Builder 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 Symbol list, Correlation tf and Correlation bars. MQL source code included.

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

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

Settings

SettingDefaultWhat it does
Symbol listEURUSD,GBPUSD,AUDUSD,NZDUSD,USDCAD,USDCHComma-separated candidate symbols
Correlation tfPERIOD_D1Timeframe the return series is built from
Correlation bars60Number of bar-to-bar returns used per correlation pair
Cluster threshold0.55Minimum absolute Pearson correlation to join two symbols into one cluster
Momentum bars10Bars back used to rank relative strength inside a cluster
Risk percent per leg0.5Percent of equity risked per leg (per symbol), before cluster balancing
Atr period14ATR period used for volatility-based sizing
Rebalance interval hours12Hours between full re-cluster and re-size passes
Magic number20272001Magic number identifying this book's own positions
Slippage pips5Slippage allowance, in pips
Dry runfalsetrue = log intended actions only, never trades
Max retries5Attempts per order 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: Risk & Lot Size · ATR · MT5 Expert Advisor · MQL5 Source Code

Expert advisors (EA) for MT4 & MT5 · PolyMaria