mt5.market_book_add() in MetaTrader5 Python: subscribe to market depth
Subscribes the terminal to market depth, the order book, for one symbol. You need it before market_book_get() returns anything. Returns True or False.
Signature
mt5.market_book_add(symbol) What it does
Market depth shows the buy and sell levels the broker publishes, with a volume at each price. The terminal only collects it for symbols you subscribe to.
Subscribe once, read with market_book_get() as often as you need, and call market_book_release() when you are done.
Parameters
| Parameter | What it is |
|---|---|
symbol | The symbol name. Required. |
What it returns
Truewhen the subscription worked.Falsewhen it did not. Checkmt5.last_error().
Example
import MetaTrader5 as mt5
if not mt5.initialize():
raise SystemExit(mt5.last_error())
if not mt5.market_book_add("EURUSD"):
raise SystemExit(f"no depth subscription: {mt5.last_error()}")
# ... read with mt5.market_book_get("EURUSD") ...
mt5.market_book_release("EURUSD")
mt5.shutdown() Common mistakes
- A subscription can work and still bring no data. Many brokers publish no depth for forex or CFDs.
- Release the subscriptions you no longer need. Each one adds load to the terminal.
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 reads the depth in one call, with no separate subscribe step. A symbol whose broker publishes no depth answers 404 depth_unavailable, never an empty book made to look real.
Market depth is MT5 only. On an MT4 account the route answers not_supported_on_mt4. Check features.marketDepth on the capabilities route first.
GET /users/current/accounts/{account_id}/symbols/{symbol}/current-bookGET /users/current/accounts/{account_id}/capabilities
Related functions
- mt5.market_book_get() in MetaTrader5 Python: read the order book of a symbol
- mt5.market_book_release() in MetaTrader5 Python: stop a market depth subscription
- mt5.symbol_select() in MetaTrader5 Python: add a symbol to Market Watch
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.