Skip to content

EA Doctor rule · Trade requests

Trade request not cleared before use

A local MqlTradeRequest is declared without an initializer and never cleared, so the fields the code does not set hold leftover values.

Why it matters

A structure declared inside a function is not set to zero by itself. Every field the code does not fill (sl, tp, deviation, type_filling, expiration, position, comment …) keeps whatever was in that memory before.

The request then carries a stop loss, a filling type or a position ticket nobody chose. The server answers with an invalid request (10013), invalid stops (10016) or an invalid filling type (10030), and the error comes and goes from one call to the next. The MQL5 examples clear every request with ZeroMemory() first.

When the rule fires

A MqlTradeRequest is declared inside a function without an initializer (MqlTradeRequest req;) and ZeroMemory() is never called on it in that function.

When it stays quiet

The request is declared with = {} or cleared with ZeroMemory(req). Requests declared outside any function start at zero and are not flagged.

How to fix it

  1. Declare it as MqlTradeRequest req = {};, or call ZeroMemory(req); before setting its fields.
  2. Clear the MqlTradeResult the same way, so an old answer is never read as a new one.

Code that fires it

MQL5 · fires this rule
bool CloseTicket(const ulong ticket)
  {
   if(!PositionSelectByTicket(ticket))
      return(false);
   MqlTradeRequest req;
   MqlTradeResult  res = {};
   req.action   = TRADE_ACTION_DEAL;
   req.position = ticket;
   req.symbol   = PositionGetString(POSITION_SYMBOL);
   req.volume   = PositionGetDouble(POSITION_VOLUME);
   req.type     = PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY ? ORDER_TYPE_SELL : ORDER_TYPE_BUY;
   req.price    = SymbolInfoDouble(req.symbol, req.type == ORDER_TYPE_SELL ? SYMBOL_BID : SYMBOL_ASK);
   req.magic    = InpMagic;
   return(OrderSend(req, res) && res.retcode == TRADE_RETCODE_DONE);
  }

Fixed code

MQL5 · fixed
bool CloseTicket(const ulong ticket)
  {
   if(!PositionSelectByTicket(ticket))
      return(false);
   MqlTradeRequest req;
   MqlTradeResult  res;
   ZeroMemory(req);
   ZeroMemory(res);
   req.action   = TRADE_ACTION_DEAL;
   req.position = ticket;
   req.symbol   = PositionGetString(POSITION_SYMBOL);
   req.volume   = PositionGetDouble(POSITION_VOLUME);
   req.type     = PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY ? ORDER_TYPE_SELL : ORDER_TYPE_BUY;
   req.price    = SymbolInfoDouble(req.symbol, req.type == ORDER_TYPE_SELL ? SYMBOL_BID : SYMBOL_ASK);
   req.magic    = InpMagic;
   return(OrderSend(req, res) && res.retcode == TRADE_RETCODE_DONE);
  }

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.