Overall Statistics
from datetime import timedelta

class MOMAlphaModel(AlphaModel):
    
    def __init__(self):
        self.mom = []
      
    def OnSecuritiesChanged(self, algorithm, changes):
        
        #Initialize a 14-day momentum indicator for each symbol
        for security in changes.AddedSecurities:
            symbol = security.Symbol
            self.mom.append({"symbol":symbol, "indicator":algorithm.MOM(symbol, 14, Resolution.Minute)})  #14
        
    def Update(self, algorithm, data):

        #Sort the list of dictionaries by indicator in descending order
        ordered = sorted(self.mom, key=lambda kv: kv["indicator"].Current.Value, reverse=True)
        
        #Return a group of insights, emitting InsightDirection.Up for the first item of ordered, and InsightDirection.Flat for the second
        return Insight.Group(
            [
                Insight.Price(ordered[0]["symbol"], timedelta(1), InsightDirection.Up),
                #Insight.Price(ordered[1]["symbol"], timedelta(1), InsightDirection.Down),
                Insight.Price(ordered[1]["symbol"], timedelta(1), InsightDirection.Flat),
                #Insight.Price(ordered[3]["symbol"], timedelta(1), InsightDirection.Down),
            ])
        
class FrameworkAlgorithm(QCAlgorithm):
    
    def Initialize(self):

        self.SetStartDate(2019, 11, 1)   
        #self.SetEndDate(2013, 12, 1)   
        self.SetCash(1000000)           
        self.reference = "SPY"
        self.SetBenchmark("SPY")
        
        symbols = [
                    Symbol.Create("GLD", SecurityType.Equity, Market.USA), 
                    Symbol.Create("SPY", SecurityType.Equity, Market.USA),

                    ]
                    
        self.UniverseSettings.Resolution = Resolution.Minute
        self.SetUniverseSelection(ManualUniverseSelectionModel(symbols))

        # Call the MOMAlphaModel Class 
        self.SetAlpha(MOMAlphaModel())

        self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
        #self.SetPortfolioConstruction(InsightWeightingPortfolioConstructionModel())
        self.SetRiskManagement(MaximumDrawdownPercentPerSecurity(0.002))
        self.SetExecution(ImmediateExecutionModel())
        self.SetBrokerageModel(BrokerageName.AlphaStreams)