環境
OS: Windows10
MetaTrader 5 Ver. 5.00 build 3521 26 Nov 2022
Python 3.8.15
MetaTrader5 (Python package) Ver. 5.0.43
Broker: TitanFX
状況
- PythonからMetaTrader 5の本番環境にログインできない
- MT5の設定でArgolithmic trading, Python APIを許可済 (CHECK: Tools/Options/Allow algorithmic trading, UNCHECK: Tools/Options/Allow algorithmic trading/Disable algorithmic trading via external Python API)
- コードにバグはない
根拠:Demo口座には同じコードでログイン情報を変えるとログインできるため - ログイン情報(口座番号,パスワード,サーバー名)は正しい
根拠:同じログイン情報でスマホのMT5アプリからはログインできるため
上記の状況に陥り,ググっても解決法が見つからず結構ハマりました。
原因はよくわかりませんが,弄っていたら解決したので解決法を報告しておきます。
コード
import MetaTrader5 as mt5 if mt5.initialize(timeout = 10000): print(f"Initialization succeeded: MT5 Py-package Ver. {mt5.__version__}, {mt5.__author__}") print(f"Mt5 Ver. {mt5.version()}") else: print(f"mt5.initialize() failed, error code = {mt5.last_error()}") authriz = mt5.login( 12345678910, password = "YourPassword", server = "ServerName_OfBroker", timeout = 10000 #10[s] ) if authriz: print("User authorization succeeded") print("Show account_info()._asdict():") account_info_dict = mt5.account_info()._asdict() for prop in account_info_dict: print(f"{prop}={account_info_dict[prop]}") else: print(f"failed to connect, error code: {mt5.last_error()}")
エラー
failed to connect, error code: (-10005, ‘IPC timeout’)
解決法
- WindowsアプリのMetaTrader 5上で,
“File/Open an Account/”
に行き,そこでBrokerを検索する(私の場合,”TitanFX”などと検索窓に入れて検索する) - 検索した結果現れた”Titan FX Limited”(Your Broker)にカーソルを合わせて次へ
- “Connect with an existing trade account”にログイン情報を入れて”完了”をクリック
- ログイン情報が正しければ,口座が正しく認識されてログインできる
- MT5を閉じて,再度上記のプログラムを実行する
まとめ
MT5のGUI上で口座情報を一度紐づけるとPythonから呼び出してもログインできるようになるようです。
コメント