mt5.order_check() in MetaTrader5 Python: test an order before you send it

Checks whether a trade request would go through, and shows the balance, equity and margin after it, without sending it. Returns a result or None.

Signature

mt5.order_check(request)

What it does

order_check() takes the same request dict as order_send(). The terminal tests it against the account and the symbol's rules, and nothing reaches the broker.

Use it to catch a wrong volume, missing margin or a bad filling mode before a real send.

Parameters

ParameterWhat it is
requestA dict with the same fields as for order_send(), such as action, symbol, volume, type, price and type_filling.

What it returns

  • A result with retcode, balance, equity, profit, margin, margin_free, margin_level, comment and the request you passed. A retcode of 0 means the check passed.
  • None when the check itself could not run. Check mt5.last_error().

Example

import MetaTrader5 as mt5

if not mt5.initialize():
    raise SystemExit(mt5.last_error())

tick = mt5.symbol_info_tick("EURUSD")
req = {
    "action": mt5.TRADE_ACTION_DEAL,
    "symbol": "EURUSD",
    "volume": 0.10,
    "type": mt5.ORDER_TYPE_BUY,
    "price": tick.ask,
    "deviation": 10,
    "type_filling": mt5.ORDER_FILLING_IOC,
}
check = mt5.order_check(req)
if check is None:
    print("check could not run:", mt5.last_error())
elif check.retcode != 0:
    print("would fail:", check.retcode, check.comment)
else:
    print("margin after:", check.margin, "free:", check.margin_free)
mt5.shutdown()

Common mistakes

  • Passing the check is not a promise. The price can move, or the broker can refuse, between the check and the send.
  • A filling mode the symbol does not allow fails here with 10030. Read symbol_info().filling_mode first.

The same with Tickwire on Linux

The package needs a Windows terminal on the same machine. Tickwire runs the terminal for you and gives you a REST API, so the code below runs on Linux, macOS or a server.

Tickwire's trade/check route takes the same body as trade and runs every check we do before a send. On MT5 it also runs the terminal's own order check. It returns the resolved request with margin figures, and nothing is sent.

MT4 has no terminal order check, so on MT4 accounts features.orderCheck is false. Tickwire still checks the request.

import os, requests

API = "https://api.tickwire.io"
h = {"auth-token": os.environ["TW_KEY"]}      # your Tickwire key
base = f"{API}/users/current/accounts/{os.environ['TW_ACCOUNT']}"

r = requests.post(f"{base}/trade/check", headers=h, json={
    "actionType": "ORDER_TYPE_BUY", "symbol": "EURUSD", "volume": 0.10,
})
print(r.status_code, r.json())

Related error codes

All MetaTrader5 Python functions

MT5 from Python, on any machine

One REST API for MT4 and MT5, with idempotent orders and an audit log. Read the MT5 Python API page or the quickstart.