mt5.market_book_get() in MetaTrader5 Python: read the order book of a symbol
Returns the current market depth of a symbol as a tuple of levels, each with a type, a price and a volume. Needs market_book_add() first. None on failure.
Signature
mt5.market_book_get(symbol) What it does
Each level in the result has a type that says whether it is a buy or a sell level, a price, and a volume at that price.
Call market_book_add() for the symbol before the first read, or the result is empty.
Parameters
| Parameter | What it is |
|---|---|
symbol | The symbol name. Required. |
What it returns
- A tuple of depth levels.
Noneon failure. Checkmt5.last_error().
Example
import MetaTrader5 as mt5
if not mt5.initialize():
raise SystemExit(mt5.last_error())
mt5.market_book_add("EURUSD")
levels = mt5.market_book_get("EURUSD")
if not levels:
print("no depth for this symbol:", mt5.last_error())
else:
for lv in levels:
print(lv.type, lv.price, lv.volume)
mt5.market_book_release("EURUSD")
mt5.shutdown() Common mistakes
- An empty tuple is a real answer: the broker publishes no depth for that symbol.
- The book changes many times a second. Each call is a snapshot of one moment.
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 current-book route returns the levels as JSON, each with type, price and volume. A symbol with no published depth answers 404 depth_unavailable. MT4 accounts answer not_supported_on_mt4.
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.get(f"{base}/symbols/EURUSD/current-book", headers=h)
if r.ok:
for lv in r.json()["book"]:
print(lv["type"], lv["price"], lv["volume"])
else:
print(r.json()["error"]) Related functions
- mt5.market_book_add() in MetaTrader5 Python: subscribe to market depth
- mt5.market_book_release() in MetaTrader5 Python: stop a market depth subscription
- mt5.symbol_info_tick() in MetaTrader5 Python: get the latest bid and ask
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.