Metatrader5 登录()Python 脚本不起作用

问题描述 投票:0回答:2

目前正在按照本教程将Python连接到Metatrader5: https://www.youtube.com/watch?v=zu2q28h9Uvc

我已经卡在 1:45,我的 Metatrader5 正确初始化,它打开,但无法使用我的脚本中输入的凭据登录。我使用经纪商模拟账户。当我手动登录时,它有效。我没有看到错误或任何东西。如果我手动登录,我可以通过我的脚本打印出我的帐户信息,所以连接似乎没问题..这让我发疯,这么简单的事情不起作用,我没有收到任何错误..

这是我的脚本:

import MetaTrader5 as mt
import pandas as pd
import plotly.express as px
from datetime import datetime


server = "ICMarketsSC-Demo"
login = myaccountid
password = "mypassword"

mt.initialize()
mt.login(login, password, server)

account_info = mt.account_info()
print(account_info)

文档参考: https://www.mql5.com/en/docs/integration/python_metatrader5/mt5login_py

如果您发现我做错了什么,请告诉我。预先感谢!

python metatrader4 metatrader5
2个回答
0
投票

对于初始化和登录函数,都应该添加返回结果检查。

例如:

if not mt5.initialize():
  print("Failed to initialize, error code: ", mt5.last_error())
else:  
  if not mt5.login(login, password=password, server=server):
    print("Failed to login, error code: ", mt5.last_error())
  else:
    account_info = mt5.account_info()
    print(account_info)

在最新的Python MT库中,您不需要登录名、密码和服务器来初始化,而是在登录时需要。此外,您可以在初始化函数调用中添加“路径”,“超时”,“可移植”参数。路径的值是“终端”可执行文件的位置,例如“C:\Program Files\MetaTrader 5 erminal64.exe”。

获取错误信息后,即可找出导致登录不成功的原因。


-1
投票

您需要将您提供给登录函数的当前参数传递给初始化函数,并且仅将登录名和密码传递给login()。

import MetaTrader5 as mt5
import pandas as pd
import plotly.express as px
from datetime import datetime


server = "ICMarketsSC-Demo"
login = myaccountid
password = "mypassword"

mt5.initialize(login, password, server)
mt5.login(login, password)

account_info = mt5.account_info()
print(account_info)
© www.soinside.com 2019 - 2024. All rights reserved.