This is a talk I gave for the Toronto R users group on using various R packages for portfolio construction and optimization with the accompanying source code. So readers can see some of this tech applied to a real product I have included some unofficial (after fee) performance numbers for the Stance Equity product at the end of the deck.

We offer services in building and applying this for portfolio managers and institutional investors, if interested please reach out to me at the contact info here



library(Quandl) # see Quandl.com for more info
library(quantmod) #Has preconfigured api with yahoo info

##Quandl - Note requires assignment, type xts is xtensible time series
SPTSXComp=Quandl("YAHOO/INDEX_GSPTSE",type='xts')

##quantmod - assigns to object named GSPTSE - automatically in xts/zoo
getSymbols("^GSPTSE")

symbol_list = c('XLF','XLE','XLU','XLK','XLB','XLP','XLY','XLI','XLV')

getSymbols(symbol_list, from = '1990-01-01')
getSymbols("SPY",from='1990-01-01')

ls()

library(PerformanceAnalytics)
securities_matrix = NULL
for( sym in symbol_list){
  securities_matrix = merge.xts(securities_matrix,Return.calculate(Ad(get(paste(sym))),method='discrete'))
}

securities_matrix=securities_matrix[complete.cases(securities_matrix)]
SPYReturn=Return.calculate(Ad(SPY),method='discrete')
ls()

library(PerformanceAnalytics)
chart.CumReturns(securities_matrix)


library(PortfolioAnalytics)
MinimumVariancePortfolio=portfolio.spec(
  assets=colnames(securities_matrix))

MinimumVariancePortfolio=add.objective(
  portfolio=MinimumVariancePortfolio,
  type='risk',
  name='StdDev')

MinimumVariancePortfolio=add.constraint(
  portfolio = MinimumVariancePortfolio,
  type="full_investment")

MinimumVariancePortfolio=add.constraint(
  portfolio = MinimumVariancePortfolio,
  type="long_only")

MinimumVariancePortfolio=add.constraint(
  portfolio = MinimumVariancePortfolio,
  type="box",
  min=0,max=0.3)


OptimizedPortfolioMinVariance=optimize.portfolio(
  R=securities_matrix,
  portfolio=MinimumVariancePortfolio,
  trace=TRUE)

chart.Weights(OptimizedPortfolioMinVariance)


MeanVariancePortfolio=add.objective(
  portfolio=MinimumVariancePortfolio,
  type='return',
  name='mean')

OptimizedPortfolioMeanVariance=optimize.portfolio(
  R=securities_matrix,
  portfolio=MeanVariancePortfolio,
  trace=TRUE)

chart.Weights(OptimizedPortfolioMeanVariance)
OptimizedPortfolioMeanVariance$weights-OptimizedPortfolioMinVariance$weights

chart.RiskReward(OptimizedPortfolioMeanVariance,
                 return.col = 'mean',
                 risk.col = 'StdDev',
                 main='Risk to Return Plot of various Portfolio Combinations')

MinimumVarianceBT=optimize.portfolio.rebalancing(securities_matrix,MinimumVariancePortfolio,rebalance_on = 'years',training_period = 252,rolling_window = 252)
MeanVarianceBT=optimize.portfolio.rebalancing(securities_matrix,MeanVariancePortfolio,rebalance_on = 'years',training_period = 252,rolling_window = 252)

MinVariancePortfReturns=Return.rebalancing(R=securities_matrix,
                                           weights=extractWeights(MinimumVarianceBT))
colnames(MinVariancePortfReturns)=c('MinVariance')
MeanVariancePortfReturns=Return.rebalancing(R=securities_matrix,
                                            weights=extractWeights(MeanVarianceBT))
colnames(MeanVariancePortfReturns)=c('MeanVariance')
EqualWeightPortfReturns=Return.rebalancing(R=securities_matrix)
colnames(EqualWeightPortfReturns)=c('EqualWeight')

PortfolioComparisonData=merge.xts(MinVariancePortfReturns,
                                  MeanVariancePortfReturns,
                                  EqualWeightPortfReturns,
                                  SPYReturn)['2000-01-01/2016-10-25']
chart.CumReturns(PortfolioComparisonData,
                 main='Performance of Various Strategies',
                 legend.loc='topleft')


maxDrawdown(PortfolioComparisonData)


table.CAPM(PortfolioComparisonData[,1:3],
           PortfolioComparisonData[,4])[c(2,6,9,10,11,12),]

2 comments

  1. Comment by dysregulation

    dysregulation Reply October 31, 2016 at 12:50 am

    I get the following error at the indicated step:
    > OptimizedPortfolioMinVariance=optimize.portfolio(
    + R=securities_matrix,
    + portfolio=MinimumVariancePortfolio,
    + trace=TRUE)
    Error: “package:DEoptim” %in% search() || requireNamespace(“DEoptim”, …. is not TRUE

    • Comment by KyleBalkissoon

      KyleBalkissoon Reply October 31, 2016 at 3:33 pm

      you need the DEoptim library, install.packages(“DEoptim”)

Leave a reply

Your email address will not be published. Required fields are marked *

Close
Go top