From a7603b6963934741d9a65c8b52bc0da9b0eefa4a Mon Sep 17 00:00:00 2001 From: Ewald de Wit Date: Fri, 18 Sep 2020 19:49:38 +0200 Subject: [PATCH] Add async streaming ticks recipe. --- docs/recipes.rst | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/docs/recipes.rst b/docs/recipes.rst index c66501e6c..3040bb74f 100644 --- a/docs/recipes.rst +++ b/docs/recipes.rst @@ -169,6 +169,41 @@ Fundemental ratios ib.sleep(2) print(ticker.fundamentalRatios) +Async streaming ticks +^^^^^^^^^^^^^^^^^^^^^ + +.. code-block:: python + + import asyncio + + import ib_insync as ibi + + + class App: + + async def run(self): + self.ib = ibi.IB() + with await self.ib.connectAsync(): + contracts = [ + ibi.Stock(symbol, 'SMART', 'USD') + for symbol in ['AAPL', 'TSLA', 'AMD', 'INTC']] + for contract in contracts: + self.ib.reqMktData(contract) + + async for tickers in self.ib.pendingTickersEvent: + for ticker in tickers: + print(ticker) + + def stop(self): + self.ib.disconnect() + + + app = App() + try: + asyncio.run(app.run()) + except (KeyboardInterrupt, SystemExit): + app.stop() + Integration with PyQt5 or PySide2 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -197,4 +232,4 @@ Integration with Tkinter ^^^^^^^^^^^^^^^^^^^^^^^^ To integrate with the Tkinter event loop, take a look at -`this example app `_. \ No newline at end of file +`this example app `_.