Partial Close All By Percent
Scaling out of a whole book by hand is slow and error-prone. Scaling out with a script is easy to write and easy to write wrongly, because a partial close is not one volume constraint but two, and almost every published version checks neither. A partial close splits one position into a closed leg and a leg that stays open. The server treats both as volumes it has to accept. Each must sit on the lot step, and each must be at least the broker's minimum. Get either one wrong and the server refuses with error 131, invalid trade volume - and the position is left completely untouched, which is the worst outcome available. You wanted to reduce risk, you pressed the button, you got a red line in the log, and you are still carrying the full size. The first trap is direction. Fifty per cent of 0.03 lots is 0.015, which no broker on a 0.01 step will take. It has to be rounded, and it has to be rounded DOWN. This script closes 0.01 and leaves 0.02. Rounding up to 0.02 would be closing two-thirds of the position when you asked for half - more risk removed than you authorised, which sounds harmless until the position was the hedge. The rule this script keeps is simple: never more than you asked for. If the residue matters, run it again. The second trap is the remainder, and this is the one that catches good code. A slice can be perfectly valid on its own and still be illegal, because of what it leaves behind. Take a broker with a 0.10 minimum on a 0.01 step - common on indices and metals. A 0.25 lot position, closing 80%, gives a slice of 0.20, which is a fine volume. But it leaves 0.05 open, and 0.05 is below the minimum. The server rejects the whole request. This script anticipates that: it works out the largest slice that still leaves a legal remainder - 0.15 here - trims to it, and says so in the log. Trimming closes less than you asked for, which is the same safe direction as the rounding. Some positions cannot be split at all. Any position smaller than twice the broker minimum has no legal division: both legs would need to be at least the minimum, and there is not enough volume for two of them. A 0.15 lot position with a 0.10 minimum is in that state. So is a 0.01 lot position on any normal account. Here the only real options are to close the whole thing or to leave it alone, and the script will not choose for you. By default it skips the position and prints the reason. Set Close whole if too small to true and it will close it in full instead - a deliberate switch, off by default, because a script that quietly liquidates a position when you asked it to trim one is a script that will eventually cost someone real money. Underneath all of it is floating point. In binary, 0.1 / 0.01 does not evaluate to 10 - it evaluates to 9.99999999999999998. Floor that and you get 9, so a slice that should have been 0.10 goes out as 0.09. The bug is invisible because the code looks obviously correct. Every division onto the lot grid in this script is nudged by an epsilon before it is rounded. The rest is ordinary care. Tickets are collected before the first close, results are checked one at a time, temporary errors like requotes and off-quotes are retried and permanent ones are not, slippage is entered in pips and converted to points for 3 and 5 digit quotes, and the summary tells you how many positions were sliced, how many were skipped and why, and how many the broker refused. Notes and limitations - This reduces exposure. It does not manage it. Stop losses and take profits on the remaining volume are left exactly as they were, and on MQL4 the remainder carries them onto its new ticket - check them afterwards if your risk model depends on them. - Trimming a slice to protect the remainder means the script sometimes closes less than the percentage you typed. That is deliberate and it is always logged. It never closes more. - A position at or below twice the broker minimum cannot be partially closed by anyone, including this script. That is a server rule, not a limitation of the code. - Volume constraints come from the server. On a symbol the terminal has never synchronised they can read as zero; the script falls back to a 0.01 step rather than dividing by zero, but a fallback is a guess. If the numbers in the log look wrong for your instrument, open the symbol's specification. - Commission and swap are settled on the closed portion only. Partial closes therefore fragment the trade history, which matters if you feed that history into an analyser. - Some brokers apply FIFO rules and will refuse to close part of a newer position while an older one on the same symbol is open. The refusal is reported per position; it is not something the script can work around. - No margin check. Reducing a position never increases margin, so this is safe here, but do not copy the pattern into code that opens anything.
Script · MT5 · v1.0 · Free
About Partial Close All By Percent
Partial Close All By Percent is an expert advisor (trading robot) for MetaTrader 5. It has 8 settings, such as Close percent, Current symbol only and Magic filter. MQL source code included.
Files in the download: PartialCloseAll.ex5, PartialCloseAll.mq5, PartialCloseAll.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 |
|---|---|---|
| Close percent | 50.0 | Percent of each position to close (over 0, under 100) |
| Current symbol only | false | Restrict to this chart's symbol |
| Magic filter | -1 | Magic number (-1 takes every magic) |
| Close whole if tiny | false | Position too small to split: close it whole |
| Slippage pips | 3 | Slippage tolerance, in pips |
| Max retries | 5 | Attempts per position before giving up |
| Retry wait ms | 500 | Pause between attempts, milliseconds |
| Confirm first | true | Ask for confirmation before closing |
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: Volume · MT5 Script · MQL5 Source Code