Source code
Partial close in MQL5: reduce a position exactly once
Closing part of a position can leave a smaller remainder to follow another exit rule. The interesting implementation challenge is remembering that the reduction already happened, especially when the EA receives another tick or restarts.
Check the account model
The CTrade PositionClosePartial method is documented for hedging accounts. Prefer the intended position ticket when several positions share a symbol. A successful method return is not the final server execution result; inspect the trade return code and resulting position.
Work through the remainder
For an illustrative 0.05-lot position, closing 0.02 leaves 0.03. Both quantities must respect the symbol volume rules. A percentage calculation can produce a quantity below the minimum or an invalid remainder, so define the rounding and skip behaviour in advance.
Test the once-only rule
Create a demo position, trigger the scale-out condition, and let several more ticks arrive. The remainder should not be repeatedly halved. Restart the EA and repeat the observation. Reconstruct completion from recorded trades or durable state instead of relying only on a temporary boolean.
Ask AI for a narrow change
Specify the account mode, ticket ownership, trigger, fraction and residual-volume policy. Request a before-and-after log showing requested volume, executed reduction and remaining volume. This turns a vague instruction such as take some profit into a reviewable behaviour.
CTrade partial close reference
Back to top












