Skip to content

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

  1. Before an entry, check the terminal, the program and the account, and skip trading while one is off.
  2. Show the reason with Comment(), so the trader sees it without opening the journal.

Code that fires it

MQL5 · fires this rule
void OnTick()
  {
   if(PositionsTotal() > 0)
      return;
   trade.SetExpertMagicNumber(InpMagic);
   if(!trade.Buy(InpLots, _Symbol))
      Print("Buy failed: ", trade.ResultRetcodeDescription());
  }

Fixed code

MQL5 · fixed
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.

More rules on environment and lifecycle

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.