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):
'''
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)
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:# 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 \
algorithm.Time.replace(year = algorithm.Time.year + 1, month = 1, day = 1)
Then, emit "grouped" insights for the two ETFs that have the highest and lowest momentum during the lookback period: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.

Author