IB証券 APIでTWS /Gatewayの起動と停止を制御

スポンサーリンク
スポンサーリンク

はじめに

TWS /Gatewayの起動(開始)と停止を制御を制御するモジュールは以下になります。

ib_insync.ibcontroller

ibcontrollerモジュールのクラスは以下の3つになります。

  • ib_insync.ibcontroller.IBC()
    IBCを使用したTWS /Gatewayの開始と停止のプログラムによる制御します。
  • ib_insync.ibcontroller.IBController()
    IBControllerを使用したTWS /Gatewayの開始と停止のプログラムによる制御します。
  • ib_insync.ibcontroller.Watchdog()
    TWSまたはゲートウェイアプリを起動し、接続して監視し、それが稼働し続けるようにします。これは、(再)接続時に自身を適切に初期化するイベント駆動型アプリケーションで使用することを目的としています。

IBController()は以下のように、IBC()を使うことを勧めています。

For new installations it is recommended to use IBC instead.

IBControllerをインストール

ibcontroller.IBC()を利用するにはIBControllerをインストールする必要があります。

インストールについては以下のユーザーガイドを参考にしました。

https://github.com/ib-controller/ib-controller/blob/master/userguide.md#installing-ibcontroller

IBControllerをダウンロード

以下のページの下部あたりにあるZIPをダンロードします。
ZIPのファイル名はIBController-3.4.0.zipなどとバージョンによりことなります。

Release 3.4.0 · ib-controller/ib-controller
This Release contains quite a number of changes, summarised below, but most users will not be affected by it. If your IBController setup is already working to y...

ibcstart.shを入手

私の場合、あとあと以下のようにibcstart.shがないというエラーが出ました。
*ディレクトリ名もデフォルトのインストール場所と異なりますが。。。

[Errno 2] No such file or directory: '/opt/ibc/scripts/ibcstart.sh': '/opt/ibc/scripts/ibcstart.sh'

上記でダウンロードしたZIPを解凍して、scripts内を確認するのですが、やはり存在しませんでした。

ibcstart.shをネットで検索すると以下にありましたので、コピーしてファイルを作成しました。
ファイル名はibcstart.shで、scripts内に配置しました。

https://github.com/IbcAlpha/IBC/blob/master/resources/scripts/ibcstart.sh

IBControllerStart.shというファイルは上位フォルダにあったのですが、まったく別物のようです。

ZIPを作成

いろいろな方法があると思いますが、私はibcstart.shを含んだZIPを作成し、マニュアル通りにインストールを進めることにしました。

Macのターミナルで、以下の手順にしました。

解凍したフォルダ内に移動

cd ~/Downloads/IBController-3.4.0

ZIPを作成

zip -r -X IBController-3.4.0.zip *

