Hi guys, I'm trying to obtain the historical prices of the past 60 trading days for 20 symbols. I'm passing this list of symbols "self.trading_symbols" into self.history. However, I've noticed that the historical dataframe is not the output I'm expecting. Instead of giving me a dataframe with the shape (1200, 5), it either returns (1140, 5) or (1080, 5).
I've checked the length of self.trading_symbols as well and it clearly shows that there are 20 symbols in the list. I am at a lost as to what the problem may be and any help will be greatly appreciated, thank you!
from QuantConnect.Data.Custom.Tiingo import *
import pandas as pd
from itertools import chain
class TransdimensionalTachyonCompensator(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 1, 1) # Set Start Date
self.SetEndDate(2020, 11, 11)
self.SetCash(100000) # Set Strategy Cash
self.AddUniverse(self.CoarseSelectionFunction)
self.UniverseSettings.Resolution = Resolution.Daily
self.spy = self.AddEquity("SPY", Resolution.Daily).Symbol
self.trading_symbols = []
self.Train(self.DateRules.MonthEnd(), self.TimeRules.AfterMarketOpen(self.spy, 0), Action(self.train))
self.Schedule.On(self.DateRules.MonthStart(), self.TimeRules.AfterMarketOpen(self.spy, 0), Action(self.rebalance))
def CoarseSelectionFunction(self, coarse):
sortedByDollarVolume = sorted(coarse, key=lambda x: x.DollarVolume, reverse=True)
self.filtered = [ x.Symbol for x in sortedByDollarVolume if x.HasFundamentalData ]
return self.filtered[:20]
def OnData(self, data):
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
Arguments:
data: Slice object keyed by symbol containing the stock data
'''
pass
def rebalance (self):
self.trading_symbols = [ (str(symbol).split(" ")[0]) for symbol in self.filtered[:20]]
def train(self):
if not self.trading_symbols: return
self.Debug(len(self.trading_symbols)) # gives a length of 20
for symbol in self.trading_symbols:
self.AddEquity(symbol, Resolution.Daily)
self.lookback_period = 60
history = self.History(self.trading_symbols , self.lookback_period , Resolution.Daily)
self.Debug(history.shape) # shows (1140, 5) or (1080, 5)
Derek Melchin
Hi Wei,
The DataFrame that is returned from the History method is smaller in cases where some of the Symbols don't have enough history to fill the lookback window.
Notice that in the algorithm below, the data resolution has been increased to the minute level. We always encourage doing this when using Scheduled Events or Train mid-day to avoid stale data. In addition, the universe selection model now only refreshed the universe on a monthly basis to match the training frequency.
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.
Wei li
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!