Partial Close + Trailing Stop: Where EAs Break
Contents (5)
Contents
Bottom Line: What Breaks Is the "State Handoff," Not the Logic
Neither partial closing nor trailing stops is a difficult operation on its own. Problems appear when you apply both to the same position. In our own development experience, the issues clustered in almost exactly three places:
- Remaining-volume tracking: the EA cannot remember whether it has "already partially closed"
- Lot step: the close volume does not match the broker's minimum lot or step size
- SL update order: partial close, break-even move and trailing overwrite one another
All three rarely show up in backtests, and are the kind of bug you usually notice only in live trading.
Recommended Broker Comparison →
🎁 Download Free EA📧 Free 8-email course + subscriber-only pack of 13 MT5 indicators
* Privacy strictly protected. You can unsubscribe at any time.
Why Combining Them Breaks Things
You Can't Rely on the EA's "Memory" for Remaining Volume
In MT5, a partial close does not change the position ticket; only POSITION_VOLUME decreases. If you track "already partially closed" with just a bool variable, the value is lost on an EA restart, a VPS reconnection or a timeframe switch. The classic accident is that on the very next tick, the EA closes half of the remaining position a second time.
Our countermeasure is to use HistorySelectByPosition() to walk through that position's deal history and determine the partial-close stage by comparing the initial position volume with the current remaining volume. In-memory flags are kept only as a supplement.
Lots Should Be Rounded Down, Not Rounded Off
Half of 0.03 lots is 0.015. On an account with a 0.01 step, rounding to the nearest value closes 0.02, so you give up more than intended. The close volume must be rounded down using SYMBOL_VOLUME_STEP, and you also need to check whether the remaining volume after the close is at least SYMBOL_VOLUME_MIN. A 0.01-lot position can't be split at all, so unless you decide in the spec whether to "skip the partial close" or "close everything," the EA will keep returning errors.
Write the SL in One Place, Once
PositionModify() sets SL and TP at the same time. If the trailing routine passes 0 for TP, the TP is wiped out. Also, if the logic that moves the SL to break-even after a partial close and the logic that raises the SL via trailing run separately on the same tick, whichever writes last wins. As a result, an SL that was already raised to break-even can move back down.
The order we use is as follows:
- Send the partial close → re-select the position and confirm the remaining volume
- Calculate both candidates: the break-even SL and the trailing SL
- Move the SL only to "the side more favorable than the current SL" (the higher one for buys, the lower one for sells)
- After checking the stop level and freeze level, pass the current TP value back in and make a single modification
Implementation and Verification Checklist
| Item to check | Symptom when broken | How to check |
|---|---|---|
| Is the partial-close stage determined from deal history? | Double partial close after a restart | Re-attach the EA while it's running and watch the remaining volume |
| Is the close volume rounded down to the lot step? | Closes more than intended, or errors appear | Open a minimum-lot position and read the log |
| Have you decided how to handle splitting at minimum lot? | Close fails on every tick | Test with 0.01 lots |
| Can the SL never move back in an unfavorable direction? | Position closes at a loss after the break-even SL | Trace the SL over time in the log |
| Does an SL change wipe out the TP? | Take profit doesn't trigger | Check the TP field of the position after modification |
| Is the SL left untouched when the partial close fails? | Remaining volume and SL assumptions diverge | Place an order just before the market closes to force a failure |
| Is the EA running on a netting account? | Positions merge with other EAs' positions | Check the account type |
If you're the one using the EA, the quickest check is to run it on a demo account at minimum lot and see whether its behavior stays the same when you re-attach the EA mid-trade. The basics of setup are covered in EA Setup Guide.
Limitations and Cautions
The Strategy Tester fills orders instantly, and partial fills of an order almost never occur. For that reason, bugs around partial closing are barely visible in backtest numbers. Differences in execution method (filling mode) on live accounts and behavior on requotes can honestly only be checked broker by broker.
Adding partial closes doesn't automatically improve results, either. For trend-following strategies with a low win rate that rely on a few large winners to carry the whole result (for example, DONCHIAN ENGINE, with a backtest win rate of 34.5%), you would be cutting position size just when profits are running, which may lower the expected value. The equity swings get smaller, but what you give up in exchange can only be learned by testing each EA. We haven't been able to verify every combination ourselves either.
If you add partial closes yourself to a strategy like DONCHIAN ENGINE, keep in mind that the original validation results no longer apply as they were.
Summary
- What breaks when combining partial close and trailing is the "state handoff," more than the logic
- Determine the partial-close stage from deal history and remaining volume, not from an in-memory flag
- Round lots down to the lot step and check that the remainder doesn't fall below the minimum lot
- Consolidate SL changes in one place, move only in the favorable direction, and never wipe out the TP
- Because backtests hide these problems, run a re-attach test on a demo account
- Partial closes smooth out equity swings but can also lower expected value
Other implementation cautions are collected in EA & MT5 Know-How.
Related
2026-11-07
Stop Level and Freeze Level: Why Your SL Gets Rejected
2026-11-01
Why EAs That Trade on Closed Bars Don't Repaint
2026-10-29
MQL5 OnTick vs OnTimer: How to Use Each in EA Design
2026-10-02
XAUUSD EA Settings: Risk, MaxSpread, SL, Time Filter
📧 Free 8-email course + subscriber-only pack of 13 MT5 indicators
* Privacy strictly protected. You can unsubscribe at any time.
Have a question about EAs or FX?
You can post it on the Nattzy trading forum (free sign-up). Questions beyond this article are welcome too — EA settings, choosing a broker and more.
Ask on Nattzy ↗