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
| Parameter | What it is |
|---|---|
request | A 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,commentand therequestyou passed. Aretcodeof 0 means the check passed. Nonewhen the check itself could not run. Checkmt5.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_modefirst.
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.
POST /users/current/accounts/{account_id}/trade/checkGET /users/current/accounts/{account_id}/capabilities
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 functions
- mt5.order_send() in MetaTrader5 Python: place, change or close a trade
- mt5.order_calc_margin() in MetaTrader5 Python: work out the margin for an order
- mt5.symbol_info() in MetaTrader5 Python: read a symbol's settings and limits
- mt5.last_error() in MetaTrader5 Python: find out why the last call failed
Related error codes
- MT5 error 10030: Invalid order filling type (TRADE_RETCODE_INVALID_FILL)
- MT5 error 10014: Invalid volume (TRADE_RETCODE_INVALID_VOLUME)
- MT5 error 10019: Not enough money (TRADE_RETCODE_NO_MONEY)
- MT5 error 10013: Invalid request (TRADE_RETCODE_INVALID)
- MT5 error 10016: Invalid stops (TRADE_RETCODE_INVALID_STOPS)
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.