Overall Statistics
# Speculation Rollover Strategy
# Use 12 Speculative ETFs (DWT, UWT, DGAZ, UGAZ, TZA, TNA, ERY, ERX, FAZ, FAS, TLT, and TBT), equal weight the TOP3 ETF’s on 1st Day of the Month. Hold asset class Sector ETF’s for 1 month.
# If ETF is still in the TOPX at month end, Keep It

import numpy as np
import pandas as pd
from datetime import datetime

class EmmausAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2019, 1, 1)

        # choose 13 speculative ETF 
        tickers = [ "SPY",  # S&P500 ETF 
                    "DWT",  # Triple Oil Down
                    "UWT",  # Triple Oil Up
                    "DGAZ",  # Triple Natural Gas Down
                    "UGAZ",  # Triple Natural Gas Up
                    "TZA",  # Triple Small Cap Down
                    "TNA",  # Triple Small Cap Up
                    "ERY",  # Triple Energy Down
                    "ERX",  # Triple Energy Up
                    "FAZ",  # Triple Financials Down
                    "FAS",  # Triple Financials Up
                    "TBT",  # 20 Treasury Bond Down
                    "TLT"]  # 20 Treasury Bond Up 

        self.data = {}

        for ticker in tickers:
            symbol = self.AddEquity(ticker, Resolution.Daily).Symbol
            self.data[symbol] = RateOfChangePercent(1)
            consolidator = TradeBarConsolidator(CalendarType.Monthly)
            self.RegisterIndicator(symbol, self.data[symbol], consolidator)

        self.TRIX_Period = 10
        self.TRIX_OB =60   # Overbought is above 60
        self.TRIX_OS =40   # Oversold is below 40    
        self.TRIX_Ind = self.TRIX("SPY",self.TRIX_Period, Resolution.Daily)
        # SMA of TRIX
        self.TRIX_avg = IndicatorExtensions.SMA(self.TRIX_Ind, 4)

        # shcedule the function to fire on 1st Day of Month, after 30 minutes 
            self.TimeRules.AfterMarketOpen("TBT", 30),
    def OnData(self, data):
        if self.TRIX_Ind.Current.Value < self.TRIX_avg.Current.Value :
            if self.Portfolio.Invested:
                self.Debug("Overbought Signal")
            self.Debug("Not Overbought") 

    def Rebalance(self):
        if self.IsWarmingUp:

        selected = {x[0]: x[1].Current.Value for x in sorted(self.data.items(), key=lambda x: x[1].Current.Value, reverse=False)[:3]}

        # liquidate the security which is no longer in the top3 momentum list
        for symbol in self.data:
            if symbol not in selected:
                if self.Portfolio[symbol].Invested:
                   self.Liquidate(symbol, 'Not selected')
        for symbol in selected:
            self.SetHoldings(symbol, 1/len(selected))