Skip to content

Instantly share code, notes, and snippets.

Created July 2, 2012 21:07
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save anonymous/3035700 to your computer and use it in GitHub Desktop.
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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment