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
- Declare it as
MqlTradeRequest req = {};, or callZeroMemory(req);before setting its fields. - Clear the
MqlTradeResultthe same way, so an old answer is never read as a new one.
Code that fires it
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
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.
Related MQL5 errors
- 10013Invalid RequestTRADE_RETCODE_INVALID
- 10016Invalid StopsTRADE_RETCODE_INVALID_STOPS
- 10030Invalid Filling TypeTRADE_RETCODE_INVALID_FILL
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.
- 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.
- 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.