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