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

ParameterWhat it is
pathFull path to the terminal's .exe file. Optional.
loginThe trading account number. Optional.
passwordThe account password. Optional: if you leave it out, the terminal uses the password it saved for that account.
serverThe trade server name, exactly as the terminal's login window shows it. Optional: the saved server is used if you leave it out.
timeoutHow long to wait for the link, in milliseconds. The default is 60000, one minute.
portableStart the terminal in portable mode, with its data next to the .exe. The default is False.

What it returns

  • True when your script has a working link to the terminal.
  • False when it does not. Call mt5.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.
  • True means your script reached the terminal. It does not prove the terminal reached the broker. Check mt5.terminal_info().connected and that mt5.account_info() is not None before 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 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.