Connect to IBKR in different environments
mainDepending on your execution environment, use the appropriate connection pattern:
Basic Script
Standard synchronous-style usage for scripts.
Jupyter Notebook
Requires calling util.startLoop() to handle the event loop in an interactive environment.
Async Application
Use await ib.connectAsync(...) within an asyncio event loop for high-performance asynchronous applications.
# Basic Script
from ib_async import *
ib = IB()
ib.connect('127.0.0.1', 7497, clientId=1)
# ... code ...
ib.disconnect()
# Jupyter Notebook
from ib_async import *
util.startLoop()
ib = IB()
ib.connect('127.0.0.1', 7497, clientId=1)
# Async Application
import asyncio
from ib_async import *
async def main():
ib = IB()
await ib.connectAsync('127.0.0.1', 7497, clientId=1)
# ... code ...
ib.disconnect()
asyncio.run(main())