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, path and data_path.
  • None when the call fails. Check mt5.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_allowed before you send orders. When it is off, orders fail with 10027 or the package's -8.
  • connected can be True while 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 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.