LoginSignup
13
11

More than 1 year has passed since last update.

ib_insync (Interactive Brokers API を python で操作するためのライブラリ) のインストールと使い方

Last updated at Posted at 2018-03-17

ib_insyncとは

Interactive Brokers の API を python で操作するためのライブラリです.
GitHub - erdewit/ib_insync: Python sync/async framework for Interactive Brokers API

スゴく使いやすいので,使って行こうと思いますが,ちょこちょこハマったので,メモします.

必要環境

  • python3.6以上

私の環境

$ cat /etc/lsb-release
DISTRIB_ID=LinuxMint
DISTRIB_RELEASE=18.1
DISTRIB_CODENAME=serena
DISTRIB_DESCRIPTION="Linux Mint 18.1 Serena"

準備

Download

Install

  1. IB API LATESTをインストール.(windows はインストーラーがあるっぽいのでソレに従えばいい?)

    $ cd /tmp
    $ unzip twsapi_macunix.972.18.zip -d $HOME/
    
    # file が解凍されているか確認
    $ cd ~/IBJts 
    
    # owner を確認
    ~/IBJts $ ls -alt
    drwxr-xr-x 2 root root 4096  3月 14 11:52 ibapi
    drwxr-xr-x 2 root root 4096 12月 28 18:35 tests
    ....
    ....
    
    # もし,↑のようにowner が root なら,自分に変更する
    ~/IBJts $ sudo chown -R shineitaro:shineitaro ./
    
    # pythonclient の setup.py を実行
    $ cd ~/IBJts/source/pythonclient
    ~/IBJts/source/pythonclient $ pip install
  2. IBゲートウェイ最新版をインストール

    $ cd /tmp
    $ chmod u+x ibgateway-latest-standalone-linux-x64.sh  
    $ ./ibgateway-latest-standalone-linux-x64.sh   
    
  3. ib_insync をインストール

    $ pip install -U ib_insync
    

試す

IB Gatway設定

  1. IBゲートウェイをインストール後,Demoアカウントでログイン.
    Screenshot from 2018-03-16 19-07-07.png
  2. Configure -> Settings と進む.(Show Log / Show API message に✔入れておくと,実行中にログがでる.オススメ.)Screenshot from 2018-03-16 19-07-44.png
  3. 設定はこんな感じです.赤枠のところを抑えておけば多分大丈夫かな?と思います.
    Screenshot from 2018-03-16 19-19-19.png

jupyter notebook で historical data を取得

# version は 3.6 以上か確認
import sys
print(sys.version)
# 3.6.4 ~~~

from ib_insync import * 

# Jupyter Notebook で実行するときは必ずイベントループをスタートさせる必要があります.
# (ターミナルでは不要.)
util.startLoop()

# IB に接続
ib = IB()
# clientIdは適当な数値.
ib.connect('127.0.0.1', 4002, clientId=3)

contract = Index('SPX', exchange="CBOE", currency='USD', )
ib.reqHistoricalData(contract,
                          endDateTime='',
                          durationStr='10 D',
                          barSizeSetting='1 day',
                          whatToShow='TRADES', 
                          useRTH=True,
                          formatDate=1)

#[BarData(date=datetime.date(2018, 3, 2), open=2660.42, high=2696.13, low=2647.79, close=2691.25, barCount=10119),
# BarData(date=datetime.date(2018, 3, 5), open=2684.75, high=2727.97, low=2675.9, close=2720.94, barCount=6243),
# BarData(date=datetime.date(2018, 3, 6), open=2728.86, high=2731.36, low=2711.55, close=2728.12, barCount=5745),
# ......

はまったところ

  • 本家githubには install時に,pythonclient の setup.py を実行しろとは書いてない.これをやっていないと,No module named ‘ibapi’ / IB API from http://interactivebrokers.github.io is required というエラーが出る.
  • Jupyter Notebookで実行するときは必ず util.startLoop() を記述する
  • IB Gatwayを最新にしないと,CBOEのIndexヒストリカルデータを取得できない.参照:Re: historical data for indexes on cboe suddenly not working?

使ってみて

  • よく出来てると思う.スゴく使いやすい.たのしい
  • datafeed がこない週末は使えない。とほほ。
13
11
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
11