The Algorithm Framework allows developers to focus on core "modules" which encourages good algorithm design and also allows for previously developed "modules" to be used repeatedly.

The conversion of the strategy Asset Class Trend Following in the strategy library to an Alpha Model is relatively straight forward.

First, define the security universe in the method Initialize():

symbols = [Symbol.Create(ticker, SecurityType.Equity, Market.USA)
for ticker in ["SPY", "EFA", "BND", "VNQ", "GSG"]]
self.UniverseSettings.Resolution = Resolution.Daily
self.SetUniverseSelection( ManualUniverseSelectionModel(symbols) )

 

 

Then, create the equivalent Alpha model, which emits daily insights according to the logic of the strategy.

When security is added to the security universe, the method OnSecuritiesChanged() will ignite and add the indicator object to the smaBySymbol dictionary and the indicator is warmed up using History():

def OnSecuritiesChanged(self, algorithm, changes):

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.smaBySymbol.setdefault(symbol, algorithm.SMA(symbol, self.period, self.resolution))
for time, value in history[str(symbol)].iteritems():
data.Update(time, value)

Insight with one-day prediction interval is emitted using the method Update() if the security price exceeds its simple moving average value:

def Update(self, algorithm, data):

insights = []

for symbol,sma in self.smaBySymbol.items():
if sma.IsReady:
if algorithm.Securities[symbol].Price > sma.Current.Value:
insights.append(Insight.Price(symbol,self.predictionInterval,InsightDirection.Up))

return insights
 This alpha model is now ready to be used with other modules within the Algorithm Framework.In the method Initialize():self.SetAlpha(AssetClassTrendFollowingAlphaModel())
self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
self.SetExecution(ImmediateExecutionModel())
self.SetRiskManagement(NullRiskManagementModel())

In this example, EqualWeightingPortfolioConstructionModel() is used to rebalance the portfolio each day, which makes life easy for developers as there is no need to code the rebalancing algorithm.

    

Author