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(ortrade.ResultRetcode()) is10027; the Experts tab shows[auto trading disabled by client].- In code,
TerminalInfoInteger(TERMINAL_TRADE_ALLOWED)isfalsewhen the terminal's button is off, andMQLInfoInteger(MQL_TRADE_ALLOWED)isfalsewhen 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.
#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.
#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.