Overall Statistics
from datetime import timedelta

class FrameworkAlgorithm(QCAlgorithm):
    
    def Initialize(self):
        self.SetStartDate(2014, 1, 15)   
        self.SetEndDate(2019, 1, 1)    
        self.SetCash(100000)     
        
        symbols = [Symbol.Create("SPY", SecurityType.Equity, Market.USA),  Symbol.Create("BND", SecurityType.Equity, Market.USA)]
        self.UniverseSettings.Resolution = Resolution.Daily
        self.SetUniverseSelection(ManualUniverseSelectionModel(symbols))
        
        self.SetAlpha(MOMAlphaModel())
        
        self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
        
        self.SetRiskManagement(MaximumDrawdownPercentPerSecurity(0.02))
        
        self.SetExecution(ImmediateExecutionModel())

class MOMAlphaModel(AlphaModel): 
    def __init__(self):
        self.mom = []
        self.month = -1
        
    def OnSecuritiesChanged(self, algorithm, changes):
        for security in changes.AddedSecurities:
            symbol = security.Symbol
            self.mom.append({"symbol":symbol, "indicator":algorithm.MOM(symbol, 14, Resolution.Daily)})
            
    def Update(self, algorithm, data):
        if self.month == algorithm.Time.month:
            return []
        self.month = algorithm.Time.month
            
        ordered = sorted(self.mom, key=lambda kv: kv["indicator"].Current.Value, reverse=True)
        return Insight.Group([Insight.Price(ordered[0]['symbol'], timedelta(22), InsightDirection.Up), 
                              Insight.Price(ordered[1]['symbol'], timedelta(22), InsightDirection.Flat) ])