Overall Statistics
# Dual Momentum Technology ETF by Vladimir

class DualMomentum(QCAlgorithm):

    def Initialize(self):

        self.SetStartDate(2007, 6, 1)    
        # self.SetEndDate(2020, 12, 15)
        self.cap = 100000
        self.SetCash(self.cap)         
        self.ASSETS = [self.AddEquity(ticker, Resolution.Minute).Symbol for ticker in ['QQQ','FDN','SHV','TLT']]
        self.MKT = self.AddEquity('SPY', Resolution.Daily).Symbol
        self.leverage = 1.0
        self.currentLong = self.ASSETS[0]
        self.mom = 126
        self.excl = 5
        self.spy = []
        self.SetWarmUp(self.mom + self.excl)
        
        self.Schedule.On(self.DateRules.MonthStart('SPY'),self.TimeRules.AfterMarketOpen('SPY', 65), 
            self.rebalance)
        self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.BeforeMarketClose('SPY', 0), 
            self.record_vars)
            
   
    def Momentum(self, symbol, period, excl):
        
        prices = self.History(symbol, TimeSpan.FromDays(period + excl),Resolution.Daily).close
        return prices[-excl] / prices[0]
        
       
    def rebalance(self):
        
        if self.Momentum(self.ASSETS[0], self.mom, self.excl) < self.Momentum(self.ASSETS[2], self.mom, self.excl):
            self.currentLong = self.ASSETS[3]
            
        elif self.Momentum(self.ASSETS[0],self.mom, self.excl) > self.Momentum(self.ASSETS[1],self.mom, self.excl):
            self.currentLong=self.ASSETS[0]
            
        else:
            self.currentLong = self.ASSETS[1]
            
        stocksInvested = [x.Key for x in self.Portfolio if x.Value.Invested]
        
        if not stocksInvested: 
            self.SetHoldings(self.currentLong, self.leverage)
            
        elif self.currentLong != stocksInvested[0]:
            self.SetHoldings(self.currentLong, self.leverage, True)


    def record_vars(self): 
    
        hist = self.History(self.MKT, 2, Resolution.Daily)['close'].unstack(level= 0).dropna() 
        self.spy.append(hist[self.MKT].iloc[-1])
        spy_perf = self.spy[-1] / self.spy[0] * self.cap
        self.Plot('Strategy Equity', 'SPY', spy_perf)
        
        account_leverage = self.Portfolio.TotalHoldingsValue / self.Portfolio.TotalPortfolioValue
        self.Plot('Holdings', 'leverage', round(account_leverage, 2))