Package: tws-async
NoticeThis project has evolved intoIB-insyncand all development is happening there now.To port code:TWSClienthas moved toib_insync.client.Client(with much improvements);TWSClientQt: The newClientcan be used with PyQt and quamash.IntroductionThetws_asyncpackage allows the Python API from Interactive Brokers (IBAPI)
to be used asynchronously and single-threaded with theasynciostandard library or with thePyQt5framework.This offers a simpler, safer and more performant approach to concurrency than
multithreading.InstallationInstall using pip:pip3 install -U tws_asyncNote that on some systems thepip3command is justpip.Pythonversion 3.5 or higher is required as well as theInteractive Brokers Python API.UsageThis 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 fromibapi.wrapper.EWrapperand 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 standardEClient, and they also replace the infinite loop ofEClient.run()with an event loop.To simplify working with contracts, this package providesContract,Stock,Option,Future,Forex,Index,CFDandCommodityclasses that can be used anywhere where aibapi.contract.Contractis expected.
Examples of some simple cases areStock('AMD'),Forex('EURUSD'),CFD('IBUS30')orFuture('ES','201612', 'GLOBEX').
To specify more complex contracts, any property can be given as a keyword.To learn more, consult theofficial IBAPI documentationor have a look at
these sample use cases:Historical data downloaderTheHistRequesterdownloads historical data and saves it to CSV files;histrequester demoillustrates how to use it.Realtime streaming ticksThetick streamersubscribes to realtime tick data.Jupyter NotebookTo use the Interactive Brokers API fully interactively in a Jupyter notebook,
have a look at theexample notebook.Jupyter can be started with the commandjupyter notebook.This notebook uses the Qt version of the client, where the
Qt event loop is started with the%gui qt5directive at the very top.
It is not necessary to call therun()method of the client.Notes on using asycio in a notebookCurrently 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 thequamashadaptor. 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:%guiqt5importasyncio,quamashloop=quamash.QEventLoop()asyncio.set_event_loop(loop)One thing that does not work in the combination of quamash and Jupyter is theloop.run_until_finishedmethod. It can be patched like this:defrun_until_complete(self,future):future=asyncio.ensure_future(future)qApp=qt.QApplication.instance()whilenotfuture.done():qApp.processEvents(qt.QEventLoop.WaitForMoreEvents)returnfuture.result()quamash.QEventLoop.run_until_complete=run_until_completeThe asyncio version of the client relies onloop.run_until_finishedto 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).ChangelogVersion 0.5.7HistRequester fix for endDateTime formattingVersion 0.5.6HistRequester updated to version 9.73.04 of the APIVersion 0.5.5small simplificationsVersion 0.5.4connect() 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.3Added optionalasyncConnectargument toclient.connect()method. The default is now to connect synchronously (block until connected).Fixed bug in HistRequester when downloading daily data.Version 0.5.0Initial pip package release.Good luck and enjoy,author:Ewald de Wit  <ewald.de.wit@gmail.com>