Voyz / ibind

IBind is a REST and WebSocket client library for Interactive Brokers Client Portal Web API.
Apache License 2.0
84 stars 8 forks source link

Asyncio #17

Open zdytch opened 1 month ago

zdytch commented 1 month ago

First, thank you Voy and other contributors, for the huge work you do on this project. I've been fighting against working with IB API for the last 4 years and find it... hmmm... (let's use a kind word here) "unfriendly". Actually, an IB's developer team is supposed to write such a wrapper for their customers, but they'd rather not.

My question is, why don't you start it as an asyncio project? From my experience, trading strategies are always doing so much stuff at the same time, reading inputs from different sources, placing orders, checking consistency, etc. So having non-blocking requests is important in this domain. Again, running a websocket client in the same thread with asyncio, looks better for me.

Maybe I don't understand something? Or maybe you already have asyncio in the roadmap?

Voyz commented 1 month ago

Hey @zdytch many thanks for checking out IBind and for your kind words.

My question is, why don't you start it as an asyncio project? From my experience, trading strategies are always doing so much stuff at the same time, reading inputs from different sources, placing orders, checking consistency, etc.

Good question! IBind could totally be written with asyncio. And the short answer is: I personally dislike it, I use multithreading.

I ran a few projects with it and ended up reverting back to threading due to complexity. This was a few years back, but from what I remember I had issues with exactly what you describe - needing for things to happen in parallel, bit by bit. Asyncio brought in an unnecessary amount of complexity when it came to different modules depending on each other - I recall code getting stuck a lot. Possibly there was an elegant solution to my issues at the time, but failing to find one after a good while trying, I decided to stick to threading which was clear to me. I've been running trading projects using threading since then without such issues, and threading speed was never a bottleneck I needed to look into. If it was I'm sure I'd have revisited asyncio.

When it comes to a project like IBind, I can see how asyncio would not cause the issues that I encountered. Not being fluent in using it though, I decided to stick to the tech I knew.

Both REST and WebSocket can be elegantly run with multithreading. I published a separate Python library called SuperLoops that I use for managing WebSockets using threading.

So yeah, not currently on the roadmap sadly - although if you think it could be integrated into IBind easily, then let me know!