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
- Use the return value: when
OrderSend()returnsfalse, logGetLastError()and stop. - Read
result.retcode:TRADE_RETCODE_DONE(andTRADE_RETCODE_PLACEDfor a pending order) is a success, anything else is not. - 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
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
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.
Related MQL5 errors
- 10004RequoteTRADE_RETCODE_REQUOTE
- 10006Request RejectedTRADE_RETCODE_REJECT
- 10013Invalid RequestTRADE_RETCODE_INVALID
- 4756Trade Request Sending FailedERR_TRADE_SEND_FAILED
More rules on trade requests
- 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.
- 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.
- 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.