Advertisement IC Markets

Retry logic for order sends, handling requotes and busy servers without doubling up

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

Retry logic for order sends, handling requotes and busy servers without doubling up

Post by Fairman »

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.
It’s Fairman :geek:
Recommended broker for automated trading & scalping IC Markets
Fairman
Posts: 3303
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Re: Retry logic for order sends, handling requotes and busy servers without doubling up

Post by Fairman »

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 :geek:
Fairman
Posts: 3303
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Re: Retry logic for order sends, handling requotes and busy servers without doubling up

Post by Fairman »

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 :geek:
Post Reply