I'm trying to get some fundamental data from the stocks, but the value returned is always zero. How do we have access to fundamental data, is that possible?
In the example below, the PB Ratio and Market Cap is always zero for every symbol.
class AlgorithmFundamentalsTest(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2023, 8, 1)
self.SetEndDate(2023, 8, 10)
self.AddUniverse(self.Select)
self.universe = {}
self.Schedule.On(
self.DateRules.EveryDay(),
self.TimeRules.At(hour=12),
self.LogFundamentals)
def OnData(self, data):
pass
def OnSecuritiesChanged(self, changes):
for s in changes.AddedSecurities:
if str(s.Symbol) not in self.universe:
self.universe[str(s.Symbol)] = s
for s in changes.RemovedSecurities:
if str(s.Symbol) in self.universe.keys():
self.universe.pop(str(s.Symbol))
def Select(self, symbols):
selected = [c for c in symbols if c.HasFundamentalData]
sorted_by_dollar_volume = sorted(selected, key=lambda c: c.DollarVolume, reverse=True)
return [ c.Symbol for c in sorted_by_dollar_volume[:10] ]
def LogFundamentals(self):
for symbol in self.universe.keys():
# The returned values here are always zero
self.Log('%s Market Cap: %.2f PB Ratio: %.2f' %
(symbol,
self.universe[symbol].Fundamentals.MarketCap,
self.universe[symbol].Fundamentals.ValuationRatios.PBRatio,
))
Mia Alissi
To access fundamental data in QuantConnect, you need to use the
FineFundamental
objects in your fine universe selection function or theFundamentals
property of theEquity
objects in your algorithm.In your code, you are using the
CoarseFundamental
object to select symbols, but you are not using theFineFundamental
object to filter and select the fundamental data.Here is an example of how to use the
FineFundamental
object to select symbols based on their P/E ratio:In this example, the
FineFundamental
object is used to sort the symbols by their P/E ratio and the top 5 symbols are selected.To access the fundamental data outside of your fine fundamental selection function, you can use the
Fundamentals
property of theEquity
objects in your algorithm:Please note that it's currently only possible to get historical fundamental data from the Research Environment. If there is no data in the period you request, the history result is empty.
For more information about accessing fundamental data, you can refer to the QuantConnect Documentation.
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.
Algo TBKL
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!