mt5.account_info() in MetaTrader5 Python: read balance, equity and margin

Returns the logged-in trading account as a named tuple: login, balance, equity, margin, free margin, leverage, currency and more. None if it fails.

Signature

mt5.account_info()

What it does

account_info() reads the account the terminal is logged in to. Use it to check that you are on the right account and to read money figures before you trade.

The result is a named tuple. Read fields by name, such as info.equity, or turn it into a dict with info._asdict().

What it returns

  • A named tuple with fields such as login, balance, equity, profit, margin, margin_free, margin_level, leverage, currency, server and trade_allowed.
  • None when there is no logged-in account or the link failed. Check mt5.last_error().

Example

import MetaTrader5 as mt5

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

info = mt5.account_info()
if info is None:
    raise SystemExit(f"no account: {mt5.last_error()}")

print(info.login, info.server, info.currency)
print("equity", info.equity, "free margin", info.margin_free)
mt5.shutdown()

Common mistakes

  • None often means the terminal is not logged in yet, even though initialize() returned True.
  • margin_level is a percentage, and it is 0 when no margin is in use.
  • trade_allowed being True does not mean the terminal's own algo trading switch is on. That is mt5.terminal_info().trade_allowed.

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.

account-information returns the same account as JSON with camelCase names: balance, equity, margin, freeMargin, leverage, currency, login, server and tradeAllowed. It is read live from the terminal on every call.

For account, positions, orders, health and server time in one read, use the snapshot route.

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']}"

info = requests.get(f"{base}/account-information", headers=h).json()
print(info["login"], info["currency"])
print("equity", info["equity"], "free margin", info["freeMargin"])

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.