diff --git a/ibkr/client.py b/ibkr/client.py index 1160905..acc86b1 100644 --- a/ibkr/client.py +++ b/ibkr/client.py @@ -80,20 +80,6 @@ class Client: self.ib.qualifyContracts(option_contract) return option_contract - def submit_option_order(self, leg: OptionLeg, quantity: int) -> Trade: - option_contract = self.get_option_contract(leg) - contract = Contract() - contract.conId = option_contract.conId - contract.exchange = SMART - - order = Order() - order.action = BUY - order.orderType = 'MKT' # TODO: Support limit orders. - order.totalQuantity = quantity - order.transmit = True - - return self.ib.placeOrder(contract, order) - def submit_combo_option_order(self, legs: List[OptionLeg], quantity: int, limit_price: float = None) -> Trade: combo_legs = [] for leg in legs: @@ -126,6 +112,37 @@ class Client: return self.ib.placeOrder(combo_contract, combo_order) + def submit_single_option_order(self, leg: OptionLeg, quantity: int, limit_price: float = None) -> Trade: + option_contract = self.get_option_contract(leg) + contract = Contract() + contract.conId = option_contract.conId + contract.exchange = SMART + + order = Order() + order.action = leg.action + + if limit_price is None: + order.orderType = 'MKT' + else: + order.orderType = 'LMT' + order.lmtPrice = limit_price + + order.totalQuantity = quantity + order.transmit = True + + return self.ib.placeOrder(contract, order) + + def submit_option_order(self, leg: OptionLeg) -> OptionOrder: + leg_data = self.get_market_data(self.get_option_contract(leg)) + leg_mid = (leg_data.ask + max(leg_data.bid, 0)) / 2.0 + leg_limit = (leg_mid - 0.25) - (leg_mid % 0.05) + + option_order = self.submit_single_option_order(leg_data, 1, limit_price = leg_limit) + while not option_order.isDone(): + self.ib.waitOnUpdate() + + return OptionOrder(option_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))