Trying to colsolidate daily data in a monthly bar. Could I then calculate monthly performance of each Sector ETF from the monthly bar? Would there be an easy way to calculate the performance of each sector in the Rebalance function, after the IsWarmingUp method? Appreciate any ideas to get the code to "Build" and calculate the monthly sector perforemance. Thanks ....
Here's the code:
# Monthly ETF Rollover Strategy
# Use 11 Sector ETFs (XLB, XLC, XLE, XLF, XLI, XLK, XLP, XLRE, XLU, XLV, and 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 TOP3 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, 5, 1)
self.SetEndDate(datetime.now())
self.SetCash(100000)
self.data = {}
period = 30
self.SetWarmUp(period)
# choose 11 sector ETFs
self.symbols = ["XLB", # Materials
"XLC", # Communication Services
"XLE", # Energy
"XLF", # Financials
"XLI", # Industrials
"XLK", # Technology
"XLP", # Staples
"XLRE", # Real Estate
"XLU", # Utilities
"XLV", # Health Care
"XLY"] # Discretionary
for symbol in self.symbols:
self.AddEquity(symbol, Resolution.Daily)
self.Consolidate(symbol, CalendarType.Monthly, self.OnDataConsolidated)
# shcedule the function to fire at the month start
self.Schedule.On(self.DateRules.MonthStart("XLB"), self.TimeRules.AfterMarketOpen("XLB", 10), self.Rebalance)
def OnData(self, data):
if self.IsWarmingUp: return
def OnDataConsolidated(self, bar):
self.currentBar = bar
def Rebalance(self):
if self.IsWarmingUp: return
top3 = pd.Series(self.data).sort_values(ascending = False)[:3]
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:
self.SetHoldings(symbol, 1/len(top3))