I want to combine multiple filters in a universe selection. If I wanted to take top 10% and bottom 10%. Can the perentile function be used in fundamental universe selection aswell? And if so, how would that be done? Thank you.
QUANTCONNECT COMMUNITY
I want to combine multiple filters in a universe selection. If I wanted to take top 10% and bottom 10%. Can the perentile function be used in fundamental universe selection aswell? And if so, how would that be done? Thank you.
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.
Jing Wu
You can select the elements in the symbol list with the length.
def CoarseSelectionFunction(self, coarse): sorted = sorted(coarse, key=lambda x: x.DollarVolume, reverse=True) # top ten percent self.top = [i.Symbol for i in sorted[:int(0.1*len(sorted))]] # bottom ten percent self.bottom = [i.Symbol for i in sorted[-int(0.1*len(sorted)):]] return self.top + self.bottom
Michael
@Jing Wu I an odd syntax error on the return arguement,
class Fundamentals(QCAlgorithmFramework):
def Initialize(self):
# Set requested data resolution
self.UniverseSettings.Resolution = Resolution.Daily
self.AddUniverse(SelectFine)
...
def SelectFine(self, data, fine):
# The company's headquarter must in the U.S.
# The stock must be traded on either the NYSE or NASDAQ
# The stock's market cap must be greater than 500 million
selected = [x for x in fine if x.CompanyReference.CountryId == "USA"
and x.ValuationRatios.FCFYield > 0
and x.EarningReports.BasicAverageShares.ThreeMonths * \
x.EarningReports.BasicEPS.TwelveMonths * x.ValuationRatios.PERatio > 5e8]
filteredFine = sorted(selected, key=lambda x: x.DollarVolume, reverse=True)
self.top = [i.Symbol for i in filteredFine][:int(0.1*len(filteredFine))]
self.bottom = [i.Symbol for i in filteredFine[-int(0.1*len(filteredFine)):]
return self.bottom + self.top
Jing Wu
There's a typo in self.top
self.top = [i.Symbol for i in selected[:int(0.1*len(selected))]] self.bottom = [i.Symbol for i in selected[-int(0.1*len(selected)):]]
DollarVolume is the property in coarse selection function. To filter stocks with the fine selection function, you need to add a coarse selection function. For details, please see the documentation and the attached algorithm
Michael
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!