Python sync/async framework for Interactive Brokers API


Keywords
ibapi, tws, asyncio, jupyter, interactive, brokers, async, interactive-brokers, python, trading, twsapi
License
BSD-3-Clause
Install
pip install ib-insync==0.9.77

Documentation

Build Join the user group PyPi Conda Number of downloads Documentation

Introduction

The goal of the IB-insync library is to make working with the Trader Workstation API from Interactive Brokers as easy as possible.

The main features are:

  • An easy to use linear style of programming;
  • An IB component that automatically keeps in sync with the TWS or IB Gateway application;
  • A fully asynchonous framework based on asyncio and eventkit for advanced users;
  • Interactive operation with live data in Jupyter notebooks.

Be sure to take a look at the notebooks, the recipes and the API docs.

Installation

pip install ib_insync

For Python 3.6 install the dataclasses package as well (newer Python versions already have it):

pip install dataclasses

Requirements:

  • Python 3.6 or higher;
  • A running TWS or IB Gateway application (version 972 or higher). Make sure the API port is enabled and 'Download open orders on connection' is checked.

The ibapi package from IB is not needed.

Example

This is a complete script to download historical data:

from ib_insync import *
# util.startLoop()  # uncomment this line when in a notebook

ib = IB()
ib.connect('127.0.0.1', 7497, clientId=1)

contract = Forex('EURUSD')
bars = ib.reqHistoricalData(
    contract, endDateTime='', durationStr='30 D',
    barSizeSetting='1 hour', whatToShow='MIDPOINT', useRTH=True)

# convert to pandas dataframe:
df = util.df(bars)
print(df)

Output:

                   date      open      high       low     close  volume  \
0   2019-11-19 23:15:00  1.107875  1.108050  1.107725  1.107825      -1
1   2019-11-20 00:00:00  1.107825  1.107925  1.107675  1.107825      -1
2   2019-11-20 01:00:00  1.107825  1.107975  1.107675  1.107875      -1
3   2019-11-20 02:00:00  1.107875  1.107975  1.107025  1.107225      -1
4   2019-11-20 03:00:00  1.107225  1.107725  1.107025  1.107525      -1
..                  ...       ...       ...       ...       ...     ...
705 2020-01-02 14:00:00  1.119325  1.119675  1.119075  1.119225      -1

Documentation

The complete API documentation.

Changelog.

Discussion

The insync user group is the place to discuss IB-insync and anything related to it.

Consultancy & Development

IB-insync offers an easy entry into building automated trading systems for both individual traders and fintech companies. However, to get the most out of it is not a trivial matter and is beyond the reach of most developers.

If you need expert help, you can contact me. This can be for a small project, such as fixing something in your own code, or it can be creating an entire new trading infrastructure. Please provide enough details so that I can assess both the feasibility and the scope. Many folks worry about having to provide their 'secret sauce', but that is never necessary (although you're perfectly welcome to send that as well!)

Disclaimer

The software is provided on the conditions of the simplified BSD license.

This project is not affiliated with Interactive Brokers Group, Inc.'s.

Good luck and enjoy,

author: Ewald de Wit <ewald.de.wit@gmail.com>