Trade server return code
10031 TRADE_ RETCODE_ CONNECTION
No connection with the trade server
What it means
The terminal was not connected to the trade server when the request was sent, so the request never reached it. The terminal reconnects by itself; what matters is what the EA does with the signal in the meantime.
Common causes
- A network interruption on the VPS or the home connection.
- The broker's server restarting, often at the weekend or during maintenance.
- The terminal still connecting right after it starts, while the EA already runs
OnInit()and the first ticks.
A real scenario
An EA marks the current bar as traded before calling OrderSend(). A 30-second network drop on the VPS hits exactly at a signal: the request fails with 10031, and after reconnecting the EA does not try again because the bar is already marked. Marking the bar only after the order is accepted, and checking the connection first, made it enter on reconnect.
How to fix it
- Check
TERMINAL_CONNECTEDbefore sending. - Keep the signal pending and mark it as handled only after the server accepted the order.
- Log the disconnection once, not on every tick.
Fixed MQL5 code
bool Connected()
{
if(TerminalInfoInteger(TERMINAL_CONNECTED))
return true;
static datetime lastNote = 0;
if(TimeLocal() - lastNote > 60) // one line a minute, not one per tick
{
Print("No connection to the trade server: the signal is kept and retried when the connection is back");
lastNote = TimeLocal();
}
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
- 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
Sources
Get your EA fixed
Still failing with 10031? 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.