ib_insync(pyton으로 Interactive Brokers API를 조작하는 프로그램 라이브러리)의 설치 및 사용

ib_인스타그램 소개


이것은 python으로 Interactive Brokers API를 조작하는 프로그램 라이브러리입니다.
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


  • Interactive Brokers - API SoftwareVersion: API 9.73.06 이상. (2018/03/16 현재 DL LATEST 버전. Stable 안 됨)
  • IB 게이트웨이 최신 | Interactive Brokers
  • Install


  • IB API LATEST 설치 .(윈도우즈에 설치 프로그램이 있는 것 같은데 그걸 따라가도 될까요?)
    $ 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

  • 최신 IB 게이트웨이 설치
    $ cd /tmp
    $ chmod u+x ibgateway-latest-standalone-linux-x64.sh  
    $ ./ibgateway-latest-standalone-linux-x64.sh   
    

  • ib_insync 설치
    $ pip install -U ib_insync
    
  • 시험해 보다


    IB Gatwy 설정


    IB 게이트웨이를 설치한 후 데모 계정을 통해 로그인합니다.

  • Configure->Settings 앞으로✔넣으면 실행 중 로그가 나타납니다.
  • 설정은 이렇습니다. 빨간 테두리를 컨트롤하는 곳은 괜찮겠죠?내 생각엔

  • jupter notebook에서histoical 데이터 가져오기

    # 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가 설치할 때 psyhoncient의 setup입니다.py를 실행해야 한다고 쓰지 않았습니다. py를 실행하지 않으면 No module named ‘ibapi’ / IB API from http://interactivebrokers.github.io is required 오류가 발생합니다.
  • Jupyter Notebook으로 실행할 때 반드시 설명util.startLoop()
  • IB Gatwy를 최신으로 만들지 않으면 CBOE의 Index 히스토리 데이터를 얻을 수 없습니다.참조: Re:histoical data for indexes on cboe suddenly not working?
  • 시용해 보다

  • 잘한 것 같아. 사용하기 편해. 좋아
  • 데이터피드가 오지 않는 주말에는 사용할 수 없습니다.허허.
  • 좋은 웹페이지 즐겨찾기