Preliminaries
cat("\014")
rm(list=ls())
#Load the required packages.
if(!require(pacman)) install.packages('pacman')
## Loading required package: pacman
pacman::p_load(xts,nloptr,zoo,dygraphs,plotly,magrittr,webshot,fBasics,dplyr,DescTools,ggplot2)
compute the log-returns
ticker <- read.table('./data/Tickers.txt', header=FALSE)
data_xts <- NULL
for (k in 2:dim(ticker)[1])
{
prices<-read.csv(paste("./data/", ticker$V1[k],".txt",sep =''))[ , c("DATE", "CLOSE")]
rets <- c(NA, diff(log(prices$CLOSE)))
dates <- as.Date(toString(prices$DATE[1]), '%Y%m%d')
for (t in 2:dim(prices)[1]) {dates <- c(dates, as.Date(toString(prices$DATE[t]), '%Y%m%d'))}
rets_xts <- xts(rets, order.by = dates)
colnames(rets_xts) <- ticker[k, 1]
if (is.null(data_xts)) {data_xts <- rets_xts}
else {data_xts <- merge(data_xts, rets_xts)}
}
plot 252-day moving avg and sharpe ratio of MA
#the stock with highest sharpe ratio is MA
mean_MA <- mean_roll[, 60]
sd_MA <- sd_roll[, 60]
sp_MA <- mean_MA/sd_MA
#draw the dygrpahs
dygraph(mean_MA,ylab = "estimated mean", main = "252-Day Moving Avg of MA") %>%
dyOptions(axisLineWidth = 1.5, fillGraph = FALSE, drawGrid = T, rightGap=50) %>%
dyRangeSelector()
dygraph(sp_MA, xlab = "Date", ylab = "Sharpe Ratio", main = "252-Day Moving Sharpe Ratio of MA") %>%
dyOptions(axisLineWidth = 1.5, fillGraph = FALSE, drawGrid = T, rightGap=50) %>%
dyRangeSelector()
##the results are not always positive. In yeaer 2009, the ratio is mostly negative. From 2007 to mid 2009, there is a decreasing tendency throughout and then gradually increase untill 2014 and then drop down again after that.