Advertisement IC Markets

How to handle execution latency and broker slippage?

Share, develop, and backtest custom MQL4/MQL5 Expert Advisors, Python data-scraping scripts, trading bots, and automated market alert systems.
PTScalper
Site Admin
Posts: 7821
Joined: Mon Jul 20, 2026 1:28 pm

Re: How to handle execution latency and broker slippage?

Post by PTScalper »

MQL5 Implementation: Asynchronous Execution Class

Here is a production-grade asynchronous trade executor class (CAsyncTradeExecutor) that dispatches orders without blocking and tracks them via request IDs and trade transactions:

Code: Select all

//+------------------------------------------------------------------+
//|                                          CAsyncTradeExecutor.mqh |
//|                                  Copyright 2026, Quant Systems   |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026"
#property link      "https://www.mql5.com"
#property version   "1.00"

class CAsyncTradeExecutor
private:
   // Dictionary or tracking buffer for active asynchronous request IDs
   ulong   m_tracked_requests[];

   void AddTrackedRequest(ulong req_id)
   {
      int size = ArraySize(m_tracked_requests);
      ArrayResize(m_tracked_requests, size + 1);
      m_tracked_requests[size] = req_id;
   }

   void RemoveTrackedRequest(ulong req_id)
   {
      int size = ArraySize(m_tracked_requests);
      for(int i = 0; i < size; i++)
      {
         if(m_tracked_requests[i] == req_id)
         {
            // Shift array elements down
            for(int j = i; j < size - 1; j++)
               m_tracked_requests[j] = m_tracked_requests[j + 1];
            ArrayResize(m_tracked_requests, size - 1);
            break;
         }
      }
   }

public:
   CAsyncTradeExecutor()
   {
      ArrayFree(m_tracked_requests);
   }

   ~CAsyncTradeExecutor() {}

   // 1. Non-Blocking Order Dispatch
   bool SendAsync(MqlTradeRequest &request, MqlTradeResult &result)
   {
      // OrderSendAsync places the packet in the terminal queue and returns immediately
      bool dispatched = OrderSendAsync(request, result);
      
      if(dispatched)
      {
         // Track the request ID to correlate with OnTradeTransaction callbacks
         AddTrackedRequest(result.request_id);
         Print("[Async Dispatch] Request queued successfully. Request ID: ", result.request_id, " Symbol: ", request.symbol);
         return true;
      }
      else
      {
         Print("[Async Error] OrderSendAsync failed to queue. System Error: ", GetLastError());
         return false;
      }
   }

   // 2. Asynchronous Event Handler (Must be called from EA's OnTradeTransaction)
   void HandleTransaction(const MqlTradeTransaction& trans,
                          const MqlTradeRequest& request,
                          const MqlTradeResult& result)
   {
      // Filter transactions related to our dispatched requests
      if(trans.type == TRADE_TRANSACTION_DEAL_ADD)
      {
         Print("[Transaction] Deal successfully added! Deal Ticket: ", trans.deal, 
               " Symbol: ", trans.symbol, " Price: ", trans.price, " Volume: ", trans.volume);
      }
      else if(trans.type == TRADE_TRANSACTION_REQUEST)
      {
         // Request processing completed by terminal/broker
         Print("[Transaction] Request processed. Retcode: ", result.retcode, 
               " Description: ", ResultRetcodeDescription(result.retcode));
         
         // Clean up tracking buffer
         RemoveTrackedRequest(result.request_id);
      }
   }
};
Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.
Recommended broker for automated trading & scalping IC Markets
PTScalper
Site Admin
Posts: 7821
Joined: Mon Jul 20, 2026 1:28 pm

Re: How to handle execution latency and broker slippage?

Post by PTScalper »

Integrating Into Your Expert Advisor (.mq5)

To hook the asynchronous executor into your main EA structure, wire HandleTransaction directly into the terminal's global OnTradeTransaction event listener:

Code: Select all

#include "CAsyncTradeExecutor.mqh"

CAsyncTradeExecutor *async_trader;

int OnInit()
{
   async_trader = new CAsyncTradeExecutor();
   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)
{
   delete async_trader;
}

// Global MQL5 Event Hook
void OnTradeTransaction(const MqlTradeTransaction& trans,
                        const MqlTradeRequest& request,
                        const MqlTradeResult& result)
{
   // Pass incoming transaction feedback to our async executor
   async_trader.HandleTransaction(trans, request, result);
}

void OnTick()
{
   // Example: Triggering an asynchronous buy order without freezing the tick thread
   MqlTradeRequest request;
   MqlTradeResult  result;
   ZeroMemory(request);
   ZeroMemory(result);

   request.action   = TRADE_ACTION_DEAL;
   request.symbol   = _Symbol;
   request.volume   = 0.1;
   request.type     = ORDER_TYPE_BUY;
   request.price    = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
   request.deviation= 10;
   request.magic    = 123456;
   request.comment  = "Async Scalper Entry";
   request.type_filling = ORDER_FILLING_IOC;

   // Non-blocking dispatch
   if(async_trader.SendAsync(request, result))
   {
      // Thread continues immediately without waiting for server network ACK
   }
}
Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.
PTScalper
Site Admin
Posts: 7821
Joined: Mon Jul 20, 2026 1:28 pm

Re: How to handle execution latency and broker slippage?

Post by PTScalper »

Key Engineering Considerations

Race Conditions in Multi-Symbol EAs: Because OnTradeTransaction is asynchronous, your transaction handler may fire milliseconds or seconds later while your EA is already processing ticks for a completely different symbol. Ensure your data structures and position-state variables are thread-safe or properly locked/synchronized.

Request ID Correlation: Always capture result.request_id upon dispatch. When OnTradeTransaction triggers TRADE_TRANSACTION_REQUEST, match the incoming ID against your internal state machine to verify whether your limit order was successfully placed or rejected.

Queue Overflows: Avoid flooding OrderSendAsync() in a tight loop without rate-limiting; if you push hundreds of requests per second faster than the terminal's IPC queue can process them, the terminal will return trade queue congestion errors.
Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.
Shadow Trader
Posts: 5594
Joined: Wed Sep 09, 2026 5:19 pm

Re: How to handle execution latency and broker slippage?

Post by Shadow Trader »

Good topic, and it's one of those costs that hides in plain sight because each individual instance looks tiny. Half a pip of slippage doesn't feel like much until you multiply it by a few hundred trades a month on a strategy whose average winner is only four or five pips.

One practical point to add for people who don't code EAs: you can measure your own slippage without any special tools. Most platforms record the requested price and the filled price, or at least you can note the price on the screen when you click and compare it with the fill in the history. Do that for a few weeks and split it by time of day. It's common to find that slippage is close to zero in the middle of the London session and noticeably worse around the open, the close and news, which is useful information in itself because it tells you which hours your edge is actually paying for.

The other thing worth checking is whether slippage is symmetrical. On a fair execution it should sometimes go in your favour as well. If every single deviation is against you, that's a question to ask the broker, and it matters more than the advertised spread.

For backtesting, adding a fixed slippage figure per trade is a decent start, but a better approach is to add it as a random value within a range that matches what you've actually measured, and to make it larger in the minutes around scheduled news. If the strategy's results collapse with a realistic slippage model, it's better to find that out in the tester than on a live account.
Post Reply