- Create Clusters
- Allocate funds within each Cluster using Risk Parity
- Allocate funds across all Clusters using Risk Parity
- Function to allocate funds. For example, risk.parity.portfolio, will use use risk parity to allocate funds both within and across clusters.
- Function to create clusters. For example, cluster.group.kmeans.90, will create clusters using k-means algorithm
############################################################ ################### # Load Systematic Investor Toolbox (SIT) # http://systematicinvestor. wordpress.com/systematic- investor-toolbox/ ############################## ############################## ################### setInternet2(TRUE) con = gzcon(url('http://www. systematicportfolio.com/sit.gz ', 'rb')) source(con) close(con) #***************************** ****************************** ****** # Load historical data for ETFs #***************************** ****************************** ******* load.packages('quantmod') tickers = spl('GLD,UUP,SPY,QQQ,IWM,EEM, EFA,IYR,USO,TLT') data <- new.env() getSymbols(tickers, src = 'yahoo', from = '1900-01-01', env = data, auto.assign = T) for(i in ls(data)) data[[i]] = adjustOHLC(data[[i]], use.Adjusted=T) bt.prep(data, align='remove.na')
Next, let’s run the 2 versions of Cluster Portfolio Allocation using Equal Weight and Risk Parity algorithms to allocate funds:
#*********************************************************** ****** # Code Strategies #***************************** ****************************** ******* periodicity = 'months' lookback.len = 250 cluster.group = cluster.group.kmeans.90 obj = portfolio.allocation.helper( data$prices, periodicity = periodicity, lookback.len = lookback.len, min.risk.fns = list( EW=equal.weight.portfolio, RP=risk.parity.portfolio, C.EW = distribute.weights(equal. weight.portfolio, cluster.group), C.RP=distribute.weights(risk. parity.portfolio, cluster.group) ) ) models = create.strategies(obj, data)$models
Finally, let’s examine the results:
#*********************************************************** ****** # Create Report #***************************** ****************************** ******* strategy.performance. snapshoot(models, T)
The Cluster Portfolio Allocation produce portfolios with better risk-adjusted returns and smaller drawdowns.
To view the complete source code for this example, please have a look at the bt.cluster.portfolio.
No comments:
Post a Comment