MT5/Metatrader 5使用python连接到不同的MT5终端

1 人关注

我有多个python程序,使用以下代码连接到Mt5终端。

# 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.

互联网上也没有什么可以解决这个问题的方法。如果有人有办法解决这个问题,或者已经解决了这个问题,我很想听听你的意见。

这样的错误是--

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

这可能与Windows的默认指向第一次安装或类似的东西有关,你甚至不会想到。只是在这里大声思考。

python
metatrader5
usert4jju7
usert4jju7
发布于 2020-09-20
3 个回答
Michael
Michael
发布于 2021-08-26
已采纳
0 人赞同

根据我的经验,我认为,MT5的python API不是为了同时处理来自同一台机器的多个连接而设计的。

我通过创建虚拟机并通过它们运行一切来克服这个问题。 我使用了Oracle VM,因为它是免费的,我过去有使用它的经验,但它在共享资源方面不是很好。

如果你的机器不是很强大,你可能想研究一些其他的解决方案。 我听说Docker在共享主机资源方面很好。

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.
siad houssam
siad houssam
发布于 2021-08-26
0 人赞同

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

他们对该模块的编码方式不允许你运行该模块的多个实例,你只能连接到一个终端。 但我有一个肮脏的方法来解决这个问题,请仔细遵循! 。

1 - 从C:\Users\your_user_name\AppData\Local\Programs\Python\Python38\Lib\site-packages\MetaTrader5复制metatrader5 python包。

2 - 将其添加到你的项目位置

3 - 在你的项目中复制 "MetaTrader5 "文件夹,并将其重命名为不同的名称,如 "Meta2"

4 - 像这样导入该文件夹。

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")
    
Vicente Escuer
Vicente Escuer
发布于 2021-08-26
0 人赞同

你必须在路径中包括 terminal64.exe 。这对我来说很有效。

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