login

使用指定参数连接交易账户。

login(
   login,                    // 账号
   password="PASSWORD",      // 密码
   server="SERVER",          // 在程序端指定的服务器名称
   timeout=TIMEOUT           // 超时
   )

参数

login

[in]  交易账号。所需的未命名参数。

password

[in]  交易账号密码。可选的命名参数。如果没有设置密码,则自动应用在程序端数据库中保存的密码。

server

[in]  交易服务器名称。可选的命名参数。如果没有设置服务器,则自动应用最后使用的服务器。

timeout=TIMEOUT

[in]  连接超时,以毫秒计算。可选的命名参数。如果没有指定,则应用60 000(60秒)的值。如果没有在指定时间内建立连接,则调用强制终止并生成异常。

返回值

成功连接交易账户,返回True,否则,返回False。

例如:

import MetaTrader5 as mt5
# 显示有关MetaTrader 5程序包的数据
print("MetaTrader5 package author: ",mt5.__author__)
print("MetaTrader5 package version: ",mt5.__version__)
 
# 建立与MetaTrader 5程序端的连接
if not mt5.initialize():
    print("initialize() failed, error code =",mt5.last_error())
    quit()
 
# 显示有关MetaTrader 5版本的数据
print(mt5.version())
# 连接到指定密码和服务器的交易账户
account=17221085
authorized=mt5.login(account)  # the terminal database password is applied if connection data is set to be remembered
if authorized:
    print("connected to account #{}".format(account))
其他
    print("failed to connect at account #{}, error code: {}".format(account, mt5.last_error()))
 
# 现在连接到指定密码的另一个交易账户
account=25115284
authorized=mt5.login(accountpassword="gqrtz0lbdm")
if authorized:
    # display trading account data 'as is'
    print(mt5.account_info())
    # display trading account data in the form of a list
    print("Show account_info()._asdict():")
    account_info_dict = mt5.account_info()._asdict()
    for prop in account_info_dict:
        print("  {}={}".format(prop, account_info_dict[prop]))
其他
    print("failed to connect at account #{}, error code: {}".format(account, mt5.last_error()))
 
# 断开与MetaTrader 5程序端的连接
mt5.shutdown()
 
 
Result:
MetaTrader5程序包作者:MetaQuotes Software Corp.
MetaTrader5程序包版本:5.0.29
[500, 2367, '23 Mar 2020']
 
connected to account #17221085
 
connected to account #25115284
AccountInfo(login=25115284, trade_mode=0, leverage=100, limit_orders=200, margin_so_mode=0, ...
account properties:
   login=25115284
   trade_mode=0
   leverage=100
   limit_orders=200
   margin_so_mode=0
   trade_allowed=True
   trade_expert=True
   margin_mode=2
   currency_digits=2
   fifo_close=False
   balance=99588.33
   credit=0.0
   profit=-45.23
   equity=99543.1
   margin=54.37
   margin_free=99488.73
   margin_level=183084.6054809638
   margin_so_call=50.0
   margin_so_so=30.0
   margin_initial=0.0
   margin_maintenance=0.0
   assets=0.0
   liabilities=0.0
   commission_blocked=0.0
   name=James Smith
   server=MetaQuotes-Demo
   currency=USD
   company=MetaQuotes Software Corp.

另见

initializeshutdown