Partial Close Helper
Scaling out of a position sounds like one division: close 60% of 0.03 lots is 0.018. It is not a volume any broker accepts. Round it to the step and the question becomes what happens to the other side - a position of 0.03 that closes 0.02 leaves 0.01, which is fine on a 0.01 step, but the same position on a coarser 0.1 step leaves a fragment the server will reject as its own trade later. Most partial-close code rounds the close side and never checks what that decision did to the remainder, which is how positions end up with a leftover that cannot be closed cleanly. This header rounds the close amount to the lot step, computes the remainder from that already-rounded number, and then classifies the result: PC_OK means both the close volume and the remainder are valid on their own. Send OrderClose for the close volume and the remainder stays open. PC_CLOSE_WHOLE_INSTEAD means the remainder would be a real but sub-minimum fragment. There is no valid volume for it, so the only clean outcome is closing the entire position now instead of leaving an amount the server will not let you touch again. PC_TOO_SMALL_TO_SPLIT means the requested close side itself rounds to zero. There is nothing meaningful to close - the caller should skip the action entirely rather than send a zero-volume close. The header never decides for you which of these to prefer; it only tells you which situation you are in. A scale-out strategy might treat CLOSE_WHOLE_INSTEAD as "take full profit early", while a risk-reduction tool might treat it as "skip this cycle and wait for the position to grow" - that is a strategy decision, not a rounding one. Division throughout is nudged by an epsilon before rounding, the same convention used across this catalogue, because 0.1 / 0.01 does not evaluate to 10 in binary floating point. Notes and limitations - Rounding on the close side uses nearest-step (MathRound), not floor. A partial-close amount is a stated preference, not a risk ceiling, so the closest achievable volume is the right target. LotNormalize.mqh elsewhere in this catalogue covers the risk-budget case, where floor is mandatory. - The header only computes volumes - it never selects an order/position or calls a close function. Wire the result into your own close call, including your own retry and requote handling. - The broker's volume step and minimum are read fresh on every call rather than cached, so behaviour stays correct if a broker changes constraints during a session; call it once per decision, not in a tight loop, if that matters for performance. - Ties in MathRound (a value exactly halfway between two steps) round to the larger step, the platform's default rounding behaviour on both MQL4 and MQL5 - this is documented, not hidden, in case a caller needs the opposite convention.
Script · MT4 · v1.0 · Free
About Partial Close Helper
Partial Close Helper is a script for MetaTrader 4. It has 2 settings, such as Position volume and Close fraction. MQL source code included.
Files in the download: PartialClose.mqh, PartialClose.txt, PartialCloseDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Position volume | 0.07 | Position size for the worked "close by fraction" example |
| Close fraction | 0.60 | Fraction requested closed in the worked example |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Volume · MT4 Script · MQL4 Source Code