获取交易账户信息

account_info 函数可获取当前交易账户的所有信息。

namedtuple account_info()

该函数以命名元组 (namedtuple) 的结构体返回信息。如果出现错误,则结果为 None

使用此函数,你可以使用一个调用来获取在 MQL5 中由 AccountInfoIntegerAccountInfoDoubleAccountInfoString 提供的所有信息,包括支持特性的所有变体。元组中字段的名称对应于没有 "ACCOUNT_" 前缀的枚举元素的名称,简化为小写。

本书中包含以下 MQL5/Scripts/MQL5Book/Python/accountinfo.py 脚本。

import MetaTrader5 as mt5
  
# let's establish a connection to the MetaTrader 5 terminal
if not mt5.initialize(): 
   print("initialize() failed, error code =", mt5.last_error())
   quit()
   
account_info = mt5.account_info()
if account_info != None:
   # display trading account data as is
   print(account_info) 
   # display data about the trading account in the form of a dictionary
   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]))
   
# complete the connection to the MetaTrader 5 terminal
mt5.shutdown()

结果应该是这样的。

AccountInfo(login=25115284, trade_mode=0, leverage=100, limit_orders=200, margin_so_mode=0, ...

Show account_info()._asdict():

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=99511.4

credit=0.0

profit=41.82

equity=99553.22

margin=98.18

margin_free=99455.04

margin_level=101398.67590140559

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=MetaQuotes Dev Demo

server=MetaQuotes-Demo

currency=USD

company=MetaQuotes Software Corp.