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 afinallyblock so a crash in your code still closes the link. - In a long-running service, do not call
initialize()andshutdown()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 functions
- mt5.initialize() in MetaTrader5 Python: connect Python to the terminal
- mt5.login() in MetaTrader5 Python: log in to a trading account
- mt5.last_error() in MetaTrader5 Python: find out why the last call failed
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.