DEV Community

Omais Ali
Omais Ali

Posted on

Which MQL5 OrderSend failures are safe to retry, and which are not

A lot of Expert Advisors handle a failed OrderSend in one of two bad ways. Either they give up on the first error and miss the trade, or they retry everything in a loop and turn a "not enough money" error into fifty log lines and a frozen chart.

The trade server tells you which case you are in through result.retcode. Here is how I sort them.

Retry with a fresh price

These mean the market moved or the quote went stale between your calculation and the server receiving the request. Refresh the price and try again, a small number of times.

  1. TRADE_RETCODE_REQUOTE (10004)
  2. TRADE_RETCODE_PRICE_CHANGED (10020)
  3. TRADE_RETCODE_PRICE_OFF (10021)

Retry after a short wait

These are about the connection or the server being busy, not your request.

  1. TRADE_RETCODE_TIMEOUT (10012), but check first whether the order actually went through
  2. TRADE_RETCODE_CONNECTION (10031)
  3. TRADE_RETCODE_TOO_MANY_REQUESTS (10024)

Never retry blindly

These mean the request itself is wrong or the account cannot take it. Retrying sends the same bad request again.

  1. TRADE_RETCODE_INVALID_VOLUME (10014)
  2. TRADE_RETCODE_INVALID_STOPS (10016)
  3. TRADE_RETCODE_NO_MONEY (10019)
  4. TRADE_RETCODE_MARKET_CLOSED (10018)
  5. TRADE_RETCODE_TRADE_DISABLED (10017)
  6. TRADE_RETCODE_INVALID_FILL (10030)

Log them clearly and stop for this signal.

Putting it together

bool SendWithRetry(MqlTradeRequest &req, MqlTradeResult &res, int maxTries = 3)
{
   for(int i = 0; i < maxTries; i++)
   {
      if(req.action == TRADE_ACTION_DEAL)
         req.price = (req.type == ORDER_TYPE_BUY)
                     ? SymbolInfoDouble(req.symbol, SYMBOL_ASK)
                     : SymbolInfoDouble(req.symbol, SYMBOL_BID);

      ZeroMemory(res);
      bool sent = OrderSend(req, res);

      if(sent && (res.retcode == TRADE_RETCODE_DONE ||
                  res.retcode == TRADE_RETCODE_PLACED ||
                  res.retcode == TRADE_RETCODE_DONE_PARTIAL))
         return true;

      switch(res.retcode)
      {
         case TRADE_RETCODE_REQUOTE:
         case TRADE_RETCODE_PRICE_CHANGED:
         case TRADE_RETCODE_PRICE_OFF:
            continue;                       // fresh price on next loop
         case TRADE_RETCODE_CONNECTION:
         case TRADE_RETCODE_TOO_MANY_REQUESTS:
            Sleep(500);
            continue;
         default:
            PrintFormat("OrderSend stopped: retcode %u %s",
                        res.retcode, res.comment);
            return false;                   // do not retry
      }
   }
   PrintFormat("OrderSend gave up after %d tries, last retcode %u",
               maxTries, res.retcode);
   return false;
}
Enter fullscreen mode Exit fullscreen mode

A few notes on this:

  1. Keep maxTries small. Three is plenty. If the market is moving that fast, your signal price is already wrong.
  2. Timeout is deliberately not retried here. After a timeout, look at open positions and recent deals for your magic number before sending again, or you can end up with two positions.
  3. Do not call Sleep inside the Strategy Tester loop if you can avoid it. It slows testing and does nothing useful there.
  4. Log the retcode as a number and the comment. The comment from the server often explains things the enum name does not.

Sorting failures this way makes an EA both more persistent when it should be and quieter when it should give up.

Top comments (0)