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. 

  1. from AlgorithmImports import *
  2. class BullPutSpreadStrategy(QCAlgorithm):
  3. def Initialize(self) -> None:
  4. self.SetStartDate(2023, 1, 1)
  5. self.SetEndDate(2023, 12, 1)
  6. self.SetCash(50000)
  7. option = self.AddOption("SPY", Resolution.Minute)
  8. self.symbol = option.Symbol
  9. option.SetFilter(self.UniverseFunc)
  10. # Initialize variables for Stop Loss logic
  11. self.credit_received = 0.0
  12. self.current_option_strategy = None
  13. def UniverseFunc(self, universe: OptionFilterUniverse) -> OptionFilterUniverse:
  14. # return universe.IncludeWeeklys().Strikes(-15, 15).Expiration(timedelta(days=1), timedelta(days=7))
  15. return universe.IncludeWeeklys().Strikes(-1, 1).Expiration(timedelta(days=1), timedelta(days=7))
  16. def OnData(self, slice: Slice) -> None:
  17. if self.Portfolio.Invested: return
  18. # Get the OptionChain
  19. chain = slice.OptionChains.get(self.symbol, None)
  20. if not chain: return
  21. # Get the furthest expiration date of the contracts
  22. # expiry = sorted(chain, key=lambda x: x.Expiry, reverse=True)[0].Expiry
  23. # expiry = [option for option in chain if option.Expiry <= Expiry.EndOfWeek(self.Time)]
  24. expiry = [option for option in chain if option.Expiry <= Expiry.EndOfWeek(self.Time)]
  25. # Select the put Option contracts with the furthest expiry
  26. puts = [i for i in chain if i.Expiry == expiry and i.Right == OptionRight.Put]
  27. if len(puts) == 0: return
  28. # Select the ITM and OTM contract strikes from the remaining contracts
  29. put_strikes = sorted([x.Strike for x in puts])
  30. lower_otm_strike = put_strikes[0]
  31. higher_otm_strike = put_strikes[-1]
  32. option_strategy = OptionStrategies.BullPutSpread(self.symbol, higher_otm_strike, lower_otm_strike, expiry)
  33. # Check if a new trade is entered and update credit received and option strategy
  34. if self.current_option_strategy is None:
  35. self.credit_received = self.Securities[self.symbol].Holdings.UnrealizedProfit
  36. self.current_option_strategy = option_strategy
  37. self.Buy(option_strategy, 1)
  38. else:
  39. # Check the Stop Loss condition and take necessary actions
  40. self.CheckStopLoss()
  41. def CheckStopLoss(self):
  42. # Calculate the current floating loss
  43. current_float_loss = self.Securities[self.symbol].Holdings.UnrealizedProfit - self.credit_received
  44. # Check if the floating loss exceeds 2.38 times the credit received
  45. if current_float_loss <= -1.5 * self.credit_received:
  46. # Close the current option strategy trade
  47. self.Liquidate(self.current_option_strategy)
  48. # Reset the credit received
  49. self.credit_received = 0.0
  50. # Re-enter the trade based on the same rules
  51. # (Add the logic here to set 'self.current_option_strategy' similar to the existing code)
  52. self.Buy(self.current_option_strategy, 1)
+ Expand

Author

Warren chapman

October 2024