I'm making a mistake in in using the history function. Here's a code snippet that I'm trying
self.symbols = [self.AddEquity(ticker).Symbol
for ticker in ['SPY, 'GLD'] ]
self.lookback = 300
history = self.History(
self.symbols,
self.lookback,
Resolution.Daily).close.unstack(level=0)
The history object does'nt have any data. I'm copying this code from the sample BetaAlgorithm. Any suggestions on what I'm missing?
Adam W
Is history called within Initialize(self) ? I believe that is called only once, and maybe putting that into an Alpha Model or some scheduled function would work better.
Arun Quant
Thanks for the response.
Here's what I'm trying:
class BetaAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2015, 1, 1) # Set Start Date
self.SetEndDate(2015, 1, 2) # Set End Date
self.SetCash(100000) # Set Strategy Cash
self.symbols = [self.AddEquity(ticker).Symbol
for ticker in ['SPY', 'GLD'] ]
self.m = len(self.symbols)
# Benchmark
self.benchmark = Symbol.Create('SPY', SecurityType.Equity, Market.USA)
# Set number days to trace back
self.lookback = 300
# Schedule Event: trigger the event at the begining of each month.
self.Schedule.On(self.DateRules.MonthStart(self.symbols[0]),
self.TimeRules.AfterMarketOpen(self.symbols[0]),
self.Rebalance)
def Rebalance(self):
# Fetch the historical data to perform the linear regression
history = self.History(
self.symbols,
self.lookback,
Resolution.Daily).close.unstack(level=0)
prices = pd.DataFrame.ewm(history, span = self.lookback).as_matrix(self.symbols)
When I look at the history history object, its blank.
Adam W
The code should be working, so I think there might be something going on under the hood with how events are scheduled in Initialize. Can you try increasing the end date (say a week) and see if it is still empty?
Alexandre Catarino
Hi Arun Quant ,
I ran the algorithm from your code snippet and there is data in the history request (line 27).
Arun Quant
Thanks Alexandre and Adam! I need to debug some more.
Arun Quant
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!