インストール(解凍とアクセス権の変更

ここからは基本的にマニュアル通りです。

解凍

sudo unzip ~/Downloads/IBController-3.4.0/IBController-3.4.0.zip -d /opt/ibc

補足
  • ZIPのファイル名はバージョンに異なりますので適宜変更します。
  • 解凍先のフォルダ名はマニュアルではIBControllerでしたが、あとあとデフォルトではエラーになったのでibcに変更しました。
    ib_insyncのIBC()の設定のibcPathで変更できるようで、デフォルトはself.ibcPath = '/opt/ibc'となっていました。

アクセス権の変更

フォルダを移動

cd /opt/IBController

アクセス権を変更
sudo chmod o+x *.sh */*.sh

動作確認

以下のサンプルプログラムで検証しました。

NoteBook?では動作しないと記載されていましたのでターミナルで実行させています。

パラメーターの説明

主なパラメーターは以下になります。

  • twsVersion:TWSのメジャーバージョン(980.4iと記載してあれば、980になります)
  • gateway:ソフトの種類
    • True = gateway
    • False = TWS
  • tradingMode:トレーディングモード
    • ‘live’:ライブ取引(本番)
    • ‘paper’:ペーパートレーディング(デモ)
  • userid:TWSのログインのユーザー名
  • password::TWSのログインのパスワード

その他のパラメーターは以下を参照してください。

ib_insync.ibcontroller — ib_insync 0.9.86 documentation

Mac・Linux

Macで動作を確認しました。

from ib_insync import *

ibc = IBC(980, gateway=False, tradingMode='paper',
    userid='XXXXX', password='YYYYYYY')
ibc.start()
IB.run()

Windows

Windowsでは、asyncioでループさせる必要があるようです。
なお、まだ動作検証を行っていません。
シストレの本番環境はWindowsServerになりますので、改めて検証します。

from ib_insync import *
import asyncio
asyncio.set_event_loop(asyncio.ProactorEventLoop())

ibc = IBC(980, gateway=False, tradingMode='paper',
    userid='XXXXX', password='YYYYYYY')
ibc.start()
IB.run()

動作確認の結果

以下のように表示され、とりあえずはib_insyncのIBC()が動作したことを確認できました。

=========================== An error has occurred =============================



Error: TWS version 980 is not installed: can't find /Users/ZZZZZZ/Applications/IB Gateway 980/jars
       You must install the offline version of TWS/Gateway
       IBC does not work with the auto-updating TWS/Gateway

IBCやIBControllerの設定が必要になりそうです。

IBControllerの設定

IBController User Guideは以下を参照してください。

https://github.com/ib-controller/ib-controller/blob/master/userguide.md

構成ファイル(IBController.ini)を暗号化フォルダーにコピー

任意の場所(ユーザーフォルダの階層以下)にフォルダを作成する。

私は以下のフォルダにしました。
/Users/<username>/IBController/

設定ファイルの修正

修正する対象ファイル

  • Mac / Unix
    • IBControllerStart.sh
    • IBControllerGatewayStart.sh
  • Windows
    • IBControllerStart.bat
    • IBControllerGatewayStart.bat

修正箇所

以下の箇所を修正する必要があります。2種類のファイルともに共通しています。

  • TWS_MAJOR_VRSN:TWSのメジャーバージョン番号。
  • IBC_INI:IBController.iniファイルのパスとファイル名。
    (/Users/<username>/IBController/IBController.iniなど
  • TRADING_MODE:’live’ or ‘paper’*任意?
  • IBC_PATH:IBControllerをインストールしたパス。
    (/opt/IBControllerまたは/opt/ibcなど
  • TWS_PATH:アプリケーションのTWSがあるパス。
    (Macの場合、/Users/<username>/Applications
  • LOG_PATH:ログファイルのパス。
    (/Users/<username>/IBController/logsなど
  • TWSUSERID:TWSのログイン時のユーザーID*任意?
  • TWSPASSWORD::TWSのログイン時のパスワード*任意?
  • JAVA_PATH:JAVAのパス*任意?
  • HIDE:YESまたはTRUEに設定されている場合、情報を含む診断ウィンドウ実行中のTWS、およびログファイルの場所についてはアイコン化されます。*任意?

シェルスクリプトを実行

Mac / Unix

IBControllerStart.sh

Windows

IBControllerStart.bat

シェルスクリプトの実行結果

bash IBControllerStart.sh*私はMacです。

+==============================================================================
+
+ IBController version 3.4.0
+
+ Running TWS 978
+
+ Diagnostic information is logged in:
+
+ /Users/xxxxxx/IBController/Logs/ibc-3.4.0_TWS-978_水曜日.txt
+
+
+==============================================================================
+
+                       **** An error has occurred ****
+
+                     Please look in the diagnostics file 
+                   mentioned above for further information
+
+
+                          Press Ctrl-C to continue
+
+==============================================================================

うーん。エラーが発生します。

ということでログファイルを確認してみると、以下のようなエラーが出ていた。

Error: TWS version 978 is not installed: can't find /Users/xxxxxx/Jts/IB Gateway 978/jars
       You must install the offline version of TWS/Gateway
       IBController does not work with the auto-updating TWS/Gateway

================================================================================

Starting IBController version 3.4.0 on 2020-09-02 at 12:18:49

Operating system: Darwin ZZZZZZZMacBook-Pro.local 19.6.0 Darwin Kernel Version 19.6.0: Thu Jun 18 20:49:00 PDT 2020; root:xnu-6153.141.1~1/RELEASE_X86_64 x86_64

Arguments:

TWS version = 978
Entry point = ibcontroller.IBController
--tws-path = /Users/xxxxxx/Jts
--tws-settings-path = /Users/xxxxxx/Jts
--ibc-path = /opt/ibc
--ibc-ini = /Users/xxxxxx/IBController/IBController.ini
--mode = 
--java-path = 
--user =
--pw =

なぜかtws-pathtws-settings-pathのパスがおかしい?

TWS_PATH=/Users/xxxxxx/Applications

と設定しているのだが。。。

そもそもTWSのインストールフォルダがおかしい。

TWSのバージョン952以降には、バージョン番号に従って名前が付けられた個別のフォルダーに関連ファイルを自動的に配置するインストーラーがあります。

と記載があるのだが、なぜだか違う。私のインストールが間違ったのか?

  • TWS:/Users/xxxxxx/Applications/Trader Workstation*おかしい
  • IB Gateway:/Users/xxxxxx/Applications/IB Gateway 978*正しい

TWSを再インストールしようとしたが、やはりデフォルトのパスは上記の通りだった。

ふと思ったので、エラーになっている以下のフォルダがあるか検索してみた。

/Users/xxxxxx/Jts/IB Gateway 978/jars

  • /Users/xxxxxx/Jtsのフォルダはあった。
  • /Users/xxxxxx/Jts/IB Gateway 978はない。
  • /Users/xxxxxx/Jts/jarsはあった。

うーん。謎が深まってきました。

時間がないので、このあたりで終了します。改めて時間があれば検証したいと思います。

最後まで読んでいただいたのに申し訳ないです。

 

コメント

タイトルとURLをコピーしました