def Initialize(self):
etf_tickers = ["XLB","XLE","XLF","XLI","XLK","XLP","XLU","XLV","XLY"]
for ticker in etf_tickers:
symbol = self.AddEquity(ticker, Resolution.Daily).Symbol
self.Securities[symbol].SetDataNormalizationMode(DataNormalizationMode.TotalReturn)
self.Consolidate(symbol, Calendar.Monthly, self.CalendarTradeBarHandler)
self.Securities[symbol].SetLeverage(1.0) #Leverage is set to 1 to ensure no margin used
historydate = datetime.date(datetime.now()) - datetime.date(datetime(1998,12,23))
self.history_days = int(historydate.days)+1
self.history_df = self.History(etf_tickers,timedelta(days=self.history_days),Resolution.Daily)
tester = True
while tester == True:
if self.history_df.empty == True:
self.history_df = self.History(self.Securities.Keys,timedelta(days=self.history_days),Resolution.Daily)
elif self.history_df.empty == False:
tester=False
Hi guys,
I'm having trouble with self.History. For some reason, the below code does not return any values/dataframe
self.history_df = self.History(etf_tickers,timedelta(days=self.history_days),Resolution.Daily)
The while loop never stops, which implies that the dataframe is empty.
Is there anything wrong with the way I'm using self.History?
Nathan Miller
Try putting brackets around your symbol when you use the self.History function. I've had the same issue and it has always seemed to be solved by adding these brakcets. It would look like:
self.history_df = self.History([etf_tickers],timedelta(days=self.history_days),Resolution.Daily)
If this doesn't work, you could loop through your tickers to create a single dataframe for each ticker and store those dataframes in a dictionary. For example,
etf_tickers = ["XLB","XLE","XLF","XLI","XLK","XLP","XLU","XLV","XLY"] df = {} for ticker in etf_tickers: df[ticker] = self.History([ticker], timedelta(days=self.history_days),Resolution.Daily)
You also may need to use the self.History function outside of the initialize function in OnData() . Not sure if this is necessary but just another idea.
Jason
Unfortunately, none of the above work.... Very weird.
Derek Melchin
Hi Jason,
To resolve this, we just need to add start and end dates to the algorithm.
def Initialize(self): self.SetStartDate(2021, 1, 1) self.SetEndDate(2021, 1, 6)
See the attached backtest for reference.
Best,
Derek Melchin
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.
Jason
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!