Last active
August 29, 2015 14:24
-
-
Save jpicerno1/565be39ca4226ecd004c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# efficient.frontier.06jun2015.R | |
# R code re: CapitalSpecator.com post on efficient frontier analytics: | |
# "Efficient Frontier Portfolios–Impractical But Still Useful" | |
# 6 July 2015 | |
# By James Picerno | |
# http://www.capitalspectator.com/efficient-frontier-portfolios-impractical-but-still-useful/ | |
# Keep in mind that results will change depending on when you run the code | |
# due to fluctuating market conditions | |
# load packages | |
library(fPortfolio) | |
library(xts) | |
library(timeSeries) | |
library(tseries) | |
library(TTR) | |
library(quantmod) | |
library(PerformanceAnalytics) | |
# download data, generate monthly returns | |
symbols <-c("SPY","IJS","EFA","EEM","IEF","LQD","VWEHX","RPIBX","PREMX","QRACX","VGSIX") | |
getSymbols(symbols, src='yahoo', from='2000-12-31') | |
for(symbol in symbols) { | |
x <- get(symbol) | |
indexFormat(x) <- '%Y-%m-%d' | |
colnames(x) <- gsub("x",symbol,colnames(x)) | |
x <- x[,6] | |
assign(symbol,x) | |
} | |
prices <- do.call(merge, lapply(symbols, get)) | |
colnames(prices) <-c(symbols) | |
prices.m <-to.period(prices,period="months",k=1,indexAt="lastof",OHLC=FALSE) | |
prices.ret <-na.omit(ROC(prices.m,12,"discrete")) | |
prices.ret.ts <-as.timeSeries(prices.ret) | |
assets <- dim(prices.ret.ts)[2] # number of columns | |
######################################## | |
# specific model and generate # | |
# efficient frontier data # | |
# no allocation constraints: long only # | |
######################################## | |
spec <- portfolioSpec() | |
setSolver(spec) <- "solveRquadprog" | |
setNFrontierPoints(spec) <- 30 | |
constraints <- c("LongOnly") | |
frontier.1 <- portfolioFrontier(prices.ret.ts, spec, constraints) | |
# Note: I use modified code to create the graphics in the blog post. | |
# For simplicity here, the default fPortfolio graphic functions are cited. | |
tailoredFrontierPlot(object = frontier.1) # rmetrics plotting function for efficient frontier | |
weightsPlot(frontier.1, col = rainbow(assets)) # rmetrics plotting funciton for portfolio weights | |
######################################## | |
# specific model and generate # | |
# efficient frontier data # | |
# with constraints: long only # | |
# 5% minimum, 50% maximum allocation # | |
# for each asset # | |
######################################## | |
spec <- portfolioSpec() | |
setSolver(spec) <- "solveRquadprog" | |
setNFrontierPoints(spec) <- 30 | |
constraints <- c("minW[1:assets]=0.05", "maxW[1:assets]=0.5") | |
frontier.2 <- portfolioFrontier(prices.ret.ts, spec, constraints) | |
tailoredFrontierPlot(object = frontier.2) # rmetrics plotting function for efficient frontier | |
weightsPlot(frontier.2, col = rainbow(assets)) # rmetrics plotting funciton for portfolio weights | |
### END ### |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment