1. For some reason when I put in my RollingWindow it creates a no-trade situation but when I take it out the program will buy and hold.  I’ve watched the videos a few times and read the Historical Data articles, but I still can’t make this work for some reason. The back-test does not throw an error, so I'm confused about what I am missing.  Any help would be appreciated.
  2. def Initialize(self):
  3. self.SetStartDate(2020, 1, 1)
  4. self.SetEndDate(2023, 9, 15)
  5. self.SetCash(3000)
  6. self.SettingsFreePortfolioValuePercentage = 10
  7. self.SetExecution(ImmediateExecutionModel())
  8. self.symbol = self.AddEquity("SPY", Resolution.Daily, dataNormalizationMode=DataNormalizationMode.Raw).Symbol
  9. self.SetWarmUp(timedelta(7))
  10. def OnData(self, data):
  11. if self.IsWarmingUp: return
  12. else:
  13. quantity = self.CalculateOrderQuantity(self.symbol, 0.4)
  14. trade_bars = self.History[TradeBar](self.symbol, 7)
  15. self.close_window = RollingWindow[float](7)
  16. self.close_window.Add(data[self.symbol].Close)
  17. self.close_window.Size = 5
  18. if not self.close_window.IsReady: return
  19. else:
  20. current_close = self.close_window[0]
  21. oldest_close = self.close_window[self.close_window.Count-1]
  22. if not self.Portfolio.Invested: self.MarketOrder(self.symbol, quantity)
+ Expand

Author

Pj Kenedy

September 2023