MT5 error 10031: No connection with the trade server (TRADE_RETCODE_CONNECTION)
The terminal lost its link to the trade server around the time of the request. The order may have executed; check before any retry.
What it means
You see this code in the retcode of a trade result, from OrderSend() in MQL5 or order_send() in Python.
10031 says the terminal couldn't complete the exchange with the broker's server. The important part is what it doesn't say: whether the request got there first.
If the link dropped after the request left, the broker may have filled it. Sending the same order again can open a second position.
Common causes
- A network drop between the terminal and the broker.
- The broker's server restarting or failing over.
- The terminal switching to another access point.
- A request sent during a weekend or maintenance window.
How to fix it
- Stop and look before resending. Check positions, pending orders and recent deals for your magic number and comment.
- Only if there is no trace of the request, decide whether to send it again.
- Give every order a unique comment or magic number so it can be found later.
Example in Python:
import MetaTrader5 as mt5
from datetime import datetime, timedelta
TAG = "job-4711" # your own id for this order, also sent as the comment
MAGIC = 4711
def find_my_order(symbol):
"""Look for the effect of an order whose answer was unclear."""
for p in mt5.positions_get(symbol=symbol) or ():
if p.magic == MAGIC and p.comment == TAG:
return "position", p.ticket
for o in mt5.orders_get(symbol=symbol) or ():
if o.magic == MAGIC and o.comment == TAG:
return "pending order", o.ticket
# Server time is not UTC, so search a wide window.
now = datetime.now()
deals = mt5.history_deals_get(now - timedelta(days=1), now + timedelta(days=1)) or ()
for d in deals:
if d.magic == MAGIC and d.comment == TAG:
return "deal", d.ticket
return None, None
r = mt5.order_send(request)
if r is not None and r.retcode == mt5.TRADE_RETCODE_CONNECTION:
kind, ticket = find_my_order(request["symbol"])
print("found" if kind else "nothing found yet", kind, ticket) Is it safe to retry?
Not until you have checked. This is the code most likely to cause a duplicate position.
Could the order have executed?
Maybe. In practice it sometimes has.
How Tickwire handles it
Tickwire never sends new exposure again after 10031. It reads positions, orders and deals for the request and reports applied if it finds the effect, rejected if it can confirm nothing happened, or unknown.
An unknown comes back with numericCode -11. A retry with the same Idempotency-Key returns the same answer and sends nothing. See when the answer is unknown.
MT4's ambiguous codes (6, 128, 139, and 1 or 2 on a send) come back as 10031 too.
Related codes
- MT5 error 10011: Request processing error (TRADE_RETCODE_ERROR)
- MT5 error 10012: Request canceled by timeout (TRADE_RETCODE_TIMEOUT)
- MT4 error 6: No connection with trade server (ERR_NO_CONNECTION)
- MT4 error 128: Trade timeout (ERR_TRADE_TIMEOUT)
- MT5 Python error -10002: IPC receive failed (RES_E_INTERNAL_FAIL_RECEIVE)
Related MT5 Python functions
One API for MT4 and MT5
Clear trade outcomes, idempotent orders and an audit log. Read the error reference or the quickstart.