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
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
# Speculation Rollover Strategy
# 11 Sectors (XLB, XLC, XLE, XLF, XLI, XLK, XLP, XLRE, XLU, XLV, XLY), 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(2018, 1, 1)
        self.SetEndDate(datetime.now())
        self.SetCash(100000) 

        # choose 11 sector ETF's 
        tickers = [ "SPY",  # S&P500 ETF 
                    "XLB",  # Materials
                    "XLC",  # Communications
                    "XLE",  # Energy
                    "XLF",  # Finance
                    "XLI",  # Industials
                    "XLK",  # Technology
                    "XLP",  # Staples
                    "XLRE", # Real Estate
                    "XLU",  # Utilities
                    "XLV",  # Health Care
                    "XLY"]  # Consumer Discretionary
                
        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.SetWarmUp(30)
        
        self.spy = Identity("SPY")
        self.SPY_Period = 10
        
        self.SPY_Ind = self.SMA("SPY",self.SPY_Period, Resolution.Daily)

        # shcedule the function to fire on 1st Day of Month, after 30 minutes 
        self.Schedule.On(
            self.DateRules.MonthStart("SPY"),
            self.TimeRules.AfterMarketOpen("SPY", 30),
            self.Rebalance)
            
    def OnData(self, data):
        
        if self.spy.Current.Value < self.SPY_Ind.Current.Value :
            if self.Portfolio.Invested:
                self.Debug("Overbought Signal")
                self.Liquidate()
        else:
            self.Debug("Not Overbought") 


    def Rebalance(self):
        if self.IsWarmingUp:
            return

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

        # 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')
                   
        if self.spy.Current.Value < self.SPY_Ind.Current.Value:
            return
        
        for symbol in selected:
            self.SetHoldings(symbol, 1/len(selected))