The order of operations bug between my breakeven and trailing logic
Posted: Thu Oct 08, 2026 12:11 pm
The order of operations bug between my breakeven and trailing logic
I had two features in my EA, a breakeven move after a certain profit and a trailing stop that starts later. They looked fine separately and then I found trades where the stop did odd things, jumping back and forth.
It was an ordering problem. On each tick the EA ran the breakeven check first, which put the stop at entry, and then the trailing check, which had its own idea of where the stop should be and sometimes put it further back. Next tick, the breakeven logic saw the stop was not at entry and moved it again. They were fighting each other.
The fix was to make the stop only ever move in one direction. Before any modification, the code checks if the new stop is better than the current one, meaning closer to profit for the trade direction. If not, it does nothing. I also moved both features into a single function so there's one place that decides the stop each tick.
It's a simple principle but easy to overlook: one owner for each piece of state. Having two bits of logic both editing the same stop is asking for trouble.
I had two features in my EA, a breakeven move after a certain profit and a trailing stop that starts later. They looked fine separately and then I found trades where the stop did odd things, jumping back and forth.
It was an ordering problem. On each tick the EA ran the breakeven check first, which put the stop at entry, and then the trailing check, which had its own idea of where the stop should be and sometimes put it further back. Next tick, the breakeven logic saw the stop was not at entry and moved it again. They were fighting each other.
The fix was to make the stop only ever move in one direction. Before any modification, the code checks if the new stop is better than the current one, meaning closer to profit for the trade direction. If not, it does nothing. I also moved both features into a single function so there's one place that decides the stop each tick.
It's a simple principle but easy to overlook: one owner for each piece of state. Having two bits of logic both editing the same stop is asking for trouble.