Help request for MetaTrader5 initialization with Python

 

Hello everyone,

I'm seeking help with an initialization issue with the MetaTrader5 Python library.

My script is as follows:

import os
import MetaTrader5 as mt5
from dotenv import load_dotenv

load_dotenv()

print("Initializing...")

# Initializes the MetaTrader5 connection
if not mt5.initialize():
    print("Initialization failed")
    mt5.shutdown()
    exit()

print("Initialization successful!")

# Prints the MetaTrader5 version
print("Version:", mt5.version())

# Connects to the trading account (optional)
authorized = mt5.login(login=int(os.getenv("LOGIN")), password=os.getenv("PASSWORD"), server=os.getenv("SERVER"))

if not authorized:
    print("Connection failed!")
    mt5.shutdown()
    exit()

print("Connection successful!")

mt5.shutdown()

When I run this code, the MetaTrader5 GUI launches as expected. However, the Python process immediately freezes after the mt5.initialize() call, as you can see in the attached screenshot. The line print("Initialization successful!") is never executed, and I have to manually stop the script.

I've already checked the following without success:

  • I'm using MetaTrader5 version 5.00 build 5233.

  • I have administrator rights for the application.

  • I have tried running the script in different environments (VS Code, standard terminal).

  • My .env file is configured correctly.

Could you please help me understand why the process is blocking? Is there a specific initialization parameter I should be aware of or a known incompatibility with this version of MT5?

Any help would be greatly appreciated.

Thank you in advance, Néhémie Konan

 
Wa Konan Néhémie Konan:

Hello everyone,

I'm seeking help with an initialization issue with the MetaTrader5 Python library.

My script is as follows:

When I run this code, the MetaTrader5 GUI launches as expected. However, the Python process immediately freezes after the mt5.initialize() call, as you can see in the attached screenshot. The line print("Initialization successful!") is never executed, and I have to manually stop the script.

I've already checked the following without success:

  • I'm using MetaTrader5 version 5.00 build 5233.

  • I have administrator rights for the application.

  • I have tried running the script in different environments (VS Code, standard terminal).

  • My .env file is configured correctly.

Could you please help me understand why the process is blocking? Is there a specific initialization parameter I should be aware of or a known incompatibility with this version of MT5?

Any help would be greatly appreciated.

Thank you in advance, Néhémie Konan

Hi Konan, Make sure your Python and MT5 Match 64 bit, In Python run the following,  import struct, MetaTrader5 as mt5

print("Python bitness:", struct.calcsize("P")*8) 
print("MT5 module:", mt5.__version__)

It should print 64


Improperly formatted code edited by moderator. Please always use the CODE button (Alt-S) when inserting code.

Code button in editor

 
Allan Graham Pike #:

Hi Konan, Make sure your Python and MT5 Match 64 bit, In Python run the following,  import struct, MetaTrader5 as mt5

It should print 64


Improperly formatted code edited by moderator. Please always use the CODE button (Alt-S) when inserting code.


Hi Allan,

Thank you so much for your help! Your suggestion to check the bitness of Python and MT5 was a great starting point and led me to the solution. The code you provided confirmed that both were 64-bit, which helped me rule out that specific incompatibility.

I then suspected a version mismatch. I tried downgrading Python to version 3.10, but the problem persisted. It turns out the issue was a mismatch between the version of MetaTrader5 on the developer's website and the one from my broker. Once I downloaded and installed the version provided by my broker (5.00 build 5238 more recent than build 5233), the freezing issue was immediately resolved.

Thanks again for your valuable assistance. It helped me get to the bottom of the problem!