Skip to main content

Use the Interactive Brokers API (IBAPI) asynchonouslywith asyncio or PyQt5

Project description

Introduction

The tws_async package allows the Python API from Interactive Brokers (IBAPI) to be used asynchronously and single-threaded with the asyncio standard library or with the PyQt5 framework.

This offers a simpler, safer and more performant approach to concurrency than multithreading.

Installation

Install using pip:

pip3 install -U tws_async

Note that on some systems the pip3 command is just pip.

Python version 3.5 or higher is required as well as the Interactive Brokers Python API.

Usage

This package offers two clients that can be used as a drop-in replacement for the standard EClient as provided by IBAPI:

  • TWSClient, for use with the asyncio event loop;

  • TWSClientQt, for use with the PyQt5 event loop.

These clients also inherit from ibapi.wrapper.EWrapper and can be used exactly as one would use the standard IBAPI version. The asynchronous clients use their own event-driven networking code that replaces the networking code of the standard EClient, and they also replace the infinite loop of EClient.run() with an event loop.

To simplify working with contracts, this package provides Contract, Stock, Option, Future, Forex, Index, CFD and Commodity classes that can be used anywhere where a ibapi.contract.Contract is expected. Examples of some simple cases are Stock('AMD'), Forex('EURUSD'), CFD('IBUS30') or Future('ES', '201612', 'GLOBEX'). To specify more complex contracts, any property can be given as a keyword.

To learn more, consult the official IBAPI documentation or have a look at these sample use cases:

Historical data downloader

The HistRequester downloads historical data and saves it to CSV files; histrequester demo illustrates how to use it.

Realtime streaming ticks

The tick streamer subscribes to realtime tick data.

Jupyter Notebook

To use the Interactive Brokers API fully interactively in a Jupyter notebook, have a look at the example notebook.

Jupyter can be started with the command jupyter notebook.

This notebook uses the Qt version of the client, where the Qt event loop is started with the %gui qt5 directive at the very top. It is not necessary to call the run() method of the client.

Notes on using asycio in a notebook

Currently there does not seem to be a single-threaded way to directly run the asyncio event loop in Jupyter. What can be done is to use the Qt event loop (which does have good integration with the Jupyter kernel) with the quamash adaptor. With quamash the Qt event loop is used to drive the asyncio event loop. This can be done by placing the following code at the top of the notebook:

%gui qt5
import asyncio
import quamash
loop = quamash.QEventLoop()
asyncio.set_event_loop(loop)

One thing that does not work in the combination of quamash and Jupyter is the loop.run_until_finished method. It can be patched like this:

def run_until_complete(self, future):
    future = asyncio.ensure_future(future)
    qApp = qt.QApplication.instance()
    while(not future.done()):
        qApp.processEvents()
        qt.QThread.usleep(1000)
    qApp.processEvents()
    return future.result()

quamash.QEventLoop.run_until_complete = run_until_complete

The asyncio version of the client relies on loop.run_until_finished to connect synchonously. So in order to run the asyncio client in the notebook, apply the patch or just connect asynchonously (i.e. give asyncConnect=True to the connect call).

Changelog

Version 0.5.5

  • small simplifications

Version 0.5.4

  • connect() call of the clients will now by default block until client is ready to serve requests.

  • getReqId() method added to both clients.

  • dataHandlingPre() and dataHandlingPost() event hooks added to clients.

  • logging added.

  • util module aded.

  • file tws_async.py renamed to twsclient.py, tws_asyncqt.py to twsclientqt.py.

Version 0.5.3

  • Added optional asyncConnect argument to client.connect() method. The default is now to connect synchronously (block until connected).

  • Fixed bug in HistRequester when downloading daily data.

Version 0.5.0

  • Initial pip package release.

Good luck and enjoy,

author:

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tws_async-0.5.5-py3-none-any.whl (54.2 kB view details)

Uploaded Python 3

File details

Details for the file tws_async-0.5.5-py3-none-any.whl.

File metadata

File hashes

Hashes for tws_async-0.5.5-py3-none-any.whl
Algorithm Hash digest
SHA256 4b517519f33483596fbd85eb6fe0737ae380d7ecb5e6a4ad1c370d63f841e47d
MD5 7ae6bd3a12f51a1f47379e2f827d6c17
BLAKE2b-256 f1763b4d3506f00fc987c49ec7b00cf798675e3312b40d20599a826019af68c0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page