Trade server return code
10018 TRADE_ RETCODE_ MARKET_ CLOSED
Market is closed: the request was sent outside a trading session
What it means
The symbol is outside its trading session at the server's time. The reference's own OrderCheck example shows the trap: on a closed market the check passes ("Retcode: 0"), and only OrderSend() fails, with error 4756 and retcode 10018.
Common causes
- Signals evaluated on the last ticks before the weekend or after the session close.
- A daily break in the symbol's session, for example the short pause many brokers have on metals and indices around the end of the server day.
- Holiday sessions shortened by the broker.
- Time filters written in local or GMT time while the sessions are defined in server time.
A real scenario
An EA on XAUUSD opens trades at the close of each H1 bar. On the broker's server, gold pauses for a short break at the end of each day, and the bar that closes into the break fires a signal: the order fails with 10018, and the EA's "one trade per bar" flag was already set, so that trade was lost. The EA now checks the session first and keeps the signal for the reopening.
How to fix it
- Check that the server time is inside one of the symbol's trading sessions (
SymbolInfoSessionTrade) before sending. - Do not rely on
OrderCheck()for this: it does not test the session. - Mark a signal as handled only after the order was accepted, so a closed market does not swallow it.
Fixed MQL5 code
bool InTradeSession(const string symbol)
{
MqlDateTime now;
TimeToStruct(TimeTradeServer(), now);
datetime sinceMidnight = (datetime)(now.hour * 3600 + now.min * 60 + now.sec);
datetime from, to; // seconds from 00:00 of the day (the date part is ignored)
for(uint i = 0; SymbolInfoSessionTrade(symbol, (ENUM_DAY_OF_WEEK)now.day_of_week, i, from, to); i++)
{
if(sinceMidnight >= from && sinceMidnight < to)
return true;
}
return false;
}
A focused example for MetaTrader 5: adapt names, magic number and limits to your EA, and test it on a demo account first.
Related codes
- 10017Trade DisabledTRADE_RETCODE_TRADE_DISABLEDTrade is disabled for the symbol or the account
- 10021Off QuotesTRADE_RETCODE_PRICE_OFFOff quotes: no current price for the symbol
- 4756Trade Request Sending FailedERR_TRADE_SEND_FAILEDTrade request sending failed: read the retcode, not 4756
EA Doctor rules that catch it
- LowAlgo trading permission not checkedThe 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.
Sources
Get your EA fixed
Still failing with 10018? Send us the EA and the journal lines: we find the cause in the code and fix it. Or check it yourself first with the EA Doctor.