mt5.terminal_info() in MetaTrader5 Python: check the terminal and its broker link
Returns the state and settings of the MetaTrader 5 terminal as a named tuple, including whether it is connected to the broker and allows algo trading.
Signature
mt5.terminal_info() What it does
terminal_info() describes the terminal, not the account. It tells you whether the terminal has a link to the trade server, whether algo trading is switched on, its build, and where its data folder is.
What it returns
- A named tuple with fields such as
connected,trade_allowed,build,ping_last,name,company,pathanddata_path. Nonewhen the call fails. Checkmt5.last_error().
Example
import MetaTrader5 as mt5
if not mt5.initialize():
raise SystemExit(mt5.last_error())
term = mt5.terminal_info()
if term is None:
raise SystemExit(mt5.last_error())
if not term.connected:
print("terminal has no link to the trade server")
if not term.trade_allowed:
print("algo trading is switched off in the terminal")
mt5.shutdown() Common mistakes
- Check
trade_allowedbefore you send orders. When it is off, orders fail with 10027 or the package's -8. connectedcan beTruewhile prices have stopped, for example at the weekend. Check the time of the last tick too.
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 health route gives the same answer as one status, healthy, degraded, disconnected or down, with connected, trading_enabled and the age of the last price. The terminal status route adds the build and the ping.
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']}"
health = requests.get(f"{base}/health", headers=h).json()
print(health["status"], health["trading_enabled"], health["last_tick_age_s"]) Related functions
- mt5.account_info() in MetaTrader5 Python: read balance, equity and margin
- mt5.version() in MetaTrader5 Python: read the terminal's version and build
- mt5.initialize() in MetaTrader5 Python: connect Python to the terminal
Related error codes
- MT5 error 10027: Autotrading disabled by client terminal (TRADE_RETCODE_CLIENT_DISABLES_AT)
- MT5 Python error -8: Auto-trading disabled (RES_E_AUTO_TRADING_DISABLED)
- MT5 error 10031: No connection with the trade server (TRADE_RETCODE_CONNECTION)
- MT5 Python error -10004: No IPC connection (RES_E_INTERNAL_FAIL_CONNECT)
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.