What it means
GetLastError() says that this program isn't allowed to trade. The trade function didn't fail because of the order itself; trading is blocked somewhere between the terminal, the EA's settings and the account. In MetaTrader 4 the same situation was error 4109.
Usual causes
MetaQuotes' reference lists four levels of permission, and any one of them blocks trading:
- The terminal. The Algo Trading button in the toolbar is off. The order shows retcode 10027.
- The program. "Allow Algo Trading" isn't ticked in the EA's Common tab.
- Automated trading for the account. The broker has disabled Expert Advisors on the account (
ACCOUNT_TRADE_EXPERTisfalse). - Trading for the account.
ACCOUNT_TRADE_ALLOWEDisfalse: no connection to the server, an account that was archived or switched to read-only, trading disabled by the broker, or a login with the investor password.
How to detect it
GetLastError()returns4752after a trade function. Depending on where the block happens, the result's retcode can also show 10027.- Check the four properties in code:
TerminalInfoInteger(TERMINAL_TRADE_ALLOWED),MQLInfoInteger(MQL_TRADE_ALLOWED),AccountInfoInteger(ACCOUNT_TRADE_EXPERT)andAccountInfoInteger(ACCOUNT_TRADE_ALLOWED). - With the investor password, the terminal's journal records that trading has been disabled in investor mode.
Broken code
This EA compiles. When trading is blocked it prints 4752 on every tick and leaves you to guess which of the four permissions is missing.
#include <Trade/Trade.mqh>
CTrade trade;
void OnTick()
{
if(PositionSelect(_Symbol))
return;
if(!trade.Buy(0.10))
Print("Buy failed, error ", GetLastError()); // "4752", and nothing about which switch is off
}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 the EA may trade, otherwise the first permission that's missing
string TradeBlockedReason()
{
if(!TerminalInfoInteger(TERMINAL_CONNECTED))
return "no connection to the trade server";
if(!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED))
return "Algo Trading is off in the terminal's toolbar";
if(!MQLInfoInteger(MQL_TRADE_ALLOWED))
return "Allow Algo Trading is not ticked in this EA's Common tab";
if(!AccountInfoInteger(ACCOUNT_TRADE_EXPERT))
return "the broker has disabled Expert Advisors on this account";
if(!AccountInfoInteger(ACCOUNT_TRADE_ALLOWED))
return "the account can't trade (investor password, read-only or disabled)";
return "";
}
void OnTick()
{
static string lastReason = "";
string reason = TradeBlockedReason();
if(reason != lastReason)
{
Print(reason == "" ? "Trading allowed" : "Trading blocked: " + reason);
lastReason = reason;
}
if(reason != "" || PositionSelect(_Symbol))
return;
ResetLastError();
if(!trade.Buy(0.10))
PrintFormat("Buy failed: error %d, retcode %u (%s)", GetLastError(), trade.ResultRetcode(), trade.ResultRetcodeDescription());
}Why the fix works
TradeBlockedReason() runs the checks from MetaQuotes' "Trade permission" page in order, plus the connection, and returns a sentence naming the first one that fails. The EA logs the reason once when it changes, not on every tick, and doesn't send orders while trading is blocked. When it does trade and fail, it logs both numbers: GetLastError() and the retcode.
Questions
What is error 4109 in MT4, and what's the MT5 equivalent?
In MQL4, 4109 (ERR_TRADE_NOT_ALLOWED) means trade is not allowed and asks you to enable live trading in the EA's properties. In MQL5 the same situation gives runtime error 4752, and retcode 10027 in the trade result when the terminal blocks the order.
Why can't my EA trade when I log in with the investor password?
The investor password is read-only: ACCOUNT_TRADE_ALLOWED is false and no orders go through, manual or automated. Log in with the account's trading password.
Which permission is off when everything looks enabled?
Check the account level: AccountInfoInteger(ACCOUNT_TRADE_EXPERT) is false when the broker has disabled Expert Advisors on the account, which no button in the terminal can change. Ask the broker in that case.
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.