library(shiny)
if (!require(quantmod)) {
stop("This app requires the quantmod package. To install it, run 'install.packages(\"quantmod\")'.\n")
}
# Download data
# 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() - (3*365),"%Y-%m-%d") # 3 years of data
dat0 = (getSymbols(symetf[1], src="google", from=start, to=end, ascii = TRUE,
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,4,i] <- as.numeric(dat0[,4])
ret[2:n,i] = dat[2:n,4,i]/dat[1:(n-1),4,i] - 1
}
time <- index(dat0)
tickm <- seq.Date(time[length(time)], time[1], by="-1 month")
ticky <- as.Date(tapply(time,format(time,format="%y"),min))
shinyServer(function(input, output) {
RollCorr = function(h){
rolcor = NULL
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(1,1), bg = gray(1/1.05), bty ="n", fg = 1 ,font.lab = 6,
font.axis = 6, font.main = 6, col.axis = 1, col.lab = 1,cex.axis=1.3,
pch = 21, tck = -0.02, cex.lab=1.5, cex.main = 2, mar = c(5, 4, 4, 3) + 0.4)
plot(rolcor~time, las=1, ylab = "",ty="l",lwd=2,col=1,xlab="",
main = "Rolling Correlation", pch=19 ,xaxt="n", las=1,cex.lab=1.3)
labb <- substr(ticky,1,4)
axis(1, at=ticky, labels=(labb),tck = -0.04, lwd=0.2,
font=2, cex.axis=1.3, hadj=0,xlab="")
axis(1, at=tickm, labels=rep("",length(tickm)),tck = -0.02)
grid(col="darkgrey")
tempcol <- "green"
par(new=TRUE)
plot(dat[1:n,4,10]~index(dat0),main="",bty="n",ty="l",pch=19,xlab="",ylab="",
axes=F, col=tempcol, lwd=1)
axis(4,at=pretty(dat[1:n,4,10]),col=tempcol, col.ticks=tempcol,col.axis=tempcol,
las=1,cex.axis=1.3)
}
output$Corrplot <- reactivePlot(function() {
RollCorr(h = input$numweeks*5) # 5 bussiness days in a week
})
})
# Define UI for dataset viewer application
shinyUI(fluidPage(
titlePanel(title=HTML("Live rolling Correlation - Most Recent 3 Years")),
sidebarPanel(
numericInput("numweeks", "Number of weeks for moving window calculation:", value = 4, min = 1,
max = 150, step = 1),
tags$div(class="header", checked=NA,
tags$p("
Using the 10 ETF's: 'XLY','XLP','XLE','XLF','XLV','XLI','XLB','XLK','XLU','SPY',
the black line corresponds to the average of the pairwise correlation calculated based on daily returns (close to close). In green, the SPY ETF which tracks the S&P 500.
"), tags$p("You can change the number of weeks you wish to use as a rolling window in the box above. A lower number will give
a more 'choppy' line, but more up-to-date, a high number will result in a smooth line, but calculation is then based on data further in the past."),
tags$p("The function to generate to plot is available under the code section")
)
),
mainPanel(
h3(textOutput("Live Rolling Correlation Plot")),
plotOutput("Corrplot", width = "100%", height = "700px")
)
))