EA Doctor rule · Environment and lifecycle
Algo trading permission not checked
The EA sends orders without checking that algo trading is allowed in the terminal, for the EA and on the account, so every order fails when one is off.
Why it matters
Orders from an EA need three permissions: the terminal's Algo Trading button (TERMINAL_TRADE_ALLOWED), the EA's own "Allow Algo Trading" setting (MQL_TRADE_ALLOWED) and the account (ACCOUNT_TRADE_ALLOWED, ACCOUNT_TRADE_EXPERT).
When one is off, every order is refused (10027, 4752) and the journal fills with errors. Checking first lets the EA say so once, on the chart, and wait.
When the rule fires
The EA opens trades and the file checks none of TERMINAL_TRADE_ALLOWED, MQL_TRADE_ALLOWED, ACCOUNT_TRADE_ALLOWED, ACCOUNT_TRADE_EXPERT or SYMBOL_TRADE_MODE, and never calls CTerminalInfo::IsTradeAllowed().
When it stays quiet
Any of those is checked anywhere in the file.
How to fix it
- Before an entry, check the terminal, the program and the account, and skip trading while one is off.
- Show the reason with
Comment(), so the trader sees it without opening the journal.
Code that fires it
void OnTick()
{
if(PositionsTotal() > 0)
return;
trade.SetExpertMagicNumber(InpMagic);
if(!trade.Buy(InpLots, _Symbol))
Print("Buy failed: ", trade.ResultRetcodeDescription());
}Fixed code
bool TradingAllowed()
{
if(!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED))
return(false); // the terminal's Algo Trading button is off
if(!MQLInfoInteger(MQL_TRADE_ALLOWED))
return(false); // "Allow Algo Trading" is off for this EA
if(!AccountInfoInteger(ACCOUNT_TRADE_ALLOWED) || !AccountInfoInteger(ACCOUNT_TRADE_EXPERT))
return(false); // the account does not allow it
return(true);
}
void OnTick()
{
if(!TradingAllowed())
{
Comment("Algo trading is not allowed: check the terminal button and the EA's settings");
return;
}
Comment("");
if(PositionsTotal() > 0)
return;
trade.SetExpertMagicNumber(InpMagic);
if(!trade.Buy(InpLots, _Symbol))
Print("Buy failed: ", trade.ResultRetcodeDescription());
}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
- 10027Algo Trading DisabledTRADE_RETCODE_CLIENT_DISABLES_AT
- 4752Trading by EAs ProhibitedERR_TRADE_DISABLED
- 10017Trade DisabledTRADE_RETCODE_TRADE_DISABLED
- 10018Market ClosedTRADE_RETCODE_MARKET_CLOSED
More rules on environment and lifecycle
- LowTimer started but never stoppedThe EA starts a timer with EventSetTimer() but never calls EventKillTimer(), which the MQL5 reference pairs with it in OnDeinit().
- LowChart objects created but never removedThe EA draws chart objects but never deletes them, so lines and labels stay on the chart after it is removed.
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.