Search code examples
pythonapiinteractive-brokers

Connecting to Interactive Brokers API via Python


I am looking to connect Python to the Interactive Brokers API. A google search reveals the availability of ibPy (see https://pypi.python.org/pypi/ib) however it appears this library is not maintained nor does it support Python 3. I also found https://github.com/colin1alexander/IbPython3 however the project has since been taken down.

I am aware that Quantopian uses Interactive Brokers as their execution agent but has a python front-end for algorithmic strategies. I am interested in knowing how they achieve this? More broadly however does anyone have any recommended resources/insights on how to connect python to interactive brokers?


Solution

  • Update

    IB has now (Feb 2017) an official Python SDK (aka API). It supports Python 3 only (use IbPy if Python 2 is a must)

    Supports API versions 9.72 and later.


    The ibpy project found a new home under https://github.com/blampe/IbPy (and apparently a new owner with it)

    As you may see in the README the API version supported is 9.70. Current IB API version is 9.72, but the existing ibpy works like a charm with the current versions of TWS (952 stable, 954 latest as of Jan-2015) and the corresponding 9.72 API.

    If using Python 3 I would put most of the emphasis in the bytes vs unicode topic because the strings passed into the API (according to my experience) must be bytes (I usually do Python 2 with from future ... unicode_literals)

    The examples provided with the ibpy distribution work out of the box.

    Edit:

    I have added a couple of working samples in other answers:

    They use Queue to make it a complete working example (the same concept can be applied to deliver historic or real-time data) which deliver what's requested (or the corresponding error)