Created
August 17, 2012 14:10
-
-
Save timelyportfolio/3378990 to your computer and use it in GitHub Desktop.
plot.xts can create horizon charts
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
#plot.xts with horizons | |
#install.packages("xtsExtra", repos="http://R-Forge.R-project.org") | |
require(PerformanceAnalytics) | |
require(quantmod) | |
require(xtsExtra) #if you get error, install xtsExtra from r-forge | |
horizon.panel <- function(index,x,...) { | |
#get some decent colors from RColorBrewer | |
#we will use colors on the edges so 2:4 for red and 7:9 for blue | |
require(RColorBrewer) | |
col.brew <- brewer.pal(name="RdBu",n=10) | |
#ease this reference later | |
n=NROW(x) | |
#clean up NA with either of the two methods below | |
#x[which(is.na(x),arr.ind=TRUE)[,1], | |
# unique(which(is.na(x),ar.ind=TRUE)[,2])] <- 0 | |
x <- apply(x,MARGIN=2,FUN=na.fill,fill=0) | |
#get number of bands for the loop | |
#limit to 3 | |
nbands = 3 | |
#first tried this but will not work since each series needs to have same number of bands | |
#min(4,ceiling(max(abs(coredata(x)))/horizonscale)) | |
for (i in 1:nbands) { | |
#draw positive | |
polygon( | |
c(index[1], index, index[n]), | |
c(origin, coredata(x) - (i-1) * horizonscale,origin), | |
col=col.brew[length(col.brew)-nbands+i-1], | |
border=NA | |
) | |
#draw negative | |
polygon( | |
c(index[1], index, index[n]), | |
c(origin, -coredata(x) - (i-1) * horizonscale,origin), | |
col=col.brew[nbands-i+1], | |
border=NA | |
) | |
} | |
#delete trash drawn below origin that we keep so no overlap between positive and negative | |
polygon( | |
c(index[1], index, index[n]), | |
c(origin, -ifelse(coredata(x)==origin,horizonscale*5,abs(coredata(x))),origin), | |
col=par("bg"), | |
border=NA | |
) | |
#draw a line at the origin | |
abline(h=origin,col="black") | |
#labelling just for example purposes | |
#do label at top left and top right | |
text(x=index[1], y=horizonscale, colnames(x), adj=c(0,1)) | |
text(x=index[n], y=horizonscale, colnames(x), adj=c(1,1)) | |
#do label at center right for ease of reference | |
#text(x=index[n], y=horizonscale/2, colnames(x), pos=2) | |
#do label at bottom center | |
#text(x=index[n/2], y=origin, colnames(x), pos=3) | |
} | |
data(edhec) | |
#set these up for global access since I do not see how to pass into panel function | |
origin = 0 | |
horizonscale = 0.1 | |
#get 12 month rolling return of edhec indexes | |
roc <- as.xts(apply(cumprod(edhec+1),MARGIN=2,ROC,n=12,type="discrete"),order.by=index(edhec)) | |
plot.xts(roc,layout.screens = 1:NCOL(roc), | |
ylim=c(origin,horizonscale), | |
panel=horizon.panel,bty="n", | |
auto.grid=FALSE, | |
yax.loc="none", | |
yaxt="n", #, xaxt="n") | |
main="Horizon Plot of EDHEC Indexes 12-month Rolling Return") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Package looks cool. I tried to run it and made it through most of the errors as I didn't have several of the packages installed but got stuck on the data(edhec). Can you make that available so I can run your code? The other errors I got were related to not having the data (I think). See below.
Thanks, Brian
Error: could not find function "as.xts"
Error: could not find function "plot.xts"