In the QuantConnect Research notebook, I used qb.GetFundamental to get the fundamental data for a list of stocks. But in the algorithm framework, it looks like we can only get those data as a FineFundamental object, and I guess the data which comes from as a FineFundamental object is set to be the newest data? (For example, if I do
x.ValuationRatios.PERatio in where x is a FineFundamental object, then the peRaio would be the newest one, not the historical data that I'm looking for)Is there any way we can get a dataframe of historical fundamental data just like the Research notebook(the attached screenshot)?
Thanks,
Eric
Link Liang
Hi Xiyuan,
Currently we don’t support direct access to historical fundamental data. However, we could implement Rolling Window to store and use fundamental data.
Here is an example using the average PE ratio in past 90 days for fine selection. We build a rolling window for each symbol with a size of 90. Everyday we add the newest PE ratio into the rolling window, and take the average of the rolling window. Notice that in the first 90 days, the rolling window is not filled, so the average is taken from the start date to the current date (you cannot access data before start date). After 90 days, the rolling window only contains data from the most recent 90 days, and works as expected. You might find more help about rolling window here.
You can always change the size of rolling window and the way it is updated for your own purpose. In addition, we are working on historical fundamental data access support. Hope it helps!
class OptimizedHorizontalCoreWave(QCAlgorithm): def Initialize(self): self.SetStartDate(2018, 10, 11) # Set Start Date self.SetEndDate(2019, 10, 10) # Set End Date self.SetCash(100000) # Set Strategy Cash self.SetUniverseSelection(FineFundamentalUniverseSelectionModel(self.CoarseSelectionFunction, self.FineSelectionFunction, None, None)) self.historicalPERatio = {} self.historicalPERatioAverage = {} self.rollingWindowSize = 90; # sort the data by daily dollar volume and take the top 'NumberOfSymbols' def CoarseSelectionFunction(self, coarse): # sort descending by daily dollar volume sortedByDollarVolume = sorted(coarse, key=lambda x: x.DollarVolume, reverse=True) # return the symbol objects of the top entries from our sorted collection return [ x.Symbol for x in sortedByDollarVolume[:1000] ] # sort the data by P/E ratio and take the top 'NumberOfSymbolsFine' def FineSelectionFunction(self, fine): for x in fine: if x.Symbol not in self.historicalPERatio.keys(): # Set up rolling window for new ticker self.historicalPERatio[x.Symbol] = RollingWindow[Decimal](self.rollingWindowSize) # Add PERatio to rolling window self.historicalPERatio[x.Symbol].Add(x.ValuationRatios.PERatio) # Take average of RW average = 0.0 for pe in self.historicalPERatio[x.Symbol]: average += pe average /= self.historicalPERatio[x.Symbol].Count self.historicalPERatioAverage[x.Symbol] = average # sort descending by average P/E ratio sortedByPeRatio = sorted(fine, key=lambda x: self.historicalPERatioAverage[x.Symbol], reverse=True) # take the top 5 entries from our sorted collection return [ x.Symbol for x in sortedByPeRatio[:5] ]
Xiyuan Liu
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!