## ETF Sector Rotation - Logic Help

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.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))

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.

Hi Mike Reichard ,

The algorithm can create the RateOfChangePercent manually and register it to a monthly consolidator (see attached example).

Alternatively, since the algorithm is rebalancing monthly, it can simply make a historical request for all symbols and calculate the monthly return.

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.

Thanks for the feedback.
0

Thanks for the help. Could you help explain the following lines of code:

func = lambda x:x[1].Current.Value

Top3 = {x[0]:x[1].Current.Value

for x in sorted(self.data.items(), key=func, reverse=True)[:3]}

Is Top3 just a python/panda series with the symbols of the lowest performing sector ETF’s? How do I learn more about the “lambda” function/method or whatever that is?

Really appreciate your help. Thank you!
0

Hi Mike Reichard ,

I used the built-in sorted function to sort the dictionary of returns keyed by Symbol object because it is more efficient than using pandas. For details, please check out the Python How To: Sorting HOW TO

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.

Thanks!
0

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.