Overall Statistics
Total Trades
95
Average Win
2.60%
Average Loss
-0.81%
Compounding Annual Return
4.754%
Drawdown
13.700%
Expectancy
0.579
Net Profit
25.932%
Sharpe Ratio
0.499
Probabilistic Sharpe Ratio
10.817%
Loss Rate
62%
Win Rate
38%
Profit-Loss Ratio
3.21
Alpha
0.045
Beta
-0.027
Annual Standard Deviation
0.086
Annual Variance
0.007
Information Ratio
-0.227
Tracking Error
0.15
Treynor Ratio
-1.572
Total Fees
$344.63
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) ])