Overall Statistics
Total Trades
8
Average Win
0%
Average Loss
0%
Compounding Annual Return
119.014%
Drawdown
8.900%
Expectancy
0
Net Profit
5.753%
Sharpe Ratio
0.446
Probabilistic Sharpe Ratio
42.733%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0.263
Beta
1.925
Annual Standard Deviation
0.507
Annual Variance
0.257
Information Ratio
0.508
Tracking Error
0.483
Treynor Ratio
0.118
Total Fees
$8.00
Estimated Strategy Capacity
$9000000.00
Lowest Capacity Asset
SSYS R735QTJ8XC9X
import pandas as pd
from datetime import datetime

class SectorMomentumAlgorithm(QCAlgorithm):

    def Initialize(self):

        self.SetStartDate(2021, 11, 1)  
        #self.SetEndDate(datetime.now())  
        self.SetCash(10000) 
        self.Settings.FreePortfolioValuePercentage = 0.01

        self.data = {}
        
        # self.trailing_stop = self.GetParameter("trailing-stop")
        # self.trailing_stop = float(self.trailing_stop) if self.trailing_stop else 0.10
        # self.AddRiskManagement(TrailingStopRiskManagementModel(self.trailing_stop))        

        period = 3*21

        self.symbols = ["SOFI", "PLUG", "NIO", "MJ", "ASAN", "FVRR" "UPWK", "LTRX", "MTTR", "SEMR", "FUBO", "QRHC", "RBLX", "SSYS", "SYK", "LCID", "TWLO", "RIOT", "OCGN", "NVDA"]
        #self.symbols = ["AMZN", "AAPL", "TSLA", "GOOGL", "FB", "NVDA" "PYPL", "ADBE", "MSFT", "CSCO", "AVGO"]
        #self.symbols = ["GIGE", "ARKF", "CIBR", "MJ", "WCLD", "HAIL", "IDNA", "ICLN"]
        
        self.vix_price = self.GetParameter("vix-price")
        self.vix_price = float(self.vix_price) if self.vix_price else 30
        self.vix = self.AddIndex("VIX", Resolution.Minute).Symbol
                        
        self.SPY = self.AddEquity("SPY", Resolution.Minute).Symbol
                        
        # warm up the MOM indicator
        self.SetWarmUp(period)
        for symbol in self.symbols:
            self.AddEquity(symbol, Resolution.Daily)
            self.data[symbol] = self.MOM(symbol, period, Resolution.Daily)
        # shcedule the function to fire at the month start 
        self.Schedule.On(self.DateRules.MonthStart("SPY"), self.TimeRules.AfterMarketOpen("SPY"), self.Rebalance)
        self.allowTrades = True
            
    def OnData(self, data):
        self.Log(". VIX: " + str(self.Securities[self.vix].Price))
        if self.Securities[self.vix].Price > self.vix_price:
            self.Liquidate()
            self.Log("ONDATA VIX CHECK FAILED LIQUIDATING")
            
            self.allowTrades = False
            
        if self.Securities[self.vix].Price < self.vix_price:
            self.allowTrades = True
            
    def Rebalance(self):
        if self.IsWarmingUp: return
    
        top3 = pd.Series(self.data).sort_values(ascending = False)[:8]
        for kvp in self.Portfolio:
            security_hold = kvp.Value
            # liquidate the security which is no longer in the top3 momentum list
            if security_hold.Invested and (security_hold.Symbol.Value not in top3.index):
                self.Liquidate(security_hold.Symbol)
        
        for symbol in top3.index:
            if self.allowTrades == False: return
            self.SetHoldings(symbol, 1/len(top3))