Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# R code re: CapitalSpecator.com post for analyzing 2 risk-managed funds
# "Analyzing Risk-Managed Funds With R"
# http://www.capitalspectator.com/analyzing-risk-managed-funds-with-r/#more-8031
# 23 Sep 2016
# By James Picerno
# http://www.capitalspectator.com/
# (c) 2016 by Beta Publishing LLC
# load packages
library(xts)
library(timeSeries)
library(tseries)
library(TTR)
library(quantmod)
library(PerformanceAnalytics)
# download fund prices
symbols <-c("MDLOX", "VGSTX")
getSymbols(symbols, src='yahoo', from='1995-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)
}
# merge price histories into one data set
prices <- do.call(merge, lapply(symbols, get))
colnames(prices) <-c(symbols)
# generate daily return series for funds
returns <-na.omit(ROC(prices,1,"discrete"))
colnames(prices) <-c(symbols)
### risk metrics
ret.1 <-round(Return.annualized(returns)*100,2)
Annualized_Vol <-round(apply(returns,2,sd)*sqrt(252)*100,2)
sr.1 <-round(SharpeRatio.annualized(returns),2)
sort.1 <-round(SortinoRatio(returns)*sqrt(252),2)
risk.metrics <- rbind(ret.1,Annualized_Vol,sr.1,sort.1)
mdlox.dd <-table.Drawdowns(returns[,1])
vgstx.dd <-table.Drawdowns(returns[,2])
# short fall risk
mdlox.252.ret <-na.omit(ROC(prices[,1],252,"discrete"))
vgstx.252.ret <-na.omit(ROC(prices[,2],252,"discrete"))
mdlox.sf.risk <-pnorm(0,mean=mean(mdlox.252.ret),sd=sd(mdlox.252.ret),lower.tail=TRUE)*100
vgstx.sf.risk <-pnorm(0,mean=mean(vgstx.252.ret),sd=sd(vgstx.252.ret),lower.tail=TRUE)*100
sf.risk.all <-round(rbind(mdlox.sf.risk, vgstx.sf.risk),1)
# VaR charts
# MDLOX VaR
chart.VaRSensitivity(returns[,1],
methods=c("HistoricalVaR", "ModifiedVaR"), ylim=c(-0.03, 0),
colorset=bluefocus, lwd=2)
mtext(side=3,"based on daily returns: 1996-2016",line=0.2)
# VGSTX VaR
chart.VaRSensitivity(returns[,2],
methods=c("HistoricalVaR", "ModifiedVaR"), ylim=c(-0.03, 0),
colorset=bluefocus, lwd=2)
mtext(side=3,"based on daily returns:1996-2016",line=0.2)
# END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.