Skip to content

Trade server return code

10013 TRADE_RETCODE_INVALID

Invalid request: a field of the trade request is wrong or missing

What it means

The request structure did not pass the server's validation. Price, volume and stop levels have their own codes (10015, 10014, 10016), so 10013 usually points at the structure itself: the action, the order type, the symbol, or the ticket of the position or order the request refers to.

Common causes

  • The MqlTradeRequest was not cleared with ZeroMemory() and still holds fields from an earlier request.
  • An order type that does not fit the action, for example a pending order type sent with TRADE_ACTION_DEAL.
  • On a hedging account, a modify or close request without the position ticket, so the server cannot tell which position is meant.
  • Code ported from MQL4 that fills the MQL5 request the way OrderSend() worked in MQL4.

A real scenario

An EA written on a netting account changes its stop loss with TRADE_ACTION_SLTP and fills only the symbol. Moved to a hedging account, where several positions can be open on the same symbol, the same request no longer says which position to change, and the modification is refused as invalid. Adding the position ticket fixed it on both account types.

How to fix it

  1. Clear the request and result structures with ZeroMemory() before every request.
  2. Fill exactly the fields the action needs (the OrderSend page of the reference lists them per action), or use CTrade, which fills them for you.
  3. Run OrderCheck() before OrderSend() and print check.retcode and check.comment: they name what is wrong.

Fixed MQL5 code

MQL5
bool ModifyStops(const ulong ticket, const double sl, const double tp)
{
   if(!PositionSelectByTicket(ticket))
      return false;
   MqlTradeRequest     req;
   MqlTradeResult      res;
   MqlTradeCheckResult check;
   ZeroMemory(req);                          // never reuse fields from an earlier request
   ZeroMemory(res);
   ZeroMemory(check);
   req.action   = TRADE_ACTION_SLTP;
   req.position = ticket;                    // required on hedging accounts
   req.symbol   = PositionGetString(POSITION_SYMBOL);
   req.sl       = sl;
   req.tp       = tp;
   req.magic    = (ulong)PositionGetInteger(POSITION_MAGIC);
   if(!OrderCheck(req, check))
   {
      PrintFormat("OrderCheck: retcode %u, %s", check.retcode, check.comment);
      return false;
   }
   if(!OrderSend(req, res) || res.retcode != TRADE_RETCODE_DONE)
   {
      PrintFormat("OrderSend: retcode %u, %s", res.retcode, res.comment);
      return false;
   }
   return true;
}

A focused example for MetaTrader 5: adapt names, magic number and limits to your EA, and test it on a demo account first.

EA Doctor rules that catch it

Sources

Get your EA fixed

Still failing with 10013? Send us the EA and the journal lines: we find the cause in the code and fix it. Or check it yourself first with the EA Doctor.