Recommended approach for adding custom tasks & events that work with ib_async #36
-
With ib_async running asyncio underneath, and me being a noob with asyncio, it leaves me unsure as to what the recommended approach is for extending the asyncio event loop in ib_async to handle custom tasks / events / callbacks for non-IB data feeds. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
A correct, but probably not as useful as you want, answer is "learn how everything works together" by trying different approaches until you see a dozen different ways how everything can break and the three ways everything works. For the most flexibility and performance, the library should be used in an async-native fashion with all the The general pattern with python asyncio is you use import asyncio
def main():
async def start():
await asyncio.sleep(1)
asyncio.run(start()) So then we end up with things like this (just an example structure, it's cleaner wrapping all this logic in proper classes for real usage): import asyncio
from ib_async import IB
def main():
async def start():
ib = IB()
# attach callback event handlers as needed outside of the reconnect loop
async def connect():
await ib.connectAsync(host, port, clientId)
await ib.reqAccountSummaryAsync()
await connect()
# Once your program reaches here, you can do anything else you want.
# You can use websockets, httpx, prompt-toolkit, any of the other ib_async interfaces, etc.
# Just note your program will _exit_ when this `start()` task returns, so you need _something_
# either running, waiting on events or network data, or just sleeping to stop from exiting.
host, port, clientId = ...
asyncio.run(start(host, port, clientId)) Then it's up to you to figure out obviously what the system does and more importantly how to make the system interactive. Do you need to modify parameters (symbols, features, algos, timeframes, durations) while your system is running? Do you need to enable or disable features when the platform is running? Or do you want everything to just run in a loop forever when running until the program exits? I've also found it useful to run multiple The main problems to watch out for:
These are good resources to fully read through and experiment with to figure out how python async systems can work together:
|
Beta Was this translation helpful? Give feedback.
A correct, but probably not as useful as you want, answer is "learn how everything works together" by trying different approaches until you see a dozen different ways how everything can break and the three ways everything works.
For the most flexibility and performance, the library should be used in an async-native fashion with all the
*Async()
methods you manually call usingawait ib.connectAsync()
etc (or it can use async "behind the scenes" transparently with justib.connect()
but then the library starts and owns the event loop itself and it's more difficult to fully interoperate other async services with).The general pattern with python asyncio is you use
asyncio.run()
to start the e…