简体   繁体   中英

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?

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)

Interactive Brokers now has an official Python API (beta 9.73) download . It requires Python 3.1+.

See the docs .

Interactive Brokers hosted a webinar on Nov. 10 2016 about Implement Algo Trading coded in Python using Interactive Brokers API. The presenter gave a good explanation on the applicability of IBridgePy, which is an open-sourced software used to connect to Interactive Brokers C++ API for execution of python codes in live markets.

The webinar was recorded so that you can listen to it anytime you want. The link of the webinar is here: https://www.interactivebrokers.com/en/index.php?f=2227 In the page, IB categorizes their webinars in several topics: TWS, Trading, API, etc. After you click the tab of "API", you will see all of the webinars about API. IBridgePy works like a standalone quantopian and it is much easier than IBpy. IBridgePy can be found here www.IBridgePy.com

Apart from official IB's webinar. There is on-line workshop and Github as following.

Udemy: https://www.udemy.com/python-algo-trading-with-interactive-brokers/

Github: https://github.com/anthonyng2/ib

The content is relatively holistic and covering how to use python 3.x:

  1. Extract Account and Portfolio Information
  2. Placing Orders
  3. Request Market Data
  4. Obtain Historical Data
  5. Access Market Depth Information
  6. Download Real Time Bars
  7. Extract Executions Information, including commission report

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM