public
Created

  • Download Gist
path of indexes since 2008.r
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
require(lattice)
require(latticeExtra)
require(directlabels)
require(ggplot2)
require(reshape2)
require(quantmod)
require(PerformanceAnalytics)
 
tckrs <- c("SPY","IWM","EWP","EFA","CRB") #will have to get CRB from Systematic Investor
descr <- c("SP500","Russell2000","Spain","EAFE","CRB")
#get equity indexes
getSymbols(tckrs[1:4],from="2008-12-01")
#and since Yahoo no longer provides ^DJUBS historical prices
#use Systematic Investor
con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb'))
source(con)
close(con)
CRB <- get.CRB()["2008-12-01::",]
 
getmaxmin <- function(prices,n=100) {
dc <- DonchianChannel(prices[,4],n)
maxmin <- rbind(prices[1,4],
prices[which(prices[,4]==dc[,1]),4],
prices[which(prices[,4]==dc[,3]),4],
prices[NROW(prices),4])/as.numeric(prices[1,4])
maxmin <- as.data.frame(cbind(as.Date(index(maxmin)),coredata(maxmin)),stringsAsFactors=FALSE)
colnames(maxmin) <- c("date","price")
return(maxmin)
}
 
n=200
df <- as.data.frame(cbind(descr[1],getmaxmin(get(tckrs[1]),n)),stringsAsFactors=FALSE)
colnames(df)[1] <- "index"
for (i in 2:length(tckrs)) {
temp <- as.data.frame(cbind(descr[i],getmaxmin(get(tckrs[i]),n)),stringsAsFactors=FALSE)
colnames(temp)[1] <- "index"
df <- rbind(df,temp)
}
 
direct.label(
xyplot(price~as.Date(date),groups=index,data=df,lwd=3,type="l",main="Path of World Markets Since Dec 2008"),
list("last.qp",hjust=0.35,vjust=-0.25,cex=0.75))
direct.label(
ggplot(aes(y=price,x=as.Date(date)),data=df) + geom_line(aes(colour=index)) + theme_bw() + opts(legend.position = "none") +
#ggplot(aes(y=price,x=as.Date(date)),data=indexes.melt) + geom_smooth(aes(colour=indexes)) + theme_bw() + opts(legend.position = "none") +
opts(panel.grid.minor = theme_blank()) +
opts(axis.line = theme_segment()) +
opts(panel.border = theme_blank()) +
opts(title="Path of World Indexes Since 2008")
,list("last.qp",hjust=0.75,vjust=-0.25,cex=0.75))
 
#do beginning, middle, and end
getbeginend <- function(prices,middle=TRUE) {
dc <- DonchianChannel(prices[,4],n)
beginend <- rbind(prices[1,4],
prices[NROW(prices)/2,4],
prices[NROW(prices),4])/as.numeric(prices[1,4])
beginend <- as.data.frame(cbind(as.Date(index(beginend)),coredata(beginend)),stringsAsFactors=FALSE)
if(middle==FALSE)
beginend <- beginend[c(1,3),]
colnames(beginend) <- c("date","price")
return(beginend)
}
 
df <- as.data.frame(cbind(descr[1],getbeginend(get(tckrs[1]),n)),stringsAsFactors=FALSE)
colnames(df)[1] <- "index"
for (i in 2:length(tckrs)) {
temp <- as.data.frame(cbind(descr[i],getbeginend(get(tckrs[i]),n)),stringsAsFactors=FALSE)
colnames(temp)[1] <- "index"
df <- rbind(df,temp)
}
 
asTheEconomist(direct.label(xyplot(price~as.Date(date),groups=index,data=df,lwd=3,type="l",main="Change Since Dec 2008"),
"last.qp"))
direct.label(
ggplot(aes(y=price,x=as.Date(date)),data=df) + geom_line(aes(colour=index)) + theme_bw() + opts(legend.position = "none") +
#ggplot(aes(y=price,x=as.Date(date)),data=indexes.melt) + geom_smooth(aes(colour=indexes)) + theme_bw() + opts(legend.position = "none") +
opts(panel.grid.minor = theme_blank()) +
opts(axis.line = theme_segment()) +
opts(panel.border = theme_blank()) +
opts(title="Change of Indexes Since 2008")
,list("last.qp",hjust=0.75,vjust=-0.25,cex=0.75))
 
 
#do yearly
indexes <- as.data.frame(to.yearly(get(tckrs[1]))[,4])
for (i in 2:length(tckrs)) {
indexes <- cbind(indexes,as.data.frame(to.yearly(get(tckrs[i]))[,4]))
}
colnames(indexes) <- descr
 
indexes.roc <- ROC(indexes,type="discrete",n=1)
indexes.roc[1,] <- 0
labs <- Return.cumulative(indexes.roc)
#thanks for the fork http://blog.fosstrading.com/
par(mar=c(4,4,4,5))
chart.CumReturns(indexes.roc[,order(labs)],ylab=NA,xlab=NA,colorset=1:5,main="Path of World Indexes Since Dec 2008")
axis(side=4,at=labs,labels=FALSE,las=1,cex.axis=0.75,lwd=0,lwd.ticks=0.5,col.ticks="black",line=-0.25)
mtext(colnames(labs)[order(labs)],cex=0.75, 4, at=labs[order(labs)],las=1,col=1:5,line=1)
 
indexes.cumul <- apply(indexes.roc+1,MARGIN=2,cumprod)
indexes.melt <- melt(as.data.frame(cbind(as.Date(rownames(indexes.cumul)),indexes.cumul),stringsAsFactors=FALSE),
id.vars=1)
colnames(indexes.melt) <- c("date","indexes","price")
direct.label(asTheEconomist(
xyplot(price~as.Date(date),groups=indexes,data=indexes.melt,type="l",main="Path of World Indexes Since Dec 2008"))
,list("last.qp",hjust=0.35,vjust=0.1,cex=1))
 
direct.label(
ggplot(aes(y=price,x=as.Date(date)),data=indexes.melt) + geom_line(aes(colour=indexes)) + theme_bw() + opts(legend.position = "none") +
#to smooth use the next line instead
#ggplot(aes(y=price,x=as.Date(date)),data=indexes.melt) + geom_smooth(aes(colour=indexes)) + theme_bw() + opts(legend.position = "none") +
opts(panel.grid.minor = theme_blank()) +
opts(axis.line = theme_segment()) +
opts(panel.border = theme_blank()) +
opts(title="Path of World Indexes Since Dec 2008")
,list("last.qp",hjust=0.75,vjust=-0.25,cex=0.75))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.