Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Probabilistic Sharpe Ratio
0%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
3.192
Tracking Error
0.19
Treynor Ratio
0
Total Fees
$0.00
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Common")

from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
import numpy as np
import scipy as sp
import talib


class main(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020, 10, 10)  # Set Start Date
        self.SetEndDate(2020, 10, 30)
        self.initialcash=10000 # Set Strategy Cash
        self.SetCash(self.initialcash)  
    
        self.flagCommentRank=0 # 0 to print daily equity, 1 to print positions, 2 to print get_rank(), 3 to print rebalance
        
        # Uncomment below to set a specific brokerage and its slippage associated model
        #self.SetBrokerageModel(BrokerageName.Alpaca)
        #self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage)
        
        
        
        ##############################################
        ##### Single instrument subscription #########
        ##############################################
        
        
        
        self.SPY=self.AddEquity("SPY", Resolution.Minute)
        self.JPM=self.AddEquity("JPM", Resolution.Minute)
        self.PG=self.AddEquity("PG", Resolution.Minute)
        self.JNJ=self.AddEquity("JNJ", Resolution.Minute)
        self.PEP=self.AddEquity("PEP", Resolution.Minute)
        self.VZ=self.AddEquity("VZ", Resolution.Minute)
        self.BAC=self.AddEquity("BAC", Resolution.Minute)
        self.DIS=self.AddEquity("DIS", Resolution.Minute)
        self.MA=self.AddEquity("MA", Resolution.Minute)
        self.DHR=self.AddEquity("DHR", Resolution.Minute)
        self.HD=self.AddEquity("HD", Resolution.Minute)
        self.KO=self.AddEquity("KO", Resolution.Minute)
        self.V=self.AddEquity("V", Resolution.Minute)
        self.CRM=self.AddEquity("CRM", Resolution.Minute)
        self.ACN=self.AddEquity("ACN", Resolution.Minute)
        self.WMT=self.AddEquity("WMT", Resolution.Minute)
        self.SBC=self.AddEquity("SBC", Resolution.Minute)
        self.NKE=self.AddEquity("NKE", Resolution.Minute)
        self.UNH=self.AddEquity("UNH", Resolution.Minute)
        self.ABT=self.AddEquity("ABT", Resolution.Minute)
        self.PFE=self.AddEquity("PFE", Resolution.Minute)
        self.FB=self.AddEquity("FB", Resolution.Minute)
        self.MRK=self.AddEquity("MRK", Resolution.Minute)
        self.LLY=self.AddEquity("LLY", Resolution.Minute)
        self.CVX=self.AddEquity("CVX", Resolution.Minute)
        
        self.Securities["SPY"].SetDataNormalizationMode(DataNormalizationMode.Raw);   #without this the prices follow the rule below!
        self.Securities["JPM"].SetDataNormalizationMode(DataNormalizationMode.Raw);   #without this the prices follow the rule below!
        self.Securities["PG"].SetDataNormalizationMode(DataNormalizationMode.Raw);   #without this the prices follow the rule below!
        self.Securities["JNJ"].SetDataNormalizationMode(DataNormalizationMode.Raw);   #without this the prices follow the rule below!
        self.Securities["PEP"].SetDataNormalizationMode(DataNormalizationMode.Raw);   #without this the prices follow the rule below!
        self.Securities["VZ"].SetDataNormalizationMode(DataNormalizationMode.Raw);   #without this the prices follow the rule below!
        self.Securities["BAC"].SetDataNormalizationMode(DataNormalizationMode.Raw);   #without this the prices follow the rule below!
        self.Securities["DIS"].SetDataNormalizationMode(DataNormalizationMode.Raw);   #without this the prices follow the rule below!
        self.Securities["MA"].SetDataNormalizationMode(DataNormalizationMode.Raw);   #without this the prices follow the rule below!
        self.Securities["DHR"].SetDataNormalizationMode(DataNormalizationMode.Raw);   #without this the prices follow the rule below!
        self.Securities["HD"].SetDataNormalizationMode(DataNormalizationMode.Raw);   #without this the prices follow the rule below!
        self.Securities["KO"].SetDataNormalizationMode(DataNormalizationMode.Raw);   #without this the prices follow the rule below!
        self.Securities["V"].SetDataNormalizationMode(DataNormalizationMode.Raw);   #without this the prices follow the rule below!
        self.Securities["CRM"].SetDataNormalizationMode(DataNormalizationMode.Raw);   #without this the prices follow the rule below!
        self.Securities["ACN"].SetDataNormalizationMode(DataNormalizationMode.Raw);   #without this the prices follow the rule below!
        self.Securities["WMT"].SetDataNormalizationMode(DataNormalizationMode.Raw);   #without this the prices follow the rule below!
        self.Securities["SBC"].SetDataNormalizationMode(DataNormalizationMode.Raw);   #without this the prices follow the rule below!
        self.Securities["NKE"].SetDataNormalizationMode(DataNormalizationMode.Raw);   #without this the prices follow the rule below!
        self.Securities["UNH"].SetDataNormalizationMode(DataNormalizationMode.Raw);   #without this the prices follow the rule below!
        self.Securities["ABT"].SetDataNormalizationMode(DataNormalizationMode.Raw);   #without this the prices follow the rule below!
        self.Securities["PFE"].SetDataNormalizationMode(DataNormalizationMode.Raw);   #without this the prices follow the rule below!
        self.Securities["FB"].SetDataNormalizationMode(DataNormalizationMode.Raw);   #without this the prices follow the rule below!
        self.Securities["MRK"].SetDataNormalizationMode(DataNormalizationMode.Raw);   #without this the prices follow the rule below!
        self.Securities["LLY"].SetDataNormalizationMode(DataNormalizationMode.Raw);   #without this the prices follow the rule below!
        self.Securities["CVX"].SetDataNormalizationMode(DataNormalizationMode.Raw);   #without this the prices follow the rule below!
        
        '''
        Splits and dividends are backwards adjusted into the price of the asset. The price today is identical to current market price. For more information on this see Investopedia's article
        '''
        
        
        """
        Store all the constants here.
        """
        self.SetWarmUp(timedelta(20))
        
        
        self.INDUSTRY = ['FB', 'JNJ', 'JPM', 'WMT', 'V', 'MA', 'DIS', 
                'HD', 'BAC', 'VZ', 'KO', 'SBC', 'PFE',
                'ABT', 'CRM', 'PEP', 'LLY', 'CVX', 'ACN', 'DHR',
                'PG', 'UNH', 'NKE', 'MRK'] 

        self.SECTOR = ['FB', 'JNJ', 'JPM', 'WMT', 'V', 'MA', 'DIS', 
                'HD', 'BAC', 'VZ', 'KO', 'SBC', 'PFE',
                'ABT', 'CRM', 'PEP', 'LLY', 'CVX', 'ACN', 'DHR',
                'PG', 'UNH', 'NKE', 'MRK'] 

        self.CASH =  ['FB', 'JNJ', 'WMT']
        self.REGIME_EQ = 'SPY'
        self.REGIME_FI = 'SBC'
        self.CASH_FILTERING_SID = 'MRK'
        self.HEDGE_SID = 'SPY'
    
        

        self.LONG_PERIOD = 126
        self.SHORT_PERIOD = 63
        self.VOL_PERIOD = 5
        self.SMA_PERIOD = 100

        # number of top ranked symbols
        self.INDUSTRY_TOPN = 10
        self.SECTOR_TOPN = 10
        # number of bottom ranked symbols
        self.INDUSTRY_BOTN = self.INDUSTRY_TOPN/2 
        self.HEDGE_VALUE = "EQUITY" # Only use this to toggle: "TOTAL" to hedge total portfolio value, "EQUITY" to hedge equity portfolio value, "NONE" to disable hedging. Define Hedge Instruments and ratios below:
        self.HEDGE_MULTIPLIER_SID_RISK_ON = 0
        self.HEDGE_MULTIPLIER_INDUSTRY_RISK_ON = 0.50
        self.HEDGE_MULTIPLIER_SID_RISK_OFF = 0
        self.HEDGE_MULTIPLIER_INDUSTRY_RISK_OFF = 0
        # multipliers to calculate rank
        self.LONG_MOMENTUM_MULTIPLIER = 0.60
        self.SHORT_MOMENTUM_MULTIPLIER = 0.00
        self.VOL_MULTIPLIER = 0.40
        # percent to keep in cash to avoid negative cash
        self.CASH_PERCENT = 0.25
        # list of rank types to apply cash filter to
        # Possible values:
        # ["sector"], ["industry"], ["sector", "industry"], []
        self.CASH_FILTER_TYPES = ["sector"]
        # calculate percentage momentum
        self.PERCENTAGE_MOMENTUM = False # set to False to calculate raw momentum SB 'False'
        # Nuanced cash: persentage of cash allocated to CASH_FILTERING_SID
        # the rest of cash is allocated to CASH symbols
        self.NUANCED_CASH = 50 # set to 0 to switch it off
        # Zero Bound Filter
        self.ZBF_PERIOD = self.VOL_PERIOD
        self.IND_ABS_MO = 0.50 # set to "OFF" to switch off SB 0.50
        self.SECT_ABS_MO = 'OFF'
        # Volatility calculation
        self.VOL_INDUSTRY = 'semi-dev' # SB 'semi-dev' -'stdev' or 'skew' or 'kurtosis'
        self.VOL_SECTOR = 'semi-dev' # SB 'semi-dev'
        
        

        
        #if self.flagCommentRank==1:
        #    # we print daily positions at 12
        #    self.Schedule.On(self.DateRules.EveryDay(),self.TimeRules.At(12, 0), self.print_positions)
        
        
            
        self.Schedule.On(self.DateRules.EveryDay(),self.TimeRules.At(12, 0), self.daily_equity)

    def daily_equity(self):    
        
        if self.flagCommentRank==0:
            self.Log("daily equity: " + str(self.Portfolio.TotalPortfolioValue) + f" at {self.Time}")
        
        
    def print_positions(self):
        
        all_symbols = [ x.Value for x in self.Portfolio.Keys ]
        
        for symbol in all_symbols:
            if self.Portfolio[symbol].Quantity!=0:
                self.Log("invested: " + str(symbol)+" quantity: " + str(self.Portfolio[symbol].Quantity)+" Holdings profit/loss " +str(self.Portfolio[symbol].UnrealizedProfit))