Coder Social home page Coder Social logo

erdewit / tws_async Goto Github PK

View Code? Open in Web Editor NEW
82.0 82.0 40.0 293 KB

Make the Python IB API from Interactive Brokers run inside an event loop

License: The Unlicense

Python 100.00%
async-python interactivebrokers jupyter-notebook pyqt5 python-3 trading

tws_async's People

Contributors

az-z avatar erdewit avatar lionelyoung avatar pehowell avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tws_async's Issues

tws_async.py getting Connection lost

Thanks for this example. I was able to run the ipynb version with no issues but trying to run tws_async.py getting 'Connection lost'

On the IB Gateway Log it shows:
....
....
Start API message, ClientID=1
Ignoring API request 'jextend.a1' since API is not accepted.
Terminating async queue.
...
...

any advice would be great, thanks!

ERROR 0 504 Not connected

I'd like to just use the tws_async.py and asyncio. I ran tws.ipnb and it works. I then modified it by commenting out the %gui qt5 and changed the :
from tws_async_qt import TWSClient, iswrapper
tp
from tws_async import TWSClient, iswrapper
Now I get a :
ERROR:root:ERROR 0 504 Not connected
on the tws.reqHistoricalData' api call.

I was expecting it would work. I'm on a Mac OS X 12.10.5 , and anaconda3 with python 3.5.2 .
Am I missing something?

tws_async with concurrent.futures

@erdewit

Compliments for the code! Instead running one TWS connection with one thread, I'd like to have more than one TWS instance (each one able to pass orders and downloading market data independently); ideally running in different processes but at the same time able to pass results between processes by using one of the IPC solutions. I have tried, without success, three solutions:

  1. wrap the each TWS_async in one process with multiprocessing.Process, it works but it is impossible to pass the data from one process to another. I tried with Manager, Queue, Listener/Client, etc... it seems that each async event loop is ring-fenced.
  2. Then I tried using concurrent.futures with ProcessPoolExecutor but actually didn't open a new process. Instead, nevertheless the error, it runs the two different TWS requests in a unique thread (as expected from async). Of course, in this case passing data between the two TWSs is trivial.
  3. In addition, I need to connect one TWS only at specific time, so I tried to use the apscheduler.scheduler.asyncio, but without success.
    Let me know if you want to see the code for the cases above.
    Do you have an idea how to modify your code in order to able to launch a second TWS with concurrent.future in another process and to use apscheduler?
    Many Thanks in advance

Error when using latest version of tws

Hello I installed tws gateway the latest version and after installation when i run my bot i get this

Traceback (most recent call last): File "bottrader.py", line 1, in <module> from ib_insync import * File "/usr/local/lib/python3.5/dist-packages/ib_insync/__init__.py", line 14 f'Old version ({ibapi.__version__}) of ibapi package detected. '

What might be the issue?

Throttling Async Requests

I'm struggling to keep under the 100/req per second on reqMktData() snapshots. I have tried

from PyQt5 import QtCore

...
class TWS(TWSClient):

    def requestBulkMktData(self, contracts: [Contract]):
        for contract in contracts:
            nap = self.reqBucket.consume(1)
            reqId = self.getReqId()
            QtCore.QTimer.singleShot(nap, lambda: self.reqMktData(reqId, contract, '', True, False, []))

This is using a token bucket to throttle. Using time.sleep() between will definitely not work with any of the event loops.

I am using the QT implementation on ubuntu 16.04, using both a jupyter notebook and a plain execution.

Would be ideal to have some test case example

Hi Ewald,

Thanks a lot for sharing your implentation of the API with asyncio.

I'm quite new to asyncio, so it is a bit challenging to learn both about it and how to integrate it with the API.

Woult that be possible for you to post in the repo some simple test example of how to do a request (say for instance an historical data request) and another one to show how to do multiple requests?

Thank you,

Vincent

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.