Created
September 15, 2011 21:32
-
-
Save zachmayer/1220538 to your computer and use it in GitHub Desktop.
Stock Correlations
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
#Load Data | |
rm(list = ls(all = TRUE)) | |
library(quantmod) | |
library(PerformanceAnalytics) | |
symbols <- c('XLE','XLV','XLI','XLU','XLP','IYZ','XLK','XLY','XLF','XLB','GLD','SLV','EFA','EEM','FXA','FXE','FXY','HYG','LQD') | |
getSymbols(symbols,from='2007-01-01') | |
getSymbols('SPY',from='2007-01-01') | |
SP500 <- Cl(SPY) | |
colnames(SP500)[1] <- 'SPY' | |
#Function to build a dataframe form a list of symbols | |
symbolFrame <- function(symbolList) { | |
Data <- data.frame(NULL) | |
for (S in symbolList) { | |
Data <- cbind(Data,Cl(get(S))) | |
} | |
colnames(Data) <- symbolList | |
return(Data) | |
} | |
#Make a color palette for the graphj | |
library(fBasics) | |
colorset <- qualiPalette(length(symbols), name="Set1") | |
#Chart Correlations | |
Data <- symbolFrame(symbols) | |
Data <- Data['2010-01-01::'] | |
chart.RollingCorrelation(Data, SP500, legend.loc="bottomleft",colorset=colorset, main = "Rolling 3-month Correlation",width=90) |
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
#Modified version of chart.RollingCorrelation that returns the data rather than plotting it | |
table.RollingCorrelation <- function (Ra, Rb, width = 12, na.pad = FALSE, ...) | |
{ | |
Ra = checkData(Ra) | |
Rb = checkData(Rb) | |
columns.a = ncol(Ra) | |
columns.b = ncol(Rb) | |
columnnames.a = colnames(Ra) | |
columnnames.b = colnames(Rb) | |
for (column.a in 1:columns.a) { | |
for (column.b in 1:columns.b) { | |
merged.assets = merge(Ra[, column.a, drop = FALSE], | |
Rb[, column.b, drop = FALSE]) | |
column.calc = rollapply(na.omit(merged.assets[, , | |
drop = FALSE]), width = width, FUN = function(x) cor(x[, | |
1, drop = FALSE], x[, 2, drop = FALSE]), by = 1, | |
by.column = FALSE, na.pad = na.pad, align = "right") | |
column.calc.tmp = xts(column.calc) | |
colnames(column.calc.tmp) = paste(columnnames.a[column.a], | |
columnnames.b[column.b], sep = " to ") | |
column.calc = xts(column.calc.tmp, order.by = time(column.calc)) | |
if (column.a == 1 & column.b == 1) | |
Result.calc = column.calc | |
else Result.calc = merge(Result.calc, column.calc) | |
} | |
} | |
return(Result.calc) | |
} | |
#Calculate mean correlation among US Sectors & plot | |
sectors <- c('XLE','XLV','XLI','XLU','XLP','IYZ','XLK','XLY','XLF','XLB') | |
corrs <- table.RollingCorrelation(symbolFrame(sectors), SP500, width=90) | |
meancorr <- apply(corrs,1,mean) | |
meancorr <- xts(meancorr,order.by=index(corrs)) | |
plot(meancorr, main = "Mean Rolling 3-month Correlation Among Major US Sectors") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment