I would like to use fundamentals data for purposes other than selecting stocks (ie as inputs in ML). I see in the docs how to select stocks based on fundamentals, but not how to access fundamental values. In my testing, Securities["SPY"].Fundamentals are always null.
For example, inside a schedule function:
Securities["SPY"].Fundamentals.ValuationRatios.PERatio
...throws a nullreferenceexeption. Is there some other step I need to take to populate Fundamentals in this scenario?
Alexandre Catarino
Hi Daniel,
The Fundamentals member in the Security object is only set in a Universe Selection algorithm.
In this case, do you have
AddUniverse(CourseGrainSelector, FineGrainSelector)
in Initialize?
Could please attach an algorithm that reproduced the issue?
Note: comment out this statement Securities["SPY"].Fundamentals.ValuationRatios.PERatio
I am assuming that the Security.Fundamentals object is null for every security, because SPY, being an ETF, does not have Fine Fundamental data.
Daniel Root
Thanks for the reply. I understand I can access fundamentals inside of a Course or Fine selector, but I am wanting to access them in a different context- inside a Schedule.On or OnData- so that I can use the latest fundamental data as-of a given day in the algorithm. Below is a sample. I've commented out " //(double)spy.Fundamentals.ValuationRatios.PERatio" because it throws a nullreferenceexception. In this case, I'd like to get the latest PE Ratio of SPY on each date so that I can use it as a signal. I understand some fundamentals may not apply to ETFs, but they do have P/E Ratio, right?
Alexandre Catarino
Hi Daniel,
We need to include a AddUniverse call to pull the Fundamental data.
In the following example, I am only getting data from MSFT (PERatio was always zero for SPY).
// Define the symbol private Symbol _msft = QuantConnect.Symbol.Create("MSFT", SecurityType.Equity, Market.USA); // In Initialize AddUniverse(coarse => new [] { _msft }, fine => new [] { _msft }); // In OnData Security msft; if (Securities.TryGetValue(_msft, out msft)) { var f = msft.Fundamentals; if (f != null && f.ValuationRatios.PERatio > 0) Log($"{Time} :: {f.ValuationRatios.PERatio}"); }
The algorithm you shared is trying to get Fundamentals in Initialize. This procedure will not work since we need at least one data iteration to set up that object.
Daniel Root
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!