The program will not liquidate after the window has initialized it will only buy and hold. What am I missing?
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))
self.close_window = RollingWindow[float](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.Size = 7
if not self.close_window.IsReady:
self.close_window.Add(data[self.symbol].Close)
else:
current_close = self.close_window[0]
oldest_close = self.close_window[self.close_window.Count-1]
if current_close > oldest_close:
if not self.Portfolio.Invested: self.MarketOrder(self.symbol, quantity)
elif current_close < oldest_close:
if self.Portfolio.Invested: self.Liquidate(self.symbol)
Mia Alissi
The issue seems to be with the way you're handling the
RollingWindow
object. You're setting the size of theRollingWindow
inside theOnData
method, which is not the correct way to use it. The size of theRollingWindow
should be set when it's initialized and then you just add data to it.Also, you're adding data to the
RollingWindow
only when it's not ready. This means that after it becomes ready, you stop adding new data to it. This could be why your liquidation condition is never met, as theRollingWindow
is not being updated with the latest data.Here's how you should modify your code:
In this modified code, the
RollingWindow
is updated with new data at every time step, and the trading decisions are made only when theRollingWindow
is ready. This should ensure that your liquidation condition is checked correctly.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!