Back

Correct use of history function

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?

Update Backtest







0

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.


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.

1

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.

0

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?

1

Hi Arun Quant ,

I ran the algorithm from your code snippet and there is data in the history request (line 27).

1

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.


Thanks Alexandre and Adam! I need to debug some more.

0

Update Backtest





0

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.


Loading...

This discussion is closed