Set the limit price based on the order action (BUY / SELL) of the nearest leg

This commit is contained in:
moshferatu 2024-02-20 07:48:42 -08:00
parent 83a008f981
commit 7c9837f3d2

View File

@ -135,7 +135,9 @@ class Client:
def submit_option_order(self, leg: OptionLeg) -> OptionOrder: def submit_option_order(self, leg: OptionLeg) -> OptionOrder:
leg_data = self.get_market_data(self.get_option_contract(leg)) leg_data = self.get_market_data(self.get_option_contract(leg))
leg_mid = (leg_data.ask + max(leg_data.bid, 0)) / 2.0 leg_mid = (leg_data.ask + max(leg_data.bid, 0)) / 2.0
leg_limit = (leg_mid - 0.25) - (leg_mid % 0.05) leg_limit = (leg_mid - 0.25) - (leg_mid % 0.05)
leg_limit = leg_limit if leg.action == BUY else -leg_limit
option_order = self.submit_single_option_order(leg_data, limit_price = leg_limit) option_order = self.submit_single_option_order(leg_data, limit_price = leg_limit)
while not option_order.isDone(): while not option_order.isDone():
@ -143,17 +145,18 @@ class Client:
return OptionOrder(option_order) return OptionOrder(option_order)
def submit_spread_order(self, short_leg: OptionLeg, long_leg: OptionLeg) -> OptionOrder: def submit_spread_order(self, near_leg: OptionLeg, far_leg: OptionLeg) -> OptionOrder:
short_leg_data = self.get_market_data(self.get_option_contract(short_leg)) near_leg_data = self.get_market_data(self.get_option_contract(near_leg))
long_leg_data = self.get_market_data(self.get_option_contract(long_leg)) far_leg_data = self.get_market_data(self.get_option_contract(far_leg))
short_leg_mid = (short_leg_data.ask + max(short_leg_data.bid, 0)) / 2.0 near_leg_mid = (near_leg_data.ask + max(near_leg_data.bid, 0)) / 2.0
long_leg_mid = (long_leg_data.ask + max(long_leg_data.bid, 0)) / 2.0 far_leg_mid = (far_leg_data.ask + max(far_leg_data.bid, 0)) / 2.0
spread_mid = near_leg_mid - far_leg_mid
spread_mid = short_leg_mid - long_leg_mid
spread_limit = (spread_mid - 0.25) - (spread_mid % 0.05) spread_limit = (spread_mid - 0.25) - (spread_mid % 0.05)
spread_limit = spread_limit if near_leg.action == BUY else -spread_limit
spread_order = self.submit_combo_option_order([short_leg, long_leg], limit_price = spread_limit) spread_order = self.submit_combo_option_order([near_leg, far_leg], limit_price = spread_limit)
while not spread_order.isDone(): while not spread_order.isDone():
self.ib.waitOnUpdate() self.ib.waitOnUpdate()