Advertisement IC Markets

Order-send retries and the duplicate position problem

Share, develop, and backtest custom MQL4/MQL5 Expert Advisors, Python data-scraping scripts, trading bots, and automated market alert systems.
Post Reply
Amanhasnoname
Posts: 1506
Joined: Fri Oct 30, 2026 9:45 am

Order-send retries and the duplicate position problem

Post by Amanhasnoname »

Scripts that retry when an order send returns an error can create the worst kind of bug: a double position. The mechanism is simple. The order goes out, the fill happens, but the confirmation is lost or delayed past the timeout. The script sees a failure, sends again, and now two positions exist where there should be one.

It is more common than it sounds on a slow connection or a busy server, and it hits scalpers harder because the logic fires often and timeouts are short.

Fixes that actually work:
- Tag each order with a unique comment or magic number tied to the signal, and check open positions and pending orders for that tag before any retry.
- Treat a timeout as unknown, not failed. Query state first, then decide.
- Cap retries at two or three, with a short pause, and refuse retries when the price has moved beyond what the signal tolerates.

Also log the raw error code each time. A requote, a timeout and a trade context busy message call for different responses, and lumping them together in a generic retry hides what is going wrong.

A script that has never been tested against a simulated lost confirmation has not really been tested. Block the reply on a demo and watch what happens.

What does your retry logic do on a timeout, and has it ever doubled a trade?
Recommended broker for automated trading & scalping IC Markets
Post Reply