Page 1 of 1

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

Posted: Thu Oct 08, 2026 1:38 am
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.

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

Posted: Thu Oct 08, 2026 2:11 am
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.

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

Posted: Thu Oct 08, 2026 2:28 am
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.