Back

Asset Class Trend Following Strategy converted to an Alpha Model

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.

    

Update Backtest






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.



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.


I love the new Algorithm Framework - it is clear and concise and in combination with the wizzard, provides an efficient and simple programming model.

What I don't understand, however,  is why it is so cumbersome to realise arbitrary prediction intervals that are greater than one day. If you don't want your strategy to be too susceptible to short term noise, rebalancing once or twice a month makes a lot of sense.

Why don't you provide easy-to-use timing functions for rebalancing cycles that are greater than one day? 

0

Hi Filib.

Thank you for the feedback - I'm glad to hear that you like the Algorithm Framework.

This post features the conversion of the Momentum and Style Rotation Effect strategy to an Alpha model. The featured Alpha Model emits "grouped" insights for two ETFs at the beginning of each month. It's possible to create a custom Portfolio Construction Model that executes trades based on the insights once per month.

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.


Update Backtest





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.


Loading...

This discussion is closed