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,serverandtrade_allowed. Nonewhen there is no logged-in account or the link failed. Checkmt5.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
Noneoften means the terminal is not logged in yet, even thoughinitialize()returnedTrue.margin_levelis a percentage, and it is 0 when no margin is in use.trade_allowedbeingTruedoes not mean the terminal's own algo trading switch is on. That ismt5.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.
GET /users/current/accounts/{account_id}/account-informationGET /users/current/accounts/{account_id}/snapshot
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 functions
- mt5.terminal_info() in MetaTrader5 Python: check the terminal and its broker link
- mt5.positions_get() in MetaTrader5 Python: read open positions
- mt5.order_calc_margin() in MetaTrader5 Python: work out the margin for an order
- mt5.login() in MetaTrader5 Python: log in to a trading account
Related error codes
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.