Overall Statistics
Total Trades
48
Average Win
0.56%
Average Loss
-1.24%
Compounding Annual Return
8.565%
Drawdown
57.600%
Expectancy
-0.417
Net Profit
261.422%
Sharpe Ratio
0.465
Probabilistic Sharpe Ratio
1.006%
Loss Rate
60%
Win Rate
40%
Profit-Loss Ratio
0.46
Alpha
0.089
Beta
-0.057
Annual Standard Deviation
0.181
Annual Variance
0.033
Information Ratio
-0.012
Tracking Error
0.249
Treynor Ratio
-1.469
Total Fees
$70.81
# https://quantpedia.com/Screener/Details/14
class MomentumEffectAlgorithm(QCAlgorithm):

    def Initialize(self):

        self.SetStartDate(2004, 7, 1)  # Set Start Date
        self.SetEndDate(2020, 7, 1)    # Set Start Date       
        self.SetCash(100000)           # Set Strategy Cash

        self.UniverseSettings.Resolution = Resolution.Daily

        self.mom = {}           # Dict of Momentum indicator keyed by Symbol
        self.lookback = 40     # Momentum indicator lookback period
        self.num_coarse = 100   # Number of symbols selected at Coarse Selection
        self.num_fine = 50      # Number of symbols selected at Fine Selection
        self.num_long = 10       # Number of symbols with open positions
        
        self.month = -1
        self.rebalance = False

        self.AddUniverse(self.CoarseSelectionFunction, self.FineSelectionFunction)


    def CoarseSelectionFunction(self, coarse):
        '''Drop securities which have no fundamental data or have too low prices.
        Select those with highest by dollar volume'''

        if self.Time.month < self.month:
            return Universe.Unchanged

        else:    
            self.rebalance = True
        
    
            selected = [x for x in coarse if x.HasFundamentalData and x.Price > 2]
    
            return [x.Symbol for x in selected]


    def FineSelectionFunction(self, fine):
        
        if self.Time.month < self.month:
            return Universe.Unchanged
        
        else:    
            self.month = self.Time.month +2 
    
            filteredfine = [f for f in fine if f.CompanyProfile.MarketCap > 1000000000]
    
            selected = sorted(filteredfine, key=lambda f: f.ValuationRatios.EarningYield + 
                        f.OperationRatios.ROIC.OneYear +
                        (f.FinancialStatements.BalanceSheet.TangibleBookValue.OneMonth / f.Price) +
                        f.OperationRatios.AVG5YrsROIC.FiveYears
                        ,reverse=True)
    
            return [x.Symbol for x in selected][:100]


    def OnData(self, data):

        # Update the indicator
        for symbol, mom in self.mom.items():
            mom.Update(self.Time, self.Securities[symbol].Close)

        if not self.rebalance:
            return

        # Selects the securities with highest momentum
        sorted_mom = sorted([k for k,v in self.mom.items() if v.IsReady],
            key=lambda x: self.mom[x].Current.Value, reverse=True)
        selected = sorted_mom[:self.num_long]

        # Liquidate securities that are not in the list
        for symbol, mom in self.mom.items():
            if symbol not in selected:
                self.Liquidate(symbol, 'Not selected')

        # Buy selected securities
        for symbol in selected:
            self.SetHoldings(symbol, 1/self.num_long)

        self.rebalance = False


    def OnSecuritiesChanged(self, changes):

        # Clean up data for removed securities and Liquidate
        for security in changes.RemovedSecurities:
            symbol = security.Symbol
            if self.mom.pop(symbol, None) is not None:
                self.Liquidate(symbol, 'Removed from universe')

        for security in changes.AddedSecurities:
            if security.Symbol not in self.mom:
                self.mom[security.Symbol] = Momentum(self.lookback)

        # Warm up the indicator with history price if it is not ready
        addedSymbols = [k for k,v in self.mom.items() if not v.IsReady]

        history = self.History(addedSymbols, 1 + self.lookback, Resolution.Daily)
        history = history.close.unstack(level=0)

        for symbol in addedSymbols:
            ticker = str(symbol)
            if ticker in history:
                for time, value in history[ticker].items():
                    item = IndicatorDataPoint(symbol, time, value)
                    self.mom[symbol].Update(item)