Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Probabilistic Sharpe Ratio
0%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
-0.843
Tracking Error
0.154
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
class StockSelectionStrategyBasedOnFundamentalFactorsAlgorithm(QCAlgorithm):

    def Initialize(self):

        self.SetStartDate(2009, 1, 2)  # Set Start Date
        self.SetEndDate(2010, 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]
                               
        self.Plot('fine length', 'value', len(fine))
        
        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]