Can you help me code for weekly bull put spreads on SPY. Where a trade is only made when the stock is above the 200 period moving average. 

 

Then can the option spread sell the top leg at the strike that has the closest delta to .35. And then buy the strike right below the sold strike.

 

Then as edit my code so that the stop loss is triggered when, the trade is down by 150% of the profit (credit) received for the strategy. 

from AlgorithmImports import *

class BullPutSpreadStrategy(QCAlgorithm): 
   def Initialize(self) -> None:
       self.SetStartDate(2023, 1, 1)
       self.SetEndDate(2023, 12, 1)
       self.SetCash(50000)

       option = self.AddOption("SPY", Resolution.Minute)
       self.symbol = option.Symbol
       option.SetFilter(self.UniverseFunc)

       # Initialize variables for Stop Loss logic
       self.credit_received = 0.0
       self.current_option_strategy = None

   def UniverseFunc(self, universe: OptionFilterUniverse) -> OptionFilterUniverse:
       # return universe.IncludeWeeklys().Strikes(-15, 15).Expiration(timedelta(days=1), timedelta(days=7))
       return universe.IncludeWeeklys().Strikes(-1, 1).Expiration(timedelta(days=1), timedelta(days=7))
   def OnData(self, slice: Slice) -> None:
       if self.Portfolio.Invested: return

       # Get the OptionChain
       chain = slice.OptionChains.get(self.symbol, None)
       if not chain: return

       # Get the furthest expiration date of the contracts
       # expiry = sorted(chain, key=lambda x: x.Expiry, reverse=True)[0].Expiry

       # expiry = [option for option in chain if option.Expiry <= Expiry.EndOfWeek(self.Time)]
       expiry = [option for option in chain if option.Expiry <= Expiry.EndOfWeek(self.Time)]

       # Select the put Option contracts with the furthest expiry
       puts = [i for i in chain if i.Expiry == expiry and i.Right == OptionRight.Put]
       if len(puts) == 0: return

       # Select the ITM and OTM contract strikes from the remaining contracts
       put_strikes = sorted([x.Strike for x in puts])
       lower_otm_strike = put_strikes[0]
       higher_otm_strike = put_strikes[-1]
       option_strategy = OptionStrategies.BullPutSpread(self.symbol, higher_otm_strike, lower_otm_strike, expiry)

       # Check if a new trade is entered and update credit received and option strategy
       if self.current_option_strategy is None:
           self.credit_received = self.Securities[self.symbol].Holdings.UnrealizedProfit
           self.current_option_strategy = option_strategy
           self.Buy(option_strategy, 1)
       else:
           # Check the Stop Loss condition and take necessary actions
           self.CheckStopLoss()

   def CheckStopLoss(self):
       # Calculate the current floating loss
       current_float_loss = self.Securities[self.symbol].Holdings.UnrealizedProfit - self.credit_received

       # Check if the floating loss exceeds 2.38 times the credit received
       if current_float_loss <= -1.5 * self.credit_received:
           # Close the current option strategy trade
           self.Liquidate(self.current_option_strategy)

           # Reset the credit received
           self.credit_received = 0.0

           # Re-enter the trade based on the same rules
           # (Add the logic here to set 'self.current_option_strategy' similar to the existing code)
           self.Buy(self.current_option_strategy, 1)