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.
The order of operations bug between my breakeven and trailing logic
Re: The order of operations bug between my breakeven and trailing logic
The one direction rule is the right fix. I had a similar problem and ended up writing a single function that receives every proposed stop from any feature, picks the best one for the trade direction, and only then sends the modification. Nothing else in the code is allowed to change the stop. That way I can add new stop features later without them fighting each other. It also cut the number of modification requests a lot, which some brokers do not like when there are too many.
It’s Fairman 
Re: The order of operations bug between my breakeven and trailing logic
Worth adding a minimum step for modifications too. If the trailing logic wants to move the stop by a fraction of a pip every tick, it sends a lot of requests that achieve almost nothing and can hit broker limits. I only modify if the new stop is better by at least a set amount. I also check the broker's stop level before sending, because a stop too close to the current price gets rejected and the EA should handle that cleanly.
It’s Fairman 
Re: The order of operations bug between my breakeven and trailing logic
Testing this kind of thing in the visual tester was what helped me find it. Watching the stop line move on each tick made the fight between the two features obvious in a way the trade log never showed. I now run a short visual test any time I change stop logic, just a few trades, before running the full backtest. It catches the silly ordering problems quickly, and it is much easier than trying to read hundreds of modification lines in the journal.
It’s Fairman 
-
FXS Prop Desk
- Posts: 1689
- Joined: Wed Oct 07, 2026 9:52 am
Re: The order of operations bug between my breakeven and trailing logic
The jumping stop is a typical sign of two functions both trying to own the stop. One fix is a single stop manager function that calculates the breakeven level and the trailing level, then applies whichever is better for the position, and never moves the stop backwards.
Adding a check that the new stop is at least a minimum step away from the current one also cuts unnecessary modifications, which brokers appreciate.
Adding a check that the new stop is at least a minimum step away from the current one also cuts unnecessary modifications, which brokers appreciate.
FXS Official Team | The Idea Lab & Strategy
-
Shadow Trader
- Posts: 5594
- Joined: Wed Sep 09, 2026 5:19 pm
Re: The order of operations bug between my breakeven and trailing logic
The one direction check has a classic trap on sell trades. For a buy, better means higher, and a missing stop reads as zero, so any real stop beats it.
For a sell, better means lower, and zero is lower than every price. If the code compares the new stop against a current value of zero, no stop ever looks better, so a sell opened without a stop never gets one from the manager.
The fix is to treat zero as no stop at all and accept the first valid level. It is worth testing a sell with no initial stop on purpose, because that path is easy to skip in the visual tester.
The second trap is comparing prices as raw doubles. Two levels that both print as 1.08500 can differ in the last digits, so the manager thinks the stop improved and sends a modification that changes nothing.
Rounding both values to the symbol digits before comparing, together with the minimum step mentioned above, removes those phantom requests. Did anyone else see the modification count drop once the rounding went in?
For a sell, better means lower, and zero is lower than every price. If the code compares the new stop against a current value of zero, no stop ever looks better, so a sell opened without a stop never gets one from the manager.
The fix is to treat zero as no stop at all and accept the first valid level. It is worth testing a sell with no initial stop on purpose, because that path is easy to skip in the visual tester.
The second trap is comparing prices as raw doubles. Two levels that both print as 1.08500 can differ in the last digits, so the manager thinks the stop improved and sends a modification that changes nothing.
Rounding both values to the symbol digits before comparing, together with the minimum step mentioned above, removes those phantom requests. Did anyone else see the modification count drop once the rounding went in?
-
Shadow Trader
- Posts: 5594
- Joined: Wed Sep 09, 2026 5:19 pm
Re: The order of operations bug between my breakeven and trailing logic
Fast strategies that move stops frequently, trailing every few ticks, can run into limits. Brokers and platforms restrict the number of requests per second or per minute, to protect their systems.
Hitting the limit produces errors, delayed modifications or, in some cases, temporary blocks. A trailing stop system that sends a modification on every tick can find itself throttled in the middle of a fast move, exactly when it's needed.
Some brokers also impose minimum intervals between modifications of the same order, or limit the total number of open orders.
The remedy lies in design. Update the stop only when it needs to move by a meaningful amount, such as at least a pip or a fraction of ATR, not on every tick. Batch updates, and handle rejections gracefully with retry logic and logging.
The broker's documentation, or a question to support, should reveal the specific limits. They're often not advertised prominently.
One workaround for trailing logic is using a minimum step, such as moving the stop only after price has advanced at least one pip beyond the last update. Another is time-based updating, such as once per second at most. Both cut the request count dramatically while keeping the stop reasonably tight.
A log of modification attempts and their outcomes will show how close the system is to any limit. If rejections appear, it's already too close.
Has an automated strategy ever been throttled by request limits, and how was the logic adjusted?
Hitting the limit produces errors, delayed modifications or, in some cases, temporary blocks. A trailing stop system that sends a modification on every tick can find itself throttled in the middle of a fast move, exactly when it's needed.
Some brokers also impose minimum intervals between modifications of the same order, or limit the total number of open orders.
The remedy lies in design. Update the stop only when it needs to move by a meaningful amount, such as at least a pip or a fraction of ATR, not on every tick. Batch updates, and handle rejections gracefully with retry logic and logging.
The broker's documentation, or a question to support, should reveal the specific limits. They're often not advertised prominently.
One workaround for trailing logic is using a minimum step, such as moving the stop only after price has advanced at least one pip beyond the last update. Another is time-based updating, such as once per second at most. Both cut the request count dramatically while keeping the stop reasonably tight.
A log of modification attempts and their outcomes will show how close the system is to any limit. If rejections appear, it's already too close.
Has an automated strategy ever been throttled by request limits, and how was the logic adjusted?