From c94919916fddc28374818598fcc3c4c31ff57050 Mon Sep 17 00:00:00 2001 From: moshferatu Date: Tue, 5 Dec 2023 05:47:22 -0800 Subject: [PATCH] Provide the ability to subscribe to market data updates in an event loop --- ibkr/client.py | 5 ++++- market_data_update_example.py | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 market_data_update_example.py diff --git a/ibkr/client.py b/ibkr/client.py index 9eb4c59..0231673 100644 --- a/ibkr/client.py +++ b/ibkr/client.py @@ -112,4 +112,7 @@ class Client: stop_loss_order.auxPrice = stop_loss_price stop_loss_order.totalQuantity = trade.order.totalQuantity stop_loss_order.transmit = True - return self.ib.placeOrder(trade.contract, stop_loss_order) \ No newline at end of file + return self.ib.placeOrder(trade.contract, stop_loss_order) + + def run_event_loop(self): + self.ib.run() \ No newline at end of file diff --git a/market_data_update_example.py b/market_data_update_example.py new file mode 100644 index 0000000..03f139d --- /dev/null +++ b/market_data_update_example.py @@ -0,0 +1,14 @@ +from datetime import datetime +from ib_insync import Option +from ibkr.client import Client +from ibkr.exchange import SMART + +client = Client(host = '127.0.0.1', port = 7497) + +expiration = datetime.now().strftime('%Y%m%d') +contract = Option('SPX', expiration, 4550.0, 'P', exchange = SMART, currency = 'USD') +contract.tradingClass = 'SPXW' +market_data = client.get_market_data(contract, streaming = True) + +market_data.updateEvent += lambda _: print(market_data.last) +client.run_event_loop() \ No newline at end of file