mt5.market_book_release() in MetaTrader5 Python: stop a market depth subscription

Ends the market depth subscription that market_book_add() started for a symbol, so the terminal stops collecting its order book. Returns True or False.

Signature

mt5.market_book_release(symbol)

What it does

Call market_book_release() when your script no longer needs the order book of a symbol. It frees the terminal from that work.

Parameters

ParameterWhat it is
symbolThe symbol name. Required.

What it returns

  • True when the subscription ended.
  • False when it did not. Check mt5.last_error().

Example

import MetaTrader5 as mt5

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

watch = ["EURUSD", "GBPUSD"]
for s in watch:
    mt5.market_book_add(s)
try:
    pass  # ... read the books ...
finally:
    for s in watch:
        mt5.market_book_release(s)
    mt5.shutdown()

Common mistakes

  • Release in a finally block so a crash does not leave subscriptions behind for the next script on that 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 unsubscribe route releases the terminal's market depth subscription for a symbol. It answers 204 with no body.

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.post(f"{base}/symbols/EURUSD/unsubscribe", headers=h)
print(r.status_code)   # 204

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.