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.
def Initialize(self):
self.SetStartDate(2020, 1, 1)
self.SetEndDate(2023, 9, 15)
self.SetCash(3000)
self.SettingsFreePortfolioValuePercentage = 10
self.SetExecution(ImmediateExecutionModel())
self.symbol = self.AddEquity("SPY", Resolution.Daily, dataNormalizationMode=DataNormalizationMode.Raw).Symbol
self.SetWarmUp(timedelta(7))
def OnData(self, data):
if self.IsWarmingUp: return
else:
quantity = self.CalculateOrderQuantity(self.symbol, 0.4)
trade_bars = self.History[TradeBar](self.symbol, 7)
self.close_window = RollingWindow[float](7)
self.close_window.Add(data[self.symbol].Close)
self.close_window.Size = 5
if not self.close_window.IsReady: return
else:
current_close = self.close_window[0]
oldest_close = self.close_window[self.close_window.Count-1]
if not self.Portfolio.Invested: self.MarketOrder(self.symbol, quantity)
Mia Alissi
It seems like you're trying to use a
RollingWindow
to store the last 7 days of closing prices for "SPY". However, there are a few issues with your code that might be causing the no-trade situation.You're initializing the
RollingWindow
inside theOnData
method. This means that every time new data comes in, a newRollingWindow
is created, effectively resetting it. You should initialize theRollingWindow
in theInitialize
method instead.You're adding data to the
RollingWindow
before checking if it's ready. This means that theRollingWindow
will never be ready because it's always being updated with new data before the check.You're setting the
RollingWindow
size to 5 after adding data to it. This will remove the oldest data points if there are more than 5, which might not be what you want.Here's how you can fix these issues:
This should ensure that the
RollingWindow
is properly initialized and filled with data before you start trading.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.
Pj Kenedy
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!