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) # ターミナルデータベースのパスワードは、接続データが保存されるように設定されている場合に適用される
if authorized:
  print("connected to account #{}".format(account))
else:
  print("failed to connect at account #{}, error code: {}".format(account, mt5.last_error()))
 
# 次に、パスワードを指定して別の取引口座に接続する
account=25115284
authorized=mt5.login(account, password="gqrtz0lbdm")
if authorized:
  # 取引口座データを「そのまま」表示する
  print(mt5.account_info())
  # 取引口座データをリスト形式で表示する
  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]))
else:
  print("failed to connect at account #{}, error code: {}".format(account, mt5.last_error()))
 
# MetaTrader 5ターミナルへの接続をシャットダウンする
mt5.shutdown()
 
 
Result:
MetaTrader5 package author:  MetaQuotes Software Corp.
MetaTrader5 package version:  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