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
MqlTradeRequestwas not cleared withZeroMemory()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
positionticket, 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
- Clear the request and result structures with
ZeroMemory()before every request. - 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. - Run
OrderCheck()beforeOrderSend()and printcheck.retcodeandcheck.comment: they name what is wrong.
Fixed MQL5 code
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.
Related codes
- 10014Invalid VolumeTRADE_RETCODE_INVALID_VOLUMEInvalid volume: the lot breaks the symbol's minimum, maximum or step
- 10015Invalid PriceTRADE_RETCODE_INVALID_PRICEInvalid price: the order price is not valid for the symbol
- 10016Invalid StopsTRADE_RETCODE_INVALID_STOPSInvalid stops: SL or TP too close to the price or on the wrong side
- 4756Trade Request Sending FailedERR_TRADE_SEND_FAILEDTrade request sending failed: read the retcode, not 4756
EA Doctor rules that catch it
- 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.
- MediumPending orders judged by TRADE_RETCODE_DONE onlyThe EA places pending orders but treats every answer except TRADE_RETCODE_DONE as a failure, and an accepted pending order answers TRADE_RETCODE_PLACED.
- 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.
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.