Add helper method to submit option spread orders with a limit price

This commit is contained in:
moshferatu 2024-02-19 12:06:01 -08:00
parent 6d41d7c9e4
commit f746bca4c4

View File

@ -10,6 +10,7 @@ from .currency import USD
from .exchange import SMART from .exchange import SMART
from .market_data_type import LIVE from .market_data_type import LIVE
from .option_leg import OptionLeg from .option_leg import OptionLeg
from .option_order import OptionOrder
from .order_action import BUY, SELL from .order_action import BUY, SELL
class Client: class Client:
@ -125,6 +126,22 @@ class Client:
return self.ib.placeOrder(combo_contract, combo_order) 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: def submit_stop_loss_order(self, trade: Trade, stop_loss_price: float, limit_price: float = None) -> Trade:
stop_loss_order = Order() stop_loss_order = Order()
stop_loss_order.action = SELL if trade.order.action == BUY else BUY stop_loss_order.action = SELL if trade.order.action == BUY else BUY