From f746bca4c4a5accbf868b4d270b4ce62e3a8fb07 Mon Sep 17 00:00:00 2001 From: moshferatu Date: Mon, 19 Feb 2024 12:06:01 -0800 Subject: [PATCH] Add helper method to submit option spread orders with a limit price --- ibkr/client.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/ibkr/client.py b/ibkr/client.py index 9a2d514..1160905 100644 --- a/ibkr/client.py +++ b/ibkr/client.py @@ -10,6 +10,7 @@ from .currency import USD from .exchange import SMART from .market_data_type import LIVE from .option_leg import OptionLeg +from .option_order import OptionOrder from .order_action import BUY, SELL class Client: @@ -125,6 +126,22 @@ class Client: return self.ib.placeOrder(combo_contract, combo_order) + def submit_spread_order(self, short_leg: OptionLeg, long_leg: OptionLeg) -> OptionOrder: + short_leg_data = self.get_market_data(self.get_option_contract(short_leg)) + long_leg_data = self.get_market_data(self.get_option_contract(long_leg)) + + short_leg_mid = (short_leg_data.ask + max(short_leg_data.bid, 0)) / 2.0 + long_leg_mid = (long_leg_data.ask + max(long_leg_data.bid, 0)) / 2.0 + + spread_mid = short_leg_mid - long_leg_mid + spread_limit = (spread_mid - 0.25) - (spread_mid % 0.05) + + spread_order = self.submit_combo_option_order([short_leg, long_leg], 1, limit_price = spread_limit) + while not spread_order.isDone(): + self.ib.waitOnUpdate() + + return OptionOrder(spread_order) + def submit_stop_loss_order(self, trade: Trade, stop_loss_price: float, limit_price: float = None) -> Trade: stop_loss_order = Order() stop_loss_order.action = SELL if trade.order.action == BUY else BUY