Overall Statistics
Total Trades
64
Average Win
13.28%
Average Loss
-3.21%
Compounding Annual Return
31.607%
Drawdown
35.300%
Expectancy
1.474
Net Profit
161.592%
Sharpe Ratio
0.969
Loss Rate
52%
Win Rate
48%
Profit-Loss Ratio
4.14
Alpha
0.255
Beta
0.576
Annual Standard Deviation
0.341
Annual Variance
0.116
Information Ratio
0.59
Tracking Error
0.337
Treynor Ratio
0.574
Total Fees
$772.00
# https://quantpedia.com/Screener/Details/25

class SmallCapInvestmentAlgorithm(QCAlgorithm):

    def Initialize(self):

        self.SetStartDate(2016, 1, 1)
        self.SetEndDate(2019, 7, 1)
        self.SetCash(100000)

        self.UniverseSettings.Resolution = Resolution.Daily
        self.count = 10

        self.year = -1
        self.symbols = []
        self.AddUniverse(self.CoarseSelectionFunction, self.FineSelectionFunction)


    def CoarseSelectionFunction(self, coarse):
        ''' Drop stocks which have no fundamental data or have low price '''

        if self.year == self.Time.year:
            return self.symbols

        return [x.Symbol for x in coarse if x.HasFundamentalData and x.Price > 5]
 

    def FineSelectionFunction(self, fine):
        ''' Selects the stocks by lowest market cap '''

        if self.year == self.Time.year:
            return self.symbols
        
        market_cap = {}
        
        # Calculate the market cap and add the "MarketCap" property to fine universe object
        for i in fine:
            market_cap[i] = (i.EarningReports.BasicAverageShares.ThreeMonths *
                           i.EarningReports.BasicEPS.TwelveMonths *
                           i.ValuationRatios.PERatio)

        sorted_market_cap = sorted([x for x in fine if market_cap[x] > 0], key=lambda x: market_cap[x])

        self.symbols = [i.Symbol for i in sorted_market_cap[:self.count]]
        return self.symbols


    def OnData(self, data):

        if self.year == self.Time.year:
            return

        self.year = self.Time.year

        for symbol in self.symbols:
            self.SetHoldings(symbol, 1.0/self.count)


    def OnSecuritiesChanged(self, changes):
        ''' Liquidate the securities that were removed from the universe '''

        for security in changes.RemovedSecurities:
            symbol = security.Symbol
            if self.Portfolio[symbol].Invested:
                self.Liquidate(symbol)