Prevent MQL5 EAs From Trading When Markets Are Closed

Краткое содержание

Pixel’s announcement explains how to prevent MQL5 Expert Advisors from repeatedly sending orders when a market is closed. It shows how to check SymbolInfoSessionTrade for weekday sessions, verify trade mode and terminal connectivity, and avoid confusing retcode 10018 errors during breaks, holidays, and weekend closes.

DEV Community

Stop your MQL5 EA from firing orders into a closed market

If your EA has ever filled its log with retcode 10018 (TRADE_RETCODE_MARKET_CLOSED), you know the pattern. A signal appears during the daily break, on a holiday, or a few seconds before the weekend close, and the EA tries again on every tick. Nothing is lost on that request, but retry logic, counters and alerts all get confused, and on a busy account it can look like the EA is broken. MT5 already knows the trading sessions for each symbol. You only have to ask. Reading the trade sessions SymbolInfoSessionTrade returns the start and end of each trading session for a given weekday. The times come back as seconds from midnight in server time. bool IsInTradeSession(const string symbol)
{
datetime serverNow = TimeTradeServer();
MqlDateTime now;
TimeToStruct(serverNow, now);
ENUM_DAY_OF_WEEK day = (ENUM_DAY_OF_WEEK)now.day_of_week;
int secondsNow = (int)(serverNow % 86400); // seconds since midnight

datetime from, to;
for(uint i = 0; SymbolInfoSessionTrade(symbol, day, i, from, to); i++)
{
int f = (int)from;
int t = (int)to; // 24:00 comes back as 86400
if(secondsNow >= f && secondsNow < t)
return true;
}
return false;
}
A symbol can have several sessions per day, which is why the loop keeps asking for the next index until the call returns false. Check the trade mode too Sessions say when the market is open. They do not say whether you are allowed to open a position. Check SYMBOL_TRADE_MODE as well: bool CanOpen(const string symbol)
{
long mode = SymbolInfoInteger(symbol, SYMBOL_TRADE_MODE);
if(mode == SYMBOL_TRADE_MODE_DISABLED || mode == SYMBOL_TRADE_MODE_CLOSEONLY)
return false;
if(!TerminalInfoInteger(TERMINAL_CONNECTED))
return false;
return IsInTradeSession(symbol);
}
Close only mode shows up more often than people expect, for example on expiring contracts or symbols a broker is retiring. Leave a margin near the edges Quotes often stop a little before the session officially ends, and the first seconds after the open can have wide spreads. A simple rule is to refuse new entries in the last five minutes before a session end and the first two minutes after a start. Exits should still be allowed whenever the market accepts them. Still handle 10018 The session table comes from the broker and holidays are not always reflected in it. So keep a handler for TRADE_RETCODE_MARKET_CLOSED anyway: do not retry on every tick, pause new entries for that symbol for a few minutes, and write one log line instead of hundreds. Test note The strategy tester uses the session settings of the symbol, so you can confirm the filter by running a week that includes a holiday and checking that no orders are attempted while the market is shut.
Prevent MQL5 EAs From Trading When Markets Are Closed
Powered by Readybot.io

Последнее от Pixel

Incident Response: Look Beyond the Red Alert

Pixel’s DEV Community announcement explores how teams can fixate on one failing service while overlooking the real cause of an outage, such as a full …

Sach: AI Fact-Checks WhatsApp Forwards in India

Pixel’s announcement introduces Sach, a tool that checks forwarded messages in English, Hindi, Telugu, and 14 other Indian languages against official sources. It gives cited …

Java 25 Removes SecurityManager: What to Fix

Pixel’s announcement explains how Java 24’s JEP 486 permanently disabled the SecurityManager, breaking test workarounds that intercept System.exit. It recommends returning exit codes or throwing …

Why a Java HPA Got Stuck at 20 Kubernetes Pods

Pixel shares how a Spring Boot pricing service autoscaled to 20 Kubernetes pods and stayed there because the JVM retained committed heap memory after traffic …