In this previous post, I covered the conversion of the strategy Asset Class Trend Following to an Alpha Model.
This post covers the conversion of the Momentum and Style Rotation Effect strategy in the strategy library into an Alpha Model.
First, define the security universe in the method Initialize() and set the universe resolution to Daily:
symbols = [Symbol.Create(ticker, SecurityType.Equity, Market.USA)
for ticker in [ "IJJ", # iShares S&P MidCap 400 Value Index ETF
"IJS", # iShares S&P SmallCap 600 Value ETF
"IVE", # iShares S&P 500 Value Index ETF
"IVW", # iShares S&P 500 Growth ETF
"IJK", # iShares S&P Mid-Cap 400 Growth ETF
"IJT", # iShares S&P Small-Cap 600 Growth ETF
]]
self.UniverseSettings.Resolution = Resolution.Daily
self.SetUniverseSelection( ManualUniverseSelectionModel(symbols) )
When security is added to the security universe, the method OnSecuritiesChanged() will ignite and add the momentum indicator to the MomentumBySymbol dictionary. The indicator is warmed up using History():
def OnSecuritiesChanged(self, algorithm, changes):
The portfolio should rebalance at the beginning of each month. The method Update() returns an empty list of insights for all days except the first day in each month:
'''
Event fired each time the we add securities from the data feed
Args:
algorithm: The algorithm instance that experienced the change in securities
changes: The security additions and removals from the algorithm
'''
addedSymbols = [ x.Symbol for x in changes.AddedSecurities ]
history = algorithm.History(addedSymbols, self.period, self.resolution)
history = history.close.unstack(level=0)
for symbol in addedSymbols:
data = self.MomentumBySymbol.setdefault(symbol, algorithm.MOM(symbol, self.period, self.resolution))
for time, value in history[str(symbol)].iteritems():
data.Update(time, value)# In Update()
month = algorithm.Time.month
if month == self.lastMonth:
return []
self.lastMonth = month
Retrieve the momentum indicator value from each object stored in the dictionary and sort the symbols on their momentum indicator values:
momentumBySymbol = {symbol: value for symbol, value in self.MomentumBySymbol.items() if value.IsReady}
sortedMomentum = [x[0] for x in sorted(momentumBySymbol.items(), key = lambda kv: kv[1], reverse=True)]
Now, estimate the length of the insight period for monthly rebalancing:
expiry = algorithm.Time.replace(month = month + 1, day = 1) if month < 12 else \
Then, emit "grouped" insights for the two ETFs that have the highest and lowest momentum during the lookback period:
algorithm.Time.replace(year = algorithm.Time.year + 1, month = 1, day = 1)Insight.Group(
[
Insight.Price(sortedMomentum[0], expiry, InsightDirection.Up),
Insight.Price(sortedMomentum[-1], expiry, InsightDirection.Down)
])
The Algorithm Framework allows users to use an Alpha Model with a Portfolio Construction Model of their choice. In this example, EqualWeightingPortfolioConstructionModel() is used to rebalance the portfolio each day, rather than once every month which will result in a different performance profile in comparison to the original strategy.
Michael Manus
Flame
:)
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.
Flame
Thanks for the tips!
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!