Skip to content

EA Doctor rule · Trade requests

Pending orders judged by TRADE_RETCODE_DONE only

The EA places pending orders but treats every answer except TRADE_RETCODE_DONE as a failure, and an accepted pending order answers TRADE_RETCODE_PLACED.

Why it matters

The trade server answers an accepted pending order with TRADE_RETCODE_PLACED (10008, "Order placed"), not with TRADE_RETCODE_DONE (10009, "Request completed").

Code that checks for DONE alone reports every placed order as failed. If it then retries, the account ends up with two or more identical orders on the book.

When the rule fires

The file compares a return code with TRADE_RETCODE_DONE, places pending orders (TRADE_ACTION_PENDING, or CTrade BuyLimit(), SellLimit(), BuyStop(), SellStop(), OrderOpen()) and never mentions TRADE_RETCODE_PLACED.

When it stays quiet

TRADE_RETCODE_PLACED is handled anywhere in the file, or the EA sends market orders only.

How to fix it

  1. Accept TRADE_RETCODE_PLACED as the success of a pending order (and TRADE_RETCODE_DONE, which some servers send).
  2. Before retrying, look for the order: OrdersTotal() and OrderGetTicket() show whether it is already on the book.

Code that fires it

MQL5 · fires this rule
bool PlaceBuyLimit(const double price)
  {
   trade.SetExpertMagicNumber(InpMagic);
   if(!trade.BuyLimit(InpLots, price, _Symbol))
      return(false);
   return(trade.ResultRetcode() == TRADE_RETCODE_DONE);
  }

Fixed code

MQL5 · fixed
bool PlaceBuyLimit(const double price)
  {
   trade.SetExpertMagicNumber(InpMagic);
   if(!trade.BuyLimit(InpLots, price, _Symbol))
      return(false);
   const uint rc = trade.ResultRetcode();
   if(rc == TRADE_RETCODE_PLACED || rc == TRADE_RETCODE_DONE)
      return(true);
   PrintFormat("Buy limit refused: retcode %u (%s)", rc, trade.ResultRetcodeDescription());
   return(false);
  }

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

  • HighOrderSend() result not checkedOrderSend() is called as a plain statement, so a refused or requoted order goes unnoticed and the EA carries on as if it traded.
  • HighCTrade call result not checkedA CTrade method such as Buy(), Sell() or PositionClose() is called as a plain statement, so its failure is never seen.
  • MediumTrade request not cleared before useA local MqlTradeRequest is declared without an initializer and never cleared, so the fields the code does not set hold leftover values.
  • MediumFilling mode not taken from the symbolRaw trade requests go out with a fixed filling type, or the default Fill or Kill, instead of one the symbol allows, which brokers refuse with 10030.

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.