Overall Statistics
Total Trades
149
Average Win
2.37%
Average Loss
-0.68%
Compounding Annual Return
12.054%
Drawdown
10.100%
Expectancy
1.115
Net Profit
80.162%
Sharpe Ratio
1.15
Probabilistic Sharpe Ratio
59.049%
Loss Rate
53%
Win Rate
47%
Profit-Loss Ratio
3.46
Alpha
0.106
Beta
-0.024
Annual Standard Deviation
0.089
Annual Variance
0.008
Information Ratio
-0.264
Tracking Error
0.194
Treynor Ratio
-4.25
Total Fees
$317.13
Estimated Strategy Capacity
$500000000.00
Lowest Capacity Asset
SPY R735QTJ8XC9X
class LongMomentumSPY(QCAlgorithm):
    
    def Initialize(self):
        
        self.SetStartDate(2016, 4, 14)  
        self.SetEndDate(2021, 6, 14)  
        self.SetCash(100000)  
        self.spy = self.AddEquity("SPY", Resolution.Daily)  
        self.SetBenchmark("SPY")
        self.SetBrokerageModel(BrokerageName.AlphaStreams)
        self.SetExecution(ImmediateExecutionModel())
        self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
        
        symbols = [Symbol.Create("SPY", SecurityType.Equity, Market.USA)]
        self.AddUniverseSelection(ManualUniverseSelectionModel(symbols))
      
        self.spyMomentum = self.MOMP("SPY", 30, Resolution.Daily)
        
        upthreshold = 0.6
        # upthreshold = self.GetParameter("upthreshold")
        
        self.AddAlpha(LongMomentumAlphaModel(self.spyMomentum, upthreshold))
        
    def OnEndOfDay(self, symbol):
        self.Log("Taking a position of " + str(self.Portfolio[symbol].Quantity) + " units of symbol " + str(symbol))
        
class LongMomentumAlphaModel(AlphaModel):

    def __init__(self, spyMomentum, upthreshold):
        self.period = timedelta(120)
        self.spyMomentum = spyMomentum
        self.upthreshold = upthreshold
        
    def Update(self, algorithm, data):
        insights = []
        
        if data.Bars.ContainsKey("SPY"):
            self.spyMomentum.Update(data["SPY"].EndTime, data["SPY"].Close)
            
        if self.spyMomentum.IsReady:
            if self.spyMomentum.Current.Value >= self.upthreshold:
                insights.append(Insight("SPY", self.period, InsightType.Price, InsightDirection.Up, 1.0, None))
            else:
                insights.append(Insight("SPY", self.period, InsightType.Price, InsightDirection.Flat, 1.0, None))
            
        return insights
    
    def OnSecuritiesChanged(self, algorithm, changes):
        self.changes = changes