Skip to content

EA Doctor rule · Trade requests

OrderSend() result not checked

OrderSend() is called as a plain statement, so a refused or requoted order goes unnoticed and the EA carries on as if it traded.

Why it matters

OrderSend() returns false when the request fails its basic checks, and the trade server's answer is in MqlTradeResult.retcode. Orders are refused for many ordinary reasons: a requote (10004), invalid stops (10016), not enough money (10019), a closed market (10018).

If neither the return value nor retcode is read, the EA behaves as if the position exists: it waits for a trade that was never opened, skips the next signal, or sends the same request again on every tick.

When the rule fires

OrderSend() or OrderSendAsync() in its MQL5 form (a request and a result) stands on its own as a statement, and the function it is in reads neither .retcode nor GetLastError() after the call.

When it stays quiet

The call is part of a condition or an assignment (if(!OrderSend(req, res)), bool ok = OrderSend(…)), or retcode or GetLastError() is read after it in the same function.

How to fix it

  1. Use the return value: when OrderSend() returns false, log GetLastError() and stop.
  2. Read result.retcode: TRADE_RETCODE_DONE (and TRADE_RETCODE_PLACED for a pending order) is a success, anything else is not.
  3. Decide per code: retry a requote or a changed price a limited number of times with a fresh price; stop on invalid volume, invalid stops or not enough money.

Code that fires it

MQL5 · fires this rule
void OpenBuy()
  {
   MqlTradeRequest req = {};
   MqlTradeResult  res = {};
   req.action = TRADE_ACTION_DEAL;
   req.symbol = _Symbol;
   req.volume = InpLots;
   req.type   = ORDER_TYPE_BUY;
   req.price  = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
   req.magic  = InpMagic;
   OrderSend(req, res);
  }

Fixed code

MQL5 · fixed
bool OpenBuy()
  {
   MqlTradeRequest req = {};
   MqlTradeResult  res = {};
   req.action    = TRADE_ACTION_DEAL;
   req.symbol    = _Symbol;
   req.volume    = InpLots;
   req.type      = ORDER_TYPE_BUY;
   req.price     = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
   req.deviation = 10;
   req.magic     = InpMagic;
   if(!OrderSend(req, res))
     {
      PrintFormat("OrderSend failed: error %d, retcode %u", GetLastError(), res.retcode);
      return(false);
     }
   if(res.retcode != TRADE_RETCODE_DONE && res.retcode != TRADE_RETCODE_PLACED)
     {
      PrintFormat("Order refused: retcode %u (%s)", res.retcode, res.comment);
      return(false);
     }
   return(true);
  }

The fixed code shows this rule’s fix and compiles in MetaEditor for MetaTrader 5 (with CTrade trade, InpLots and InpMagic declared, as in a typical EA). Other rules may still apply to it as a fragment: check your whole EA.

More rules on trade requests

Sources

Get your EA fixed

Check a whole EA for this and 29 other mistakes with EA Doctor, free and in your browser. Or send it to us: we confirm the price in writing, then fix it.