Hi, using the code below, I'm able to choose securities based on their fundamental data such as PERatios etc., but can only do so by hardcoding a specific value, like a PERatio of above 10. How do I go about making this value dynamic? Such as only wanting to select symbols whose PERatio is above a given benchmark, like say, SPY.
sortedByDollarVolume = sorted([x for x in fine if x.CompanyReference.CountryId == "USA" \
and x.CompanyReference.PrimaryExchangeID == "NAS" \
and x.CompanyReference.IndustryTemplateCode == "N" \
and (algorithm.Time - x.SecurityReference.IPODate).days > 180 \
and x.ValuationRatios.PERatio > 10], \
key = lambda x: self.dollarVolumeBySymbol[x.Symbol], reverse=True)
So instead of 10, that value would be whatever the PERatio of SPY would be when doing the fine selection.
Thank you
Derek Melchin
Hi Vncne,
We only have fundamental data for equities, not ETFs. So, for example, if we want to only have our universe consist of stocks with a PE Ratio greater than TSLA's, we can use
def FineSelectionFunction(self, fine): tsla_pe = None for f in fine: if f.Symbol == self.tsla: tsla_pe = f.ValuationRatios.PERatio if tsla_pe is None: return [] return [f.Symbol for f in fine if f.Symbol != self.tsla and f.ValuationRatios.PERatio > tsla_pe]
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.
Vncne
HiDerek Melchin I guess I meant something like getting the mean P/E Ratio of all companies within said benchmark. So I'd have to get the P/E Ratio of all companies contained in the S&P500 and use the mean of that as a dynamic P/E Ratio criteria for my fine selection. How do I apply that to the universe selection?
Derek Melchin
Hi Vncne,
This can be done by extending the QC500UniverseSelectionModel. We can determine the mean PE Ratio of the QC500 securities at the end of the SelectFine method. Refer to the source code here.
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.
Vncne
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!