# MT5 Python initialize()

Independent and unofficial. Not affiliated with or endorsed by any broker or platform named here. Educational only: nothing here is advice, and nothing here recommends a broker or a trade.

This is the markdown version of https://openbrokerapi.com/platforms/metatrader-5/functions/python-initialize, written from the same records as that page.

Risk warning: CFDs and spread bets are complex instruments and come with a high risk of losing money rapidly due to leverage. You can lose money on these products, and each provider publishes its own loss figure under FCA rules. You should consider whether you understand how they work and whether you can afford to take the high risk of losing your money. Automated and API trading can amplify losses through bugs, connectivity failures and runaway orders.

Connects the Python package to a MetaTrader 5 terminal on the same computer, starting the terminal if it is not running, and can log in to a trading account in the same call.

- Function: `initialize()`
- Interface: MetaTrader5 Python package
- Group: Connection
- Status: verified. Source: [mql5.com](https://www.mql5.com/en/docs/python_metatrader5/mt5initialize_py), checked 2026-10-12.

This is a Python function, not an HTTP endpoint. It has no method, no path and no address to send a request to: the package talks to a MetaTrader 5 terminal running on the same computer. It is in no OpenAPI file on this site and the request builder does not list it.

Note: The page shows three forms of the call: with nothing, with a path alone, and with a path and account details. Its example logs in with an account number and a password typed into the code. The sample here does not: it lets the terminal use the account it already has saved.

## Signature

The 3 forms of the call on the vendor's page, with the vendor's comments taken out.

```python
initialize()
```

```python
initialize(
  path
)
```

```python
initialize(
  path,
  login=LOGIN,
  password="PASSWORD",
  server="SERVER",
  timeout=TIMEOUT,
  portable=False
)
```

## Arguments

- `path` (type not stated, optional): Where the terminal's executable is: the page names metatrader.exe or metatrader64.exe. Given first and without a name. Left out, the package looks for the terminal itself.
- `login` (type not stated, optional): The trading account number. Left out, the terminal's last used account is used.
- `password` (type not stated, optional): The trading account's password. Left out, the password the terminal has saved for that account is used, so it need not appear in code at all.
- `server` (type not stated, optional): The trade server's name, as the terminal shows it. Left out, the server saved for that account is used.
- `timeout` (type not stated, optional): How long to wait for the connection, in milliseconds. The page gives the default as 60,000, which is 60 seconds.
- `portable` (type not stated, optional): Whether to start the terminal in portable mode. The page gives the default as False.

## Return value

- Type: `bool`
- What comes back: True when the connection to the terminal is made, False otherwise. The page's own example reads last_error() after a False.

## Sample

Written by this site from the signature and arguments recorded on this page. It has not been run against a terminal, a server or an account, and the values in it are placeholders. No credential is typed into it: where one is needed it is read from an environment variable on your own machine. Nothing is entered on this site.

```python
import MetaTrader5 as mt5

# No account details in code: the terminal uses the account it already has saved.
if not mt5.initialize():
    raise SystemExit(f"initialize() failed: {mt5.last_error()}")

print(mt5.version())
mt5.shutdown()
```

## Related MT5 Python functions: Connection

- [last_error()](https://openbrokerapi.com/platforms/metatrader-5/functions/python-last-error): Returns the code and description of the last error raised by a call to the package. The codes are the package's own, not the terminal's.
- [login()](https://openbrokerapi.com/platforms/metatrader-5/functions/python-login): Switches the connected terminal to a trading account, by account number, with the password and server optional.
- [shutdown()](https://openbrokerapi.com/platforms/metatrader-5/functions/python-shutdown): Closes the connection that initialize() opened between the Python package and the terminal.
- [terminal_info()](https://openbrokerapi.com/platforms/metatrader-5/functions/python-terminal-info): Returns the status and settings of the connected terminal in one call.
- [version()](https://openbrokerapi.com/platforms/metatrader-5/functions/python-version): Returns the version of the connected MetaTrader 5 terminal, with its build number and the date of that build.

## More on MetaTrader 5

- [All MetaTrader 5 functions](https://openbrokerapi.com/platforms/metatrader-5/functions): 78 documented here, grouped by what they do.
- [Log in and keep a session: the call at each API](https://openbrokerapi.com/endpoints/session): The same job at every broker and platform API recorded here, HTTP or not.
- [MetaTrader 5 API quickstart](https://openbrokerapi.com/platforms/metatrader-5/quickstart)
- [All MetaTrader 5 error codes](https://openbrokerapi.com/errors/mt5)
- [MetaTrader 5 API page](https://openbrokerapi.com/platforms/metatrader-5): The full record: access, authentication, limits and the source for each.

## Licence and how to cite

The records behind this page are all rights reserved. This file is the page itself, at its own address, in another format. It is not part of the CC BY 4.0 downloads listed at https://openbrokerapi.com/data. Quote from it with a link to the page.

Cite as: MT5 Python initialize(). Broker API Hub, https://openbrokerapi.com/platforms/metatrader-5/functions/python-initialize, newest check 2026-10-12.

Where you rely on a single figure, cite the source and the checked date printed beside it as well. Where this page and a broker's own documentation differ, the documentation wins.
