Loading…

Target Weight Rebalancer

A basket held at fixed proportions - say 40% EURUSD, 30% GBPUSD, 30% XAUUSD - drifts out of those proportions as prices move, exactly like a portfolio of stocks does. Left alone, the winners grow to dominate the basket and the losers shrink to irrelevance, which is a real, if often unintended, change to the basket's risk profile. Rebalancing back to target on a schedule, or whenever drift exceeds a threshold, is the standard fix - tedious to do by hand across several symbols, straightforward to automate. Every value this EA uses to decide how much to trade is derived from MODE_TICKVALUE and MODE_TICKSIZE, never a hardcoded contract size. The formula - money per one unit of price move, for one lot, in account currency - is tickValue divided by tickSize; multiplying that by the current price and by the actual lots held gives a symbol's real notional value on any instrument a broker lists, correctly and identically for a standard FX pair, gold, or an index CFD with a completely different contract size. Every trade the EA sizes to correct drift goes through that same formula, scaled by however many lots are actually being added or trimmed - never a fixed dollar-per-lot assumption that only happens to be right on one kind of symbol. Two independent triggers decide when a rebalance runs: drift, checked every InpCheckIntervalSeconds, fires only when a symbol has moved more than InpDriftThresholdPercent of equity away from its target; and an optional daily or weekly schedule, which rebalances every basket symbol back to its exact target regardless of how small the drift is. Both can run together, drift catching a fast move intraday and the schedule tidying up small remaining drift on a fixed calendar day. Notes and limitations - Long only. The basket is assumed to hold long exposure in each symbol; it opens additional BUY orders to add and closes existing BUY orders to trim. It does not open short positions and is not designed to manage a mixed long/short basket. - Only positions carrying this EA's own InpMagic are counted as part of the basket and only those are ever trimmed - manual trades or other EAs' positions on the same symbol are left alone. - No stop-loss or take-profit is placed on any position this EA opens. It manages target exposure only; pair it with your own separate risk controls if you need protective stops. - When trimming, the oldest matching tickets are closed first, in full or partially on the last ticket needed, so the basket's remaining cost basis is always its most recently added lots. - A symbol whose tick value or tick size cannot currently be read is skipped for that cycle with a log line, rather than sized with a guessed number. - Rebalancing trades at market price with the configured slippage tolerance; a fast-moving symbol can still see a difference between the intended and the filled adjustment size.

Expert advisor · MT5 · v1.0 · Free

About Target Weight Rebalancer

Target Weight Rebalancer is an expert advisor (trading robot) for MetaTrader 5. It has 10 settings, such as Basket spec, Rebalance mode and Drift threshold percent. MQL source code included.

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

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

Settings

SettingDefaultWhat it does
Basket specEURUSD:40,GBPUSD:30,XAUUSD:30Symbol:WeightPercent pairs, comma separated
Rebalance modeREBAL_BOTHWhat triggers a rebalance
Drift threshold percent5.0Percent of equity a symbol may drift before trimming/adding
Schedule hour21Server hour (0-23) the scheduled rebalance runs
Schedule day of week-1-1 = every day, 0=Sunday .. 6=Saturday
Check interval seconds3600Seconds between drift checks and schedule checks
Magic20261214Magic number for every order this EA sends
Slippage pips3Slippage tolerance, in pips
Max retries5Attempts per order before giving up this cycle
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: Gold (XAUUSD) · EURUSD · GBPUSD · MT5 Expert Advisor · MQL5 Source Code

Expert advisors (EA) for MT4 & MT5 · PolyMaria