Advertisement IC Markets

The partial close bug that kept closing my position until nothing was left

Share, develop, and backtest custom MQL4/MQL5 Expert Advisors, Python data-scraping scripts, trading bots, and automated market alert systems.
Post Reply
Fairman
Posts: 6955
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

The partial close bug that kept closing my position until nothing was left

Post by Fairman »

The partial close bug that kept closing my position until nothing was left

coded a partial close into my EA: when price reaches a first target, close half and move the stop to breakeven. It worked in testing for most of the trades, but on a live demo I found a position that shrank over a couple of minutes until it was gone.

The cause was simple. My condition was "if price is above the first target, close half". After the first partial, the trade was still above the target, so on the next tick the condition was true again and it closed half of what remained. Then again. It kept going until it hit the minimum lot size.

The fix is to keep track of whether the partial has been done for that specific position. I used the position's ticket and a flag stored in a global variable, so it can only happen once. Another way is to check whether the current volume is still equal to the original volume.

It was a good reminder to test the logic on a demo with a long time frame and look at the journal, not just the equity curve. Edge cases in the order management are where EAs go wrong.
It’s Fairman :geek:
Recommended broker for automated trading & scalping IC Markets
Fairman
Posts: 6955
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Re: The partial close bug that kept closing my position until nothing was left

Post by Fairman »

A small addition on the fix. Storing the flag in a global variable works, but if the terminal restarts, you need to make sure it reads them back correctly, otherwise the EA can forget which trades already had their partial. Checking the current volume against the original volume avoids that problem, because the position itself carries the information. I now use the volume check as the main condition and the flag only as a backup. It is a small change but it made the EA much more reliable after restarts and disconnects.
It’s Fairman :geek:
Fairman
Posts: 6955
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Re: The partial close bug that kept closing my position until nothing was left

Post by Fairman »

This bug made me write down all the trade management states for each position: entered, partial done, stop at breakeven, trailing. The EA now moves through those in order and cannot go back to an earlier step. Before that, every rule was a separate check that could fire at any time, and they sometimes interfered with each other. Laying it out as steps made the logic much easier to read and test, and I have not had a repeat since.
It’s Fairman :geek:
Shadow Trader
Posts: 5594
Joined: Wed Sep 09, 2026 5:19 pm

Re: The partial close bug that kept closing my position until nothing was left

Post by Shadow Trader »

On MT4 the ticket based flag has a hole that MT5 does not. When MT4 closes part of an order, the remaining lots get a new ticket with a comment like from #12345, so a flag keyed to the old ticket is missing and the partial can fire again.
img_13541_r1.png
img_13541_r1.png (59.21 KiB) Viewed 231 times
The rounding of the half also decides how the bug ends. Say the order opens at 0.80 lots and each half is rounded down to the 0.01 step but never below the minimum lot, then the closes go 0.40, 0.20, 0.10, 0.05, 0.02, 0.01, 0.01 and 0.01, and the position is gone after eight closes, as in the chart (illustration).

If it rounds to zero instead and has no clamp, the last 0.01 stays open and the EA sends an invalid volume request on every tick. That costs nothing directly, but it floods the journal and some brokers dislike a stream of rejected requests.

The volume check needs the original volume stored somewhere that survives a restart. On MT5 the position keeps its ticket, so a history lookup by position ID recovers it.

Did your EA ever see the first partial rejected, say by a requote? A once only flag set before the result arrives means it is never retried.
FXS Prop Desk
Posts: 1689
Joined: Wed Oct 07, 2026 9:52 am

Re: The partial close bug that kept closing my position until nothing was left

Post by FXS Prop Desk »

The usual fix is to store a flag that records that the partial close has already happened, either in a global variable or by checking the current volume against the original volume.

Using the order comment is another option, but some brokers change or remove comments on partial closes, so a check based on volume is usually more reliable. It is also worth testing the logic on a live demo, as you did, because the strategy tester may not show the problem.
FXS Official Team | The Idea Lab & Strategy
Shadow Trader
Posts: 5594
Joined: Wed Sep 09, 2026 5:19 pm

Re: The partial close bug that kept closing my position until nothing was left

Post by Shadow Trader »

On an MT5 netting account the volume check has its own trap. If the EA scales in, say 0.50 lots and then another 0.30 on a pullback, the result is one 0.80 lot position and the stored original volume no longer matches anything.

The partial logic then compares 0.80 with 0.50, decides the partial already happened, and never closes anything. The fix is to update the stored reference every time the EA adds, so it holds the volume after the last entry rather than the first.

Netting also merges trades from different EAs on the same symbol, since magic numbers do not keep them apart there. Two EAs on EURUSD in one netting account will each see the other's volume changes and can trip each other's partial logic.

The tester mode decides whether any of this shows up before going live. In open prices only mode the condition is checked once per bar, so on M15 a runaway partial fires every 15 minutes instead of every tick.

Eight closes spread over two hours can then look like a deliberate scale out in the report. Every tick based on real ticks reproduces the live behaviour much more closely, and for order management code it is the mode worth waiting for.
Shadow Trader
Posts: 5594
Joined: Wed Sep 09, 2026 5:19 pm

Re: The partial close bug that kept closing my position until nothing was left

Post by Shadow Trader »

Here's a way to see partials without the hype. Closing half at a first target and leaving half for a bigger one is the same as running two separate trades, each with its own target and the same stop.

Example, made-up numbers, spread ignored. Stop is 4 pips away. Target one is +3, target two is +6. Say price reaches +3 before the stop 60% of the time and reaches +6 before the stop 40% of the time.

Trade A (target 3, stop 4): 0.6 x 3 minus 0.4 x 4 = +0.2 pips.

Trade B (target 6, stop 4): 0.4 x 6 minus 0.6 x 4 = 0 pips.

Half of each gives +0.1 pips per unit of position. Tiny. Notice what the partial did. It didn't create edge, it blended two expectancies. If both legs are negative, the blend is negative too.

What it does change is the shape of the equity curve. More trades finish green and the swings smooth out. That's useful if smoother results keep a trader following the plan.

Move the stop to breakeven after the partial and the maths shifts again, because the second leg now has a different stop. That's a separate calculation, and a lot of partial-profit advice skips it. Some accounts also charge per ticket or have minimum commissions, so splitting can add cost.

Do you take the partial at a fixed pip distance or at a structure level, and has it changed your average winner?
Post Reply