Live Rolling Correlation Plot

Open source is amazing! I cannot even start to imagine the amount of work invested in R, in firefox browser (Mozilla), or Rstudio IDE, all of which are used extensively around the globe, free. Not free as in: free sample till you decide to upgrade, or: sure it’s free, just watch this one minute commercial every time you need to use it,  but free, as in: we think it might make your life better, enjoy. Warms the heart, in direct opposite to the fabulous fabs out there, that instead of contributing to a better, safer society, set it back and get paid for it (see appendix). Character is also normally distributed I guess.

Jeroen C.L. Ooms, (and friends probably) started OpenCPU, an R based implementation of Open Computing, transparent and reproducible. I took a close look and decided to try it out. I will briefly show what you can do with it.

In financial markets, there is empirical evidence for higher correlation between sectors when markets weaken, lower correlation when markets are on the up. It is part of a wider phenomena called Financial Contagion. I have created a graph of rolling average correlation between sectors (sectors ETF were used as proxies). I check nine sectors, “utilities”, “financial” etc., calculate correlation between each pair, and average them. This give a measure for the “closeness” between the sectors. Rolling means I use only the  most recent two weeks at each point in time for the calculation. What we should be able to see is that when market is going up, average correlation declines or the reverse. Another option is that the measure has some predictive power to it, but I don’t believe it is so, in any case we can use it as an extra tool to decide if we are bears or bulls (or pigeons.. for neutral). What’s OpenCPU has to do with it?

OpenCPU allows you is to upload your own function which is rendered on the server side and outputs to your browser. It is still in diapers, so it’s possible that the link will not work at some point, I uploaded it in their “temp” directory. The way the function is built is that you should be able to view the correlation in real time, as every click will execute the function anew. Check it out!, it produces a pdf file with the figure that was generated as described. You can also get other formats, click on me!. The nice thing about it is that next time you are interested, the pdf file is updated since the click calls the function again, so the graph is “alive” in that sense.
Code and appendix below, thanks for reading.

Appendix:
Comments:
1. When I use “Yahoo” to get the quotes, about 40% of the time something goes wrong, google as source is more stable. Yet also not 100%, you might get: “Error: cannot open URL ‘http://finance.google.com/finance/historical?q=XLF&startdate=Feb+19,+2011&enddate=Feb+19,+2012&output=csv’”, just refresh and try again.
2. OpenCPU has nice interface to upload your work, if you made it this far, you don’t need my explanation about how to use it.
3. The aggregation site Rbloggers managed by Tal Galili is also a form of free service, it is on a smaller scale, not firefox browser yet, but it’s on the move.

* Fabulous Fabs:

Related Books:




?View Code RSPLUS
fun = function(){
library(quantmod)
symetf = c('XLY','XLP','XLE','XLF','XLV','XLI','XLB','XLK','XLU','SPY')
end<- format(Sys.Date(),"%Y-%m-%d") 
start<-format(Sys.Date() - 365,"%Y-%m-%d")
dat0 = (getSymbols(symetf[1], src="google", from=start, to=end, auto.assign = F, warnings = FALSE,symbol.lookup = F))
n = NROW(dat0)  ; l = length(symetf)
dat = array(dim = c(n,NCOL(dat0),l)) ; ret = matrix(nrow = n, ncol = l) 
for (i in 1:l){
 dat0 = (getSymbols(symetf[i], src="google", from=start, to=end, auto.assign = F,warnings = FALSE,symbol.lookup = F))
dat[1:n,,i] = dat0 
ret[2:n,i] = dat[2:n,4,i]/dat[1:(n-1),4,i] - 1
 }
rolcor = NULL ; h = 10 # 10 bussiness days is two weeks
for (i in 2:(n-h)){
rolcor[i+h] = mean(cor(ret[i:(i+h),])[lower.tri(cor(ret[i:(i+h),]))]) # just the rolling average correlation
}
par( mfrow = c(2,1), bg = "white", bty ="n", fg = gray(0.3) ,font.lab = 6, font.axis = 6, #xaxp =  c(x1, x2, n = 2)
font.main = 6, col.axis = gray(0.3) , col.lab = gray(0.3) , pch = 21,  tck = -0.02, #tck is the length of the axis spikes 
 xaxs = "r")  # Graph parameters
lwd1 = 2.5
plot(rolcor~index(dat0), ty = "l",lwd = lwd1, xlab = "Time",ylab = "Average Correlation", main = "Two Weeks Rolling Correlation")
plot(dat[1:n,4,10]~index(dat0), ty = "l", lwd = lwd1, xlab = "Time", ylab = "SPY Price Level",main ="SPY Price Level")
}
 
fun()

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>