Portfolio Construction and Optimization with R

Kyle Balkissoon

KKB Research and Development

2016-10-26

Introduction

At the end of this presentation you will know how to the following

  • Get Data
  • Process Data
  • Create Portfolio objectives
  • Optimize Portfolios
  • Analyse Portfolios
  • Case Study: Stance Equity

Get Data

Before we can do anything we need data, some good R libraries for getting data are:

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")

A realistic case - US SP500 Sectors

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')

Preprocess Data into Returns

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')

Introduction to PerformanceAnalytics

  • Library to compute many functions in return space
  • Used by hedge funds, proprietary trading firms, mutual funds, SMA's and banks
  • Robust tools for measuring portfolio and security performance with all the standard risk metrics.

Have a look at our data

chart.CumReturns(securities_matrix)