Close Book By Risk Contribution
"Cut risk" is usually done by closing whichever position looks worst on screen, or the newest one, or a round number of tickets - none of which is actually a measure of risk. This script measures it directly: for every open position with a stop loss, the money that would be lost if that stop were hit, converted through the symbol's own tick value and tick size rather than a hardcoded contract size. The positions are then sorted by that figure and closed largest-first until the sum of what remains falls under a target percentage of current equity. The naive version of this script would ignore any position with no stop loss, since it has no defined risk to sum. That is the wrong default - an unstopped position is exactly the one that should be flagged as the most dangerous thing in the book, not silently excluded from a risk report because it cannot be measured. Three policies are offered: rank it as the highest risk in the book (the default, and the safest starting point), price a hypothetical stop at a configurable distance and include that instead, or explicitly exclude it - available for a trader who deliberately runs some positions without a stop and does not want them touched by this tool. Heat is computed once at the start of the run and the plan is built against that snapshot; it is not recomputed position-by-position as tickets close, since a position's own risk contribution does not change because a different position closed. The plan is shown in full before anything happens, and confirmation is required by default. Notes and limitations - Only market positions with a priceable stop, or one assigned by policy, count toward the heat total. Excluded stopless positions are invisible to the target percentage entirely - the book can still be materially riskier than the reported number if several positions are excluded. - The stop distance used is the current stop loss price, not the original one. A stop already moved to breakeven or trailed contributes little or nothing to the heat total, which is correct - that position genuinely does not risk much money at its current stop. - A stop on the wrong side of entry (which should not normally happen) is treated as zero risk rather than negative risk. - This is a one-time cut, not a running risk manager. It does not re-evaluate as price moves after it finishes - run it again if the book changes. - Closing uses a generous fixed slippage tolerance since the point of this script is to cut risk with certainty, not to get the best price on the way out.
Script · MT5 · v1.0 · Free
About Close Book By Risk Contribution
Close Book By Risk Contribution is an expert advisor (trading robot) for MetaTrader 5. It has 8 settings, such as Max heat percent, Current symbol only and Magic filter. MQL source code included.
Files in the download: CloseByRiskContribution.ex5, CloseByRiskContribution.mq5, CloseByRiskContribution.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 |
|---|---|---|
| Max heat percent | 5.0 | Target: stop total risk below this % of equity |
| Current symbol only | false | Restrict to this chart's symbol |
| Magic filter | -1 | Magic number (-1 covers every magic) |
| No stop policy | NOSTOP_TREAT_AS_HIGHEST | Positions with no stop loss |
| Fallback stop pips | 200.0 | Used only by the fallback-pips policy above |
| Confirm first | true | Ask for confirmation before closing |
| 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: MT5 Script · MQL5 Source Code