mt5.initialize() in MetaTrader5 Python: connect Python to the terminal
Opens the link between your Python script and a MetaTrader 5 terminal, and can log in to an account in the same call. Returns True or False.
Signature
mt5.initialize()
mt5.initialize(path)
mt5.initialize(path, login=..., password=..., server=..., timeout=..., portable=False) What it does
Call initialize() before anything else. Every other function needs the link it opens.
With no path, the package looks for an installed terminal by itself and starts it if it is not already running. Pass path when the machine has more than one terminal.
Pass login, password and server to log in to that account as part of the call. Leave them out and the terminal stays on the account it last used.
Parameters
| Parameter | What it is |
|---|---|
path | Full path to the terminal's .exe file. Optional. |
login | The trading account number. Optional. |
password | The account password. Optional: if you leave it out, the terminal uses the password it saved for that account. |
server | The trade server name, exactly as the terminal's login window shows it. Optional: the saved server is used if you leave it out. |
timeout | How long to wait for the link, in milliseconds. The default is 60000, one minute. |
portable | Start the terminal in portable mode, with its data next to the .exe. The default is False. |
What it returns
Truewhen your script has a working link to the terminal.Falsewhen it does not. Callmt5.last_error()straight away to see why.
Example
import os
import MetaTrader5 as mt5
ok = mt5.initialize(
login=int(os.environ["MT5_LOGIN"]),
password=os.environ["MT5_PASSWORD"], # never hard-code it
server=os.environ["MT5_SERVER"],
timeout=30_000,
)
if not ok:
code, text = mt5.last_error()
raise SystemExit(f"initialize failed: {code} {text}")
# ... your work here ...
mt5.shutdown() Common mistakes
- The package runs on Windows only, next to a terminal installed on the same machine. It does not reach the broker by itself.
Truemeans your script reached the terminal. It does not prove the terminal reached the broker. Checkmt5.terminal_info().connectedand thatmt5.account_info()is notNonebefore you trade.- A terminal starting for the first time can take longer than your timeout. A -10005 timeout or -10003 often clears on a second try once the terminal is up.
- One Python process holds one terminal link. To work with two accounts at once, use two processes and two terminal folders.
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 has no initialize() step. Each account gets its own hosted terminal that stays logged in, so your code can run on Linux or macOS and send HTTPS requests with your API key.
To check that the terminal really reached the broker, read the account's health. Its status is healthy, degraded, disconnected or down, measured from reads that worked.
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']}"
health = requests.get(f"{base}/health", headers=h).json()
print(health["status"], health["connected"]) Related functions
- mt5.login() in MetaTrader5 Python: log in to a trading account
- mt5.shutdown() in MetaTrader5 Python: close the link to the terminal
- mt5.last_error() in MetaTrader5 Python: find out why the last call failed
- mt5.terminal_info() in MetaTrader5 Python: check the terminal and its broker link
- mt5.version() in MetaTrader5 Python: read the terminal's version and build
Related error codes
- MT5 Python error -6: Authorization failed (RES_E_AUTH_FAILED)
- MT5 Python error -10003: IPC initialize failed (RES_E_INTERNAL_FAIL_INIT)
- MT5 Python error -10004: No IPC connection (RES_E_INTERNAL_FAIL_CONNECT)
- MT5 Python error -10005: IPC timeout (RES_E_INTERNAL_FAIL_TIMEOUT)
- MT5 Python error -1: Generic failure (RES_E_FAIL)
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.