All Projects → erdewit → tws_async

erdewit / tws_async

Licence: Unlicense license
Make the Python IB API from Interactive Brokers run inside an event loop

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to tws async

Execution Trace Viewer
Tool for viewing and analyzing execution traces
Stars: ✭ 149 (+88.61%)
Mutual labels:  pyqt5
Shadowsocks Pyqt
一个用PyQt5实现的shadowsocks 客户端, 可以在 windows、linux、OSX 等平台上运行
Stars: ✭ 209 (+164.56%)
Mutual labels:  pyqt5
Photobooth
A flexible photobooth software
Stars: ✭ 227 (+187.34%)
Mutual labels:  pyqt5
Pyqtclient
PyQt Examples Client
Stars: ✭ 160 (+102.53%)
Mutual labels:  pyqt5
Skillbox Chat
Skillbox demo application for the Python course
Stars: ✭ 86 (+8.86%)
Mutual labels:  pyqt5
Stackoverflow
my answers in Stack Overflow
Stars: ✭ 211 (+167.09%)
Mutual labels:  pyqt5
Qdarkstylesheet
A dark style sheet for QtWidgets application
Stars: ✭ 1,952 (+2370.89%)
Mutual labels:  pyqt5
HealthApp
A desktop application to fetch Wikipedia,Google,Disease results and save them as text file,in database.Have a Section to search details about doctors in location
Stars: ✭ 23 (-70.89%)
Mutual labels:  pyqt5
Pyqt5 Chinese Tutorial
PyQt5中文教程
Stars: ✭ 2,574 (+3158.23%)
Mutual labels:  pyqt5
Breezestylesheets
Breeze/BreezeDark-like Qt StyleSheets
Stars: ✭ 221 (+179.75%)
Mutual labels:  pyqt5
Amadia
Astus' Mathematical Display Application : A GUI for Mathematics (Calculator, LaTeX Converter, Plotter, ... )
Stars: ✭ 172 (+117.72%)
Mutual labels:  pyqt5
Cq Editor
CadQuery GUI editor based on PyQT
Stars: ✭ 183 (+131.65%)
Mutual labels:  pyqt5
V2rayl
v2ray linux GUI客户端,支持订阅、vemss、ss等协议,自动更新订阅、检查版本更新
Stars: ✭ 2,722 (+3345.57%)
Mutual labels:  pyqt5
Pyqt5 Qtquick2 Example
A sample of QtQuick 2 providing material and fluent design themes in PyQt5.
Stars: ✭ 159 (+101.27%)
Mutual labels:  pyqt5
Oblivion
Data leak checker & OSINT Tool
Stars: ✭ 237 (+200%)
Mutual labels:  pyqt5
Mindfulness At The Computer
Helps you stay mindful of your breathing while using your computer
Stars: ✭ 143 (+81.01%)
Mutual labels:  pyqt5
Uranium
A Python framework for building Desktop applications.
Stars: ✭ 210 (+165.82%)
Mutual labels:  pyqt5
python-kaynaklari
Python Türkiye Facebook sayfasında başlatılan projenin yeni sayfası
Stars: ✭ 30 (-62.03%)
Mutual labels:  pyqt5
food-and-grocery-automatization
Python application that automatizes your food and grocery orders using Selenium library.
Stars: ✭ 15 (-81.01%)
Mutual labels:  pyqt5
Face recognition py
基于OpenCV的视频人脸识别
Stars: ✭ 215 (+172.15%)
Mutual labels:  pyqt5

Notice

This project has evolved into IB-insync and all development is happening there now.

To port code:

  • TWSClient has moved to ib_insync.client.Client (with much improvements);
  • TWSClientQt: The new Client can be used with PyQt and quamash.

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, 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.QEventLoop.WaitForMoreEvents)
    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.7

  • HistRequester fix for endDateTime formatting

Version 0.5.6

  • HistRequester updated to version 9.73.04 of the API

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 <[email protected]>
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].