Retry logic for order sends, handling requotes and busy servers without doubling up
sometimes an order send fails because of a requote, a busy server or a brief disconnect. My first version of the EA either gave up on the first failure, so it missed good entries, or it retried blindly, which once led to two positions.
What I do now is check the error code and handle each type separately. For price changed or requote errors, it checks that the setup is still valid and the price hasn't moved too far, and tries again once or twice with a short pause. For a busy server or timeout, it first checks whether the order actually went through, by looking at the open positions and pending orders for that magic number, before retrying. That check is what prevents duplicates.
For errors that mean something is wrong, like not enough margin or invalid stops, it doesn't retry. It logs it and moves on. I also set a maximum number of retries and a maximum time window, since an entry that's a minute late isn't the same trade.
It's not exciting code but it's the part that keeps the EA from doing something silly in poor conditions.
Retry logic for order sends, handling requotes and busy servers without doubling up
Re: Retry logic for order sends, handling requotes and busy servers without doubling up
Something that helped with the duplicate problem was giving every order a unique comment built from the signal time and symbol. Before retrying, the EA searches its trades and any working orders for that exact comment, not just the magic number. If it finds one, it assumes the first attempt went through and stops. The magic number alone was not enough when two signals happened close together on the same pair, because both orders looked the same. It sounds fussy, but it has stopped every double entry since.
It’s Fairman 
Re: Retry logic for order sends, handling requotes and busy servers without doubling up
On the maximum time window, I set mine to around thirty seconds for M1 entries. After that, the price has usually moved enough that the original stop and target no longer make sense, so the EA abandons the signal and logs it. I would rather lose a trade than enter late with a stop in the wrong place. For M5 entries I allow a bit longer, but the same idea applies. A late entry is a different trade from the one the rules described.
It’s Fairman 
-
Shadow Trader
- Posts: 5594
- Joined: Wed Sep 09, 2026 5:19 pm
Re: Retry logic for order sends, handling requotes and busy servers without doubling up
On MT5, one trap with the busy server case is timing. After a timeout such as retcode 10012, the position may not show in the open positions list straight away, so a check made a few milliseconds later can see nothing and allow a second send.
Waiting around 500 milliseconds and checking the order history as well as open positions closes most of that gap. The history check also catches an order that filled and was stopped out very quickly, which a positions scan alone would miss.
The comment approach above has one weakness. Some servers change or cut comments, the field only holds 31 characters, and a stop or target hit can append text, so matching on the exact comment can fail at the worst moment.
A sturdier key is a counter held in a global variable and written into both the comment and a local file before the send. If the comment comes back altered, the file still says whether attempt one went out.
On requotes, do you keep the stop at the original structural level and resize the lot, or shift the stop with the new fill? Keeping the level and resizing keeps the money at risk the same, which seems safer than carrying the old distance to a new price.
Waiting around 500 milliseconds and checking the order history as well as open positions closes most of that gap. The history check also catches an order that filled and was stopped out very quickly, which a positions scan alone would miss.
The comment approach above has one weakness. Some servers change or cut comments, the field only holds 31 characters, and a stop or target hit can append text, so matching on the exact comment can fail at the worst moment.
A sturdier key is a counter held in a global variable and written into both the comment and a local file before the send. If the comment comes back altered, the file still says whether attempt one went out.
On requotes, do you keep the stop at the original structural level and resize the lot, or shift the stop with the new fill? Keeping the level and resizing keeps the money at risk the same, which seems safer than carrying the old distance to a new price.
-
Shadow Trader
- Posts: 5594
- Joined: Wed Sep 09, 2026 5:19 pm
Re: Retry logic for order sends, handling requotes and busy servers without doubling up
The price side of the retry loop needs a cap on total drift, not just on each attempt. If every retry accepts up to 0.5 pips of deviation, three retries can finish 1.5 pips away from the price the signal was built on.
On a 6 pip stop that is a quarter of the planned risk gone before the trade starts, while each individual attempt looked within limits. Checking the new price against the original signal price, and abandoning once the gap passes something like 0.6 pips, keeps the trade close to the one the rules described.
Spacing the retries matters on busy servers. Doubling the pause each time, such as 250, 500 and then 1,000 milliseconds, gives the server room to recover instead of adding to the queue that caused the error.
On MT4 with several EAs on one terminal, error 146 for a busy trade context is a separate case. A shared lock that one EA sets before sending and clears afterwards stops them tripping over each other and saves retries altogether.
Does anyone log the attempt number on the fill line? Comparing slippage on first attempts with slippage on retries would show whether the retry path is quietly costing more than the missed entries it saves.
On a 6 pip stop that is a quarter of the planned risk gone before the trade starts, while each individual attempt looked within limits. Checking the new price against the original signal price, and abandoning once the gap passes something like 0.6 pips, keeps the trade close to the one the rules described.
Spacing the retries matters on busy servers. Doubling the pause each time, such as 250, 500 and then 1,000 milliseconds, gives the server room to recover instead of adding to the queue that caused the error.
On MT4 with several EAs on one terminal, error 146 for a busy trade context is a separate case. A shared lock that one EA sets before sending and clears afterwards stops them tripping over each other and saves retries altogether.
Does anyone log the attempt number on the fill line? Comparing slippage on first attempts with slippage on retries would show whether the retry path is quietly costing more than the missed entries it saves.
-
FXS Prop Desk
- Posts: 1689
- Joined: Wed Oct 07, 2026 9:52 am
Re: Retry logic for order sends, handling requotes and busy servers without doubling up
The blind retry that opened two positions is a common problem, usually because the first order was accepted by the server but the confirmation did not arrive.
A useful safety check before each retry is to look at open positions and pending orders for the symbol and magic number. If a position already exists, the retry is skipped. It adds a few lines of code but removes the risk of doubling up.
A useful safety check before each retry is to look at open positions and pending orders for the symbol and magic number. If a position already exists, the retry is skipped. It adds a few lines of code but removes the risk of doubling up.
FXS Official Team | The Idea Lab & Strategy
-
Shadow Trader
- Posts: 5594
- Joined: Wed Sep 09, 2026 5:19 pm
Re: Retry logic for order sends, handling requotes and busy servers without doubling up
Periods of extreme activity place heavy loads on trading infrastructure. Platform servers, price feeds and web interfaces can slow down, drop connections or reject orders exactly when volatility makes timely action most valuable. Message volume spikes in a news shock, and systems built for typical demand may queue or time out requests.
An outage creates an asymmetry. Stops and limits held on the server may still execute, but orders managed locally by software, such as trailing logic in a script or expert advisor, stop working when the connection drops. Pending modifications may be lost, and a position can stay open without the protection that was assumed.
Recovery has its own hazards. When service returns, queued orders may be processed in an unexpected sequence, and the first prices received can sit well away from the last quote seen. Chart history may also contain gaps that distort indicators.
A written contingency plan helps: a secondary connection such as a mobile app on a separate network, the dealing desk contact details saved offline, and server-side protective orders rather than client-side ones. Reducing exposure before scheduled high-impact events lowers dependence on infrastructure holding up under strain.
An outage creates an asymmetry. Stops and limits held on the server may still execute, but orders managed locally by software, such as trailing logic in a script or expert advisor, stop working when the connection drops. Pending modifications may be lost, and a position can stay open without the protection that was assumed.
Recovery has its own hazards. When service returns, queued orders may be processed in an unexpected sequence, and the first prices received can sit well away from the last quote seen. Chart history may also contain gaps that distort indicators.
A written contingency plan helps: a secondary connection such as a mobile app on a separate network, the dealing desk contact details saved offline, and server-side protective orders rather than client-side ones. Reducing exposure before scheduled high-impact events lowers dependence on infrastructure holding up under strain.