mt5.shutdown() in MetaTrader5 Python: close the link to the terminal

Closes the link that initialize() opened between your script and the MetaTrader 5 terminal. It takes no arguments and returns None.

Signature

mt5.shutdown()

What it does

Call shutdown() when your script is done with the terminal. It ends your script's link; the terminal itself keeps running.

After shutdown(), every other function fails until you call initialize() again.

What it returns

  • None. There is nothing to check.

Example

import MetaTrader5 as mt5

if not mt5.initialize():
    raise SystemExit(mt5.last_error())
try:
    print(mt5.account_info().balance)
finally:
    mt5.shutdown()   # runs even if the code above raised

Common mistakes

  • Put shutdown() in a finally block so a crash in your code still closes the link.
  • In a long-running service, do not call initialize() and shutdown() around every request. Open the link once and keep it.

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.

There is no link to close on Tickwire. Each request is a plain HTTPS call, and the hosted terminal keeps running between them.

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.