MT5/Metatrader 5 connect to different MT5 terminals using python
Asked Answered
M

6

6

I've got multiple python programs that connect to Mt5 terminal using the following code.

# Establish connection to the MetaTrader 5 terminal
if not mt5.initialize("C:\\Program Files\\ICMarkets - MetaTrader 5 - 01\\terminal64.exe"):
    print("initialize() failed, error code =", mt5.last_error())
    quit()

The python module for MT5 is the one here - https://www.mql5.com/en/docs/integration/python_metatrader5

The problem I have is that, when multiple programs connect to the same MT5 terminal.exe, the performance degrades & one or more python programs exit with errors. To overcome this, I have installed multiple copies of MT5 & have updated the python code such that different copies of python program use different installations of MT5. However, only the first installation of MT5 is the only one that can be invoked by all the python programs. Trying to use any other terminal.exe from other installation raises an exception & the connection fails.

There isn't much on the internet either to troubleshoot this. If anyone has ideas to tackle this or has solved the problem, I'd be keen to hear from you please.

The error as such is -

initialize() failed, error code = (-10003, "IPC initialize failed, Process create failed 'C:\\Program Files\\ICMarkets - MetaTrader 5 - 02\terminal64.exe'")

This could be something to do with Windows's default pointing to the first installation or something like that that you wouldn't even think about. Just thinking aloud here.

Mackie answered 20/9, 2020 at 3:38 Comment(0)
A
3

From my experience, imho, the MT5 python API was not designed to handle multiple connections from the same machine simultaneously.

I have overcome this by creating Virtual Machines and running everything through them. I used Oracle VM because it's free, I had past experience with it, but its not very good at sharing resources.

If your machine is not very strong you might want to look into some other solution. I heard Docker is good at sharing the host resources.

Armes answered 24/1, 2021 at 21:28 Comment(1)
Thanks for this Michael. I ended up installing multiple MT5s on the same machine & giving the path of these different installations to the different copies of Python program.Mackie
O
5

The problem is with The python module for MT5 https://www.mql5.com/en/docs/integration/python_metatrader5

they coded that module in such way that don't allow you to run multiple instances of that module , you can connect to one terminal only. but I have a dirty fix for this issue,follow carefully! :

1 - copy the metatrader5 python package from C:\Users\your_user_name\AppData\Local\Programs\Python\Python38\Lib\site-packages\MetaTrader5

2 - add it to your project location

3 - duplicate the "MetaTrader5" Folder inside your project and rename it to different name like "Meta2"

4 - Import that folder like this :

import MetaTrader5 as mt5
import Meta2 as mt2
import time

if not mt5.initialize(path="C:/Program Files/Fusion Markets MetaTrader 5/terminal64.exe",login=xxxx, server="FusionMarkets-Demo",password="xxxxx"):
        print("initialize() failed, error code =",mt5.last_error())

if not mt2.initialize(path="C:/Program Files/MetaTrader 5 EXNESS/terminal64.exe",login=xxxxx, server="Exness-MT5Trial",password="xxxxx"):
        print("initialize() failed, error code =",mt5.last_error())  
       
fusion_ticker = mt5.symbol_info_tick("EURUSD")
exness_ticker = mt2.symbol_info_tick("EURUSDm")
Owenowena answered 15/4, 2022 at 19:44 Comment(0)
A
3

From my experience, imho, the MT5 python API was not designed to handle multiple connections from the same machine simultaneously.

I have overcome this by creating Virtual Machines and running everything through them. I used Oracle VM because it's free, I had past experience with it, but its not very good at sharing resources.

If your machine is not very strong you might want to look into some other solution. I heard Docker is good at sharing the host resources.

Armes answered 24/1, 2021 at 21:28 Comment(1)
Thanks for this Michael. I ended up installing multiple MT5s on the same machine & giving the path of these different installations to the different copies of Python program.Mackie
M
2

You must include terminal64.exe in path. This worked for me:

 path1='C:\\Program Files\\Capitaria MT5 Terminal\\terminal64.exe'

 path2='C:\\Program Files\\Admiral Markets MT5\\terminal64.exe'

            
Moramorabito answered 26/8, 2021 at 2:46 Comment(1)
Thank you. This has solved the issue for me as well!Ramunni
V
1
import MetaTrader5 as mt5

# account details
account1 = {
    "login": 123456,
    "password": "password1",
    "server": "MT5Server1",
}

account2 = {
    "login": 654321,
    "password": "password2",
    "server": "MT5Server2",
}

# connect to MT5 servers
mt5.initialize()

# login to first account
account1_result = mt5.login(account1["login"], account1["password"], account1["server"])
if account1_result:
    print(f"Logged in to account {account1['login']}")
else:
    print(f"Failed to login to account {account1['login']}: {mt5.last_error()}")

# login to second account
account2_result = mt5.login(account2["login"], account2["password"], account2["server"])
if account2_result:
    print(f"Logged in to account {account2['login']}")
else:
    print(f"Failed to login to account {account2['login']}: {mt5.last_error()}")

# disconnect from MT5 servers
mt5.shutdown()
Vagrant answered 30/3, 2023 at 7:56 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Peppie
E
0

In my experience MT5 tries to use latest open MT5 Terminal. I mean if i run another terminal meanwhile code using MT5 Terminal even if i installed in another drive my code starts to manuplate my terminal.

MQL recently launched its web terminal and its seperate from MT5 terminal as you run it from internet. I heard its can be used by mql5 and i want try my code use web terminal somehow for solve this issue. Yet i didn't find any documentation about it. Probably itsn't possible with python either. But i guess mql5 language may solve most of our problems with web terminal use.

Elery answered 23/10, 2023 at 11:12 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Peppie
P
0

try this:


import MetaTrader5 as mt5_1
import MetaTrader5 as mt5_2
counter = 0
while True:
    counter += 1
    print(str(counter), "----------------------")
    mt5_1.initialize(path="d:\\MT5_1\\terminal64.exe", portable=True)
    print(mt5_1.symbol_info_tick("XAUUSD"))
    mt5_2.initialize(path="d:\\MT5_2\\terminal64.exe", portable=True)
    print(mt5_2.symbol_info_tick("XAUUSD"))

I have 1400 loop in one second

Parkman answered 17/4 at 13:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.