Advertisement IC Markets

Async order sending and what comes back

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

Async order sending and what comes back

Post by Fairman »

Async order sending and what comes back

In MetaTrader 5, an order can be sent synchronously, waiting for the server's answer, or asynchronously, returning immediately and delivering the result later through an event.

The synchronous call is simple. The program stops until the order is confirmed or rejected, which can take tens or hundreds of milliseconds. During that time, it can't react to anything else.

The asynchronous call frees the program to continue. It can send several orders in quick succession or handle price updates while waiting. The result arrives as a trade transaction event, which the code must handle.

The complication is state. After sending an async order, the program doesn't yet know whether it filled. If it assumes success and sends a second order based on that, errors compound.

Careful design tracks each order's status explicitly: pending, filled, rejected, partially filled. A state machine prevents duplicates and orphaned positions.

For strategies that need speed, async is the better tool. For simple ones, sync is safer.

The bug reports that come from async code are often about assumptions: the position wasn't there yet, the stop was set on a ticket that didn't exist.

Which order-sending mode is used in the automated systems, and what bugs came with it?
It’s Fairman :geek:
Recommended broker for automated trading & scalping IC Markets
FXS HongKong Scalper
Posts: 542
Joined: Sat Oct 24, 2026 10:15 pm

Re: Async order sending and what comes back

Post by FXS HongKong Scalper »

Fairman wrote: Async order sending and what comes back In MetaTrader 5, an order can be sent synchronously, waiting for the server's answer, or asynchronously, returning immediately and delivering the result later through an event.
Async sending is useful for EAs managing several symbols. The trade-off is that error handling becomes more complex, since a rejection arrives later through OnTradeTransaction. Without careful handling, an EA can think an order is live when it was rejected.
FXS Official Team | Hong Kong Session Desk
Amanhasnoname
Posts: 2060
Joined: Fri Oct 30, 2026 9:45 am

Re: Async order sending and what comes back

Post by Amanhasnoname »

The state problem is really the heart of it. With OrderSendAsync the return value only tells you the request left the terminal, not that anything happened at the server, so the EA needs its own record of what it has asked for and what it has actually received. A simple map keyed by request id, holding states like sent, accepted, filled, partially filled and rejected, stops the classic bug where the code fires a second entry because the first position hasn't appeared yet.

OnTradeTransaction is where most of the subtle issues live. Events can arrive in an order you don't expect, a deal can show up before the order update, and on a reconnect you can miss events entirely. Because of that it's safer to treat transactions as hints and periodically reconcile against PositionsTotal and the history, rather than trusting the event stream alone.

Things worth testing: a timeout that flags any request without a reply after a set interval, behaviour when the server rejects for invalid stops or requotes, and what happens if the terminal restarts mid-trade. Running those cases on a demo first catches most orphaned-position bugs before real money is involved.
Post Reply