Basket Risk Rebalancer
A basket built up over a session rarely ends with equal risk per position. One trade got sized generously because it looked good at the time, another was added half-heartedly, and by the end the basket's total risk is dominated by whichever position happened to be biggest, not by which one is actually the best idea. This script measures the money each stopped position would lose if its own stop were hit, using the broker's tick value and tick size so it is correct for FX, gold, indices and anything else without a single hardcoded contract size. The smallest risk already present in the basket becomes the target every other position is trimmed toward. That direction is deliberate and not a shortcut: MQL4 has no call that grows a live order's volume in place, only a partial close that can shrink it. Equalising upward would mean opening brand new tickets and quietly changing the basket's composition; equalising downward only ever removes risk, which is the one direction safe to automate. Positions with no stop loss are left alone entirely - there is nothing to equalise against - and are reported separately so they are not mistaken for already-balanced. The script always prints a full plan first: every ticket, its current risk, the lots that would be trimmed, and the money that trim removes. Nothing is sent to the server unless InpConfirmExecute is set to true AND InpConfirmPhrase is typed exactly as REBALANCE. Getting one of the two gates wrong runs the preview again instead of touching anything - there is no way to fire it by accident. Notes and limitations - Only shrinks positions. It never opens a new order or grows an existing one - the smallest current risk in the basket is the floor everything else is trimmed toward, not a number you set directly. - A position with no stop loss is excluded from both the target calculation and the trimming - protect it first with a separate tool if it should be included. - A trim is a real partial close at current market price plus a small slippage allowance; it is not free and pays the spread on the volume removed, the same as any other close. - Retries only cover the broker's ordinary temporary refusals (requote, busy context, and similar). A hard rejection - trading disabled, market closed - is reported per ticket and the script moves on rather than looping forever. - Running it repeatedly on a basket that has not changed does nothing further once every position is within InpMinTrimLots of the target.
Script · MT5 · v1.0 · Free
About Basket Risk Rebalancer
Basket Risk Rebalancer is an expert advisor (trading robot) for MetaTrader 5. It has 7 settings, such as Current symbol only, Magic filter and Min trim lots. MQL source code included.
Files in the download: BasketRiskRebalancer.ex5, BasketRiskRebalancer.mq5, BasketRiskRebalancer.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 |
|---|---|---|
| Current symbol only | false | Restrict to this chart's symbol |
| Magic filter | -1 | Only this magic (-1 means every magic) |
| Min trim lots | 0.01 | Skip a trim smaller than this many lots (broker noise floor) |
| Confirm execute | false | Must be true to actually send partial closes |
| Confirm phrase | Must equal REBALANCE exactly to execute | |
| Max retries | 5 | Attempts per position 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: Trading Sessions · MT5 Script · MQL5 Source Code