Edgoro
Trade server return code 10027 · MQL5

MT5 error 10027 Autotrading disabled by client: fix

Fix MT5 retcode 10027 (autotrading disabled by client terminal): the Algo Trading button, the EA's Allow Algo Trading box, and how to check both in MQL5.

Trade server return code · MqlTradeResult.retcode
10027 TRADE_RETCODE_CLIENT_DISABLES_AT
“Autotrading disabled by client terminal”
Experts tab · line printed by CTrade (example values)
CTrade::OrderSend: market buy 0.10 EURUSD [auto trading disabled by client]

What it means

MetaTrader itself blocked the order: algorithmic trading is switched off for the whole terminal or for this program. The EA keeps running and computing, but none of its trade requests go through.

Usual causes

  • The Algo Trading button is off. The button in the terminal's toolbar is the master switch: when it's off, no Expert Advisor can trade.
  • The EA isn't allowed to trade. "Allow Algo Trading" wasn't ticked in the Common tab when the EA was attached to the chart.
  • The terminal switched it off. The Expert Advisors tab of Tools → Options has settings that turn algo trading off on some events, such as a change of account or profile.

Two neighbours have their own numbers: when the broker disables automated trading for the account, the code is 10026 (TRADE_RETCODE_SERVER_DISABLES_AT), and GetLastError() can return 4752, trading by Expert Advisors prohibited.

How to detect it

  • result.retcode (or trade.ResultRetcode()) is 10027; the Experts tab shows [auto trading disabled by client].
  • In code, TerminalInfoInteger(TERMINAL_TRADE_ALLOWED) is false when the terminal's button is off, and MQLInfoInteger(MQL_TRADE_ALLOWED) is false when this program isn't allowed to trade.
  • On the chart, the icon next to the EA's name in the top-right corner shows whether it may trade.

Broken code

This EA compiles. With Algo Trading off, it prints "Buy failed" on every tick without saying why.

MQL5
#include <Trade/Trade.mqh>

CTrade trade;

void OnTick()
  {
   if(PositionSelect(_Symbol))
      return;
   // Tries on every tick, whether or not the terminal lets it trade
   if(!trade.Buy(0.10))
      Print("Buy failed");
  }

Fixed code

The same program with the check that prevents the error. It compiles in MetaEditor with 0 errors and 0 warnings.

MQL5
#include <Trade/Trade.mqh>

CTrade trade;

// Empty string when this EA may trade, otherwise what the user has to switch on
string AlgoTradingProblem()
  {
   if(!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED))
      return "Algo Trading is off: press the Algo Trading button in the toolbar";
   if(!MQLInfoInteger(MQL_TRADE_ALLOWED))
      return "This EA may not trade: tick Allow Algo Trading in its Common tab";
   return "";
  }

int OnInit()
  {
   string problem = AlgoTradingProblem();
   if(problem != "")
      Print(problem);
   return INIT_SUCCEEDED;
  }

void OnTick()
  {
   string problem = AlgoTradingProblem();
   Comment(problem);   // shown on the chart until it's fixed
   if(problem != "" || PositionSelect(_Symbol))
      return;
   if(!trade.Buy(0.10))
      PrintFormat("Buy failed: retcode %u (%s)", trade.ResultRetcode(), trade.ResultRetcodeDescription());
  }

void OnDeinit(const int reason)
  {
   Comment("");
  }

Why the fix works

The EA checks the two client-side switches before trading, with the property MetaQuotes' reference uses for each: the terminal (TERMINAL_TRADE_ALLOWED) and the program (MQL_TRADE_ALLOWED). When one is off, it writes what to do on the chart instead of sending requests that the terminal will block. When you switch Algo Trading back on, it resumes on the next tick.

Questions

Why does my EA say autotrading disabled when the button is green?

The button only covers the terminal. The EA also needs its own permission: open the EA's properties from the chart and tick Allow Algo Trading in the Common tab. MQLInfoInteger(MQL_TRADE_ALLOWED) tells you which one is off.

What's the difference between error 10027 and 10026?

10027 means the terminal blocked the order (Algo Trading off, or the EA not allowed). 10026 means automated trading is disabled for the account on the broker's side, which you can't change from the terminal.

Does Algo Trading switch itself off?

It can. The Expert Advisors tab of Tools → Options has settings that disable algorithmic trading on certain events, such as a change of account or profile. Check them if the button keeps turning off.

The programs on this page are minimal examples written to show the error and its fix, not trading systems. Some of them send orders when they run: try them on a demo account. Trading involves substantial risk of loss and is not suitable for every investor. Edgoro is a software tool, not a broker or financial adviser: nothing it produces is investment advice, and past or backtested performance does not guarantee future results.

Working code that avoids it

All MQL5 examples

Rather not write the error handling yourself?

Edgoro's MT5 EA generator writes complete MQL5 Expert Advisors from plain English and compiles each one in MetaEditor before you get it. A clean build proves the code compiles, not that a strategy works: test it on a demo account.