I'm looking to create an algorithm that returns a list of companies that meet the following 5 criteria:
1. 20% revenue growth over the next 3 years
2. Cash > short term debt
3. PEG ratio not above 1.5
4. Within 3% of the 50dma or 200dma
5. Free cash flow positive
I've completed the boot camp and have been following the tutorials on universe selection but I'm new to all of this. I'm finding plenty of help with returning a list sorted by some value (dollar volume for example) but I need a list of companies that meet these criteria not a ranked list.
Here's the code I edited from the Stock Selection Strategy Based On Fundamental Factors Algorithm found in the strategy library. I would include a backtest but it won't even build. The issue is clearly in the FineSelectionFunction().
class StockSelectionStrategyBasedOnFundamentalFactorsAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2009, 1, 2) # Set Start Date
self.SetEndDate(2017, 5, 2) # Set End Date
self.SetCash(50000) # Set Strategy Cash
self.current_month = -1
self.coarse_count = 300
self.fine_count = 10
self.AddUniverse(self.CoarseSelectionFunction, self.FineSelectionFunction)
self.SetAlpha(ConstantAlphaModel(InsightType.Price, InsightDirection.Up, timedelta(30)))
self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel(lambda time:None))
def CoarseSelectionFunction(self, coarse):
if self.current_month == self.Time.month:
return Universe.Unchanged
self.current_month = self.Time.month
sortedByDollarVolume = sorted([x for x in coarse if x.HasFundamentalData],
key=lambda x: x.DollarVolume, reverse=True)[:self.coarse_count]
return [i.Symbol for i in sortedByDollarVolume]
def FineSelectionFunction(self, fine):
fine = [x for x in fine if x.FinancialStatements.BalanceSheet.Cash.OneMonth > x.FinancialStatements.BalanceSheet.CurrentLiabilities.OneMonth
and x.ValuationRatios.PEGRatio < 1.5
and x.FinancialStatements.CashFlowStatement.FreeCashFlow.OneMonth > 0
and x.ValuationRatios.SecondYearEstimatedEPSGrowth > 0.12
sortedByfactor1 = sorted(fine, key=lambda x: x.FinancialStatements.BalanceSheet.Cash.OneMonth > x.FinancialStatements.BalanceSheet.CurrentLiabilities.OneMonth)
sortedByfactor2 = sorted(fine, key=lambda x: x.ValuationRatios.PEGRatio < 1.5)
sortedByfactor3 = sorted(fine, key=lambda x: x.FinancialStatements.CashFlowStatement.FreeCashFlow.OneMonth > 0)
sortedByfactor4 = sorted(fine, key=lambda x: x.ValuationRatios.SecondYearEstimatedEPSGrowth > 0.12)
stock_dict = {}
for rank1, ele in enumerate(sortedByfactor1):
rank2 = sortedByfactor2.index(ele)
rank3 = sortedByfactor3.index(ele)
rank4 = sortedByfactor4.index(ele)
stock_dict[ele] = rank1 + rank2 + rank3 + rank4
sorted_stock = sorted(stock_dict.items(),
key=lambda d:d[1], reverse=True)[:self.fine_count]
return [x[0].Symbol for x in sorted_stock]
Any help with this would be greatly appreciated. I think these are good criteria to trade on.
Shile Wen
Hi Tom,
The issue is that line 36 was missing the closing square bracket for the list comprehension. Furthermore, it appears the filter criteria in lines 33-36 is too strict, and thus doesn't result in anything in the list, which means no stocks will be invested in, so I'd suggest loosening the filters.
Best,
Shile Wen
Tom Penrose
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!