Hi, I was working on a project and whenever I try to run it it says that I am referring to future data. What am I doing wrong? Thanks in advance. The code is posted below.
class TransdimensionalParticleRadiator(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2010, 1, 4) #start Date
self.SetEndDate(2020, 1, 4) # End Date
self.SetCash(100000) # Set Strategy Cash
self.AddEquity("VTI", Resolution.Daily) # Set Equity for different companies
self.AddEquity("IAU", Resolution.Daily)
self.AddEquity("VGLT", Resolution.Daily)
self.AddEquity("VGIT", Resolution.Daily)
self.AddEquity("PDBC", Resolution.Daily)
self.smavti = self.SMA("VTI", 305)
self.smavglt = self.SMA("VGLT", 305) # SMA for both vglt and vti
self.rebal = 4 # Rebalance every 2 weeks
self.rebalTimer = self.rebal - 1 # Initialize to trigger first week
self.flag1 = 0
self.Schedule.On(self.DateRules.On(2020, 10, 7), self.TimeRules.AfterMarketOpen("VTI", 150), self.Event) # set both events one for the first set holdings
self.Schedule.On(self.DateRules.WeekStart("VTI"), self.TimeRules.AfterMarketOpen("VTI", 150), self.Rebalance) # Set event for rebalance
def OnData(self, data):
if self.flag1 == 0:
if data["VTI"].Close < self.smavti.Current.Value: # Check for Vti data more than the SMA
self.SetHoldings("VTI", .50)
self.SetHoldings("VGLT", .30)
self.SetHoldings("VGIT", .15)
self.SetHoldings("IAU", .08)
self.SetHoldings("PDBC", .07)
elif data["VGLT"].Close < self.smavglt.Current.Value: # Check for Vti data more than the SMA
self.SetHoldings("VGLT", .50)
self.SetHoldings("VTI", .20)
self.SetHoldings("VGIT", .15)
self.SetHoldings("IAU", .08)
self.SetHoldings("PDBC", .07)
else:
pass
self.rebaltimer = 0 # Reset timer and flag
self.flag1 = 0
def Rebalance(self): # Define Rebalance
self.rebalTimer +=1
if self.rebalTimer == self.rebal:
self.flag1 = 1
def Event(self): # Define first event
self.SetHoldings("VTI", .30)
self.SetHoldings("VGLT", .50)
self.SetHoldings("VGIT", .15)
self.SetHoldings("IAU", .08)
self.SetHoldings("PDBC", .07)
Hector Barrio
Sometimes symbols will not be in the slice or not have data for many possible reasons, checking that there is indeed data avoids the error, and maybe alters the intent of the strategy and has to be managed somehow.
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!