Can you add stop loss logic to my code , that is the unrealized losses are greater than the credit recived then the option will close
from AlgorithmImports import *
class BullCreditSpread(QCAlgorithm):
def initialize(self):
self.set_start_date(2020, 1, 1)
self.set_end_date(2023, 1, 1)
self.set_cash(5000)
self.spy = self.add_equity("SPY", Resolution.HOUR)
self.spy_option = self.add_option("SPY")
self.spy_option.set_filter(self.weekly_filter)
self.sma = self.sma("SPY", 200, Resolution.HOUR)
self.has_position = False
self.spy_price = None # Initialize spy_price
self.schedule.on(self.date_rules.every(DayOfWeek.MONDAY), self.time_rules.at(10, 0), self.trade_options)
def weekly_filter(self, universe):
return universe.include_weeklys().strikes(-5, 5).expiration(0, 7)
def on_data(self, slice):
if not self.sma.is_ready:
return
if self.securities.contains_key("SPY"):
self.spy_price = self.securities["SPY"].price
def trade_options(self):
if self.spy_price is None: # Check if spy_price is set
return
if self.spy_price > self.sma.current.value:
chain = self.current_slice.option_chains.get(self.spy_option.Symbol)
if chain is None:
return
contracts = sorted(chain, key=lambda x: abs(x.greeks.delta - 0.35))
if len(contracts) == 0:
return
short_option = contracts[0]
long_options = [x for x in chain if x.strike < short_option.strike and x.expiry == short_option.expiry]
if not long_options:
return
long_option = sorted(long_options, key=lambda x: x.strike, reverse=True)[0]
if not self.has_position:
spread = OptionStrategies.bull_put_spread(self.spy_option.Symbol, short_option.strike, long_option.strike, short_option.expiry)
self.buy(spread, 1)
self.has_position = True
def on_order_event(self, orderEvent):
if orderEvent.status == OrderStatus.FILLED:
self.has_position = False
Mia Alissi
Sources: Index Option Bull Put Spread Algorithm, Index Option Bull Call Spread Algorithm, Index Option Bear Put Spread Algorithm, Bull Call Spread - QuantConnect.com, Bull Put Spread - QuantConnect.com
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Warren chapman
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!