Halve Every Position
There is a moment - a data release you forgot about, a spike that has gone far enough, a session you no longer want to hold through - when the right action is not to close and not to hold, but to take half off. Doing that by hand across eight positions takes long enough that the reason for doing it has usually passed. This script is one keystroke. What makes it worth downloading rather than writing in ten minutes is that halving a position is arithmetic with two constraints on it, and the ten-minute version checks neither. Half is almost never a whole number of lot steps. A 0.03 lot position halves to 0.015, which no server on a 0.01 step will accept. It has to be rounded, and the direction is not a matter of taste. This script rounds DOWN: it closes 0.01 and leaves 0.02. You could argue that for a de-risking tool the safer error is to close too much, and the argument is not silly - but a tool that sometimes removes more exposure than you told it to is a tool you cannot reason about, and the residue here is one lot step, which a second run takes off. Never more than you asked for is a rule worth keeping even when breaking it would be convenient. The second constraint is the half that stays open, and it is the one that breaks otherwise-correct code. Both legs of a partial close have to satisfy the broker's minimum volume, not just the leg being closed. On a broker with a 0.10 minimum - normal on indices and metals - a 0.15 lot position cannot be halved at all, because 0.10 plus 0.10 is already more than it holds. Neither can a 0.01 lot position on a standard forex account. Ask the server anyway and it answers with error 131, invalid trade volume, and leaves the position at full size. You pressed the de-risk button and nothing happened. This script works that out before it sends anything. A position it cannot halve is skipped and the reason is printed - which constraint, which number, which position. It does not decide for you what should happen instead. If you genuinely want those positions closed in full there is a switch for it, defaulted to off, because a de-risk script that silently liquidates the small positions is a script that will surprise someone at the worst possible time. Underneath is the floating point problem that makes lot arithmetic quietly wrong. In binary, 0.1 / 0.01 evaluates to 9.99999999999999998, not 10. Floor it and a 0.10 slice becomes 0.09. The code looks obviously right, which is exactly why the bug survives. Every division onto the lot grid here is nudged by an epsilon first. Everything else is ordinary care: tickets collected before the first close so the pool cannot re-index underneath the loop, every result checked, requotes and off-quotes retried while permanent refusals are not, slippage entered in pips and converted to points for 3 and 5 digit quotes, and a summary that separates what was halved from what was skipped and what the broker refused. Notes and limitations - This reduces exposure. It does not manage it. Stops and targets on the remaining half 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. - A position at or below twice the broker minimum cannot be partially closed by anyone. That is a server rule, not a limitation of this code. - An odd number of lot steps never splits evenly. 0.03 becomes 0.01 closed and 0.02 left, and the log says so explicitly rather than letting you wonder. - 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. - Commission and swap settle on the closed half only, which fragments the trade history. That matters if you feed the history into an analyser. - FIFO brokers may refuse to close part of a newer position while an older one on the same symbol is open. The refusal is reported per position and cannot be worked around from a script. - Halving a hedged pair on an MQL4 account halves both sides, which leaves the net exposure unchanged and only reduces the financing cost. If that is not what you meant, use the symbol or magic filter.
Script · MT5 · v1.0 · Free
About Halve Every Position
Halve Every Position is an expert advisor (trading robot) for MetaTrader 5. It has 7 settings, such as Current symbol only, Magic filter and Close whole if tiny. MQL source code included.
Files in the download: HalveAllPositions.ex5, HalveAllPositions.mq5, HalveAllPositions.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 | Magic number (-1 takes every magic) |
| Close whole if tiny | false | Position too small to halve: 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 · Trading Sessions · MT5 Script · MQL5 Source Code