title: “Lab 1 ANLY 512” author: “Shridhar Kulkarni” Date: “December 6 2019” output: fexdashboard::flex_dashboard: orientation: rows source_code: embed vertical_layout: scroll —
### Stock Price - BAYRY See stock price trend to get a general sense of the performance of the four stocks.
tickers <- c("BAYRY", "MRK", "ABBV", "PFE")
symbol <- getSymbols(tickers, src = 'yahoo', auto.assign = TRUE, warnings = FALSE, from = '2016-10-01')
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
##
## This message is shown once per session and may be disabled by setting
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
BAYRY <- BAYRY[,1:4]
MRK <- MRK[,1:4]
ABBV <- ABBV[,1:4]
PFE <- PFE[,1:4]
dygraph(BAYRY, main = "Bayer") %>%
dyCandlestick() %>%
dyAxis("y", label="Price") %>%
dyOptions(colors= RColorBrewer::brewer.pal(5, "Set2")) %>%
dyHighlight(highlightCircleSize = 4,
highlightSeriesBackgroundAlpha = 1) %>%
dyRangeSelector(height = 55)%>%
dyLegend(show = "onmouseover")
dygraph(MRK, main = "Merck") %>%
dyCandlestick() %>%
dyAxis("y", label="Price") %>%
dyOptions(colors= RColorBrewer::brewer.pal(5, "Set2")) %>%
dyHighlight(highlightCircleSize = 4,
highlightSeriesBackgroundAlpha = 1) %>%
dyRangeSelector(height = 55)%>%
dyLegend(show = "onmouseover")
dygraph(ABBV, main = "Abbvie") %>%
dyCandlestick() %>%
dyAxis("y", label="Price") %>%
dyOptions(colors= RColorBrewer::brewer.pal(5, "Set2")) %>%
dyHighlight(highlightCircleSize = 4,
highlightSeriesBackgroundAlpha = 1) %>%
dyRangeSelector(height = 55)%>%
dyLegend(show = "onmouseover")
dygraph(PFE, main = "Pfizer") %>%
dyCandlestick() %>%
dyAxis("y", label="Price") %>%
dyOptions(colors= RColorBrewer::brewer.pal(5, "Set2")) %>%
dyHighlight(highlightCircleSize = 4,
highlightSeriesBackgroundAlpha = 1) %>%
dyRangeSelector(height = 55)%>%
dyLegend(show = "onmouseover")
getSymbols(c("BAYRY", "MRK", "ABBV", "PFE"), from = '2016-10-01', to = '2017-10-29')
## [1] "BAYRY" "MRK" "ABBV" "PFE"
getSymbols('USD1MTD156N', src = 'FRED')
## [1] "USD1MTD156N"
BAYRY.ret <- periodReturn(BAYRY, period = 'daily')
MRK.ret <- periodReturn(MRK, period = 'daily')
ABBV.ret <- periodReturn(ABBV, period = 'daily')
PFE.ret <- periodReturn(PFE, period = 'daily')
ret <- merge(BAYRY.ret, MRK.ret, ABBV.ret, PFE.ret)
colnames(ret) <- c('BAYRY.ret', 'MRK.ret', 'ABBV.ret', 'PFE.ret')
rf <- mean(USD1MTD156N['2016/10 to 2017/10'], na.rm = T) / 100
# Average 1-M LIBOR as Risk-Free Rate for simplification
### Rolling Sharpe-Ratio for four Stocks
mean_roll_30 <- rollapply(ret, 30, FUN = mean)
mean_roll_30 <- mean_roll_30[complete.cases(mean_roll_30), ]
sd_roll_30 <- rollapply(ret, 30, FUN = sd)
sd_roll_30 <- sd_roll_30[complete.cases(sd_roll_30), ]
sharp.ratio <- (mean_roll_30 - rf) / sd_roll_30
colnames(sharp.ratio) <- c('BAYRY_SR', 'MRK_SR', 'ABBV_SR', 'PFE_SR')
sharp.ratio.df <- data.frame(date=index(sharp.ratio), coredata(sharp.ratio))
sharp.ratio.melt <- gather(sharp.ratio.df, key = 'company', value = 'Sharp_Ratio', -date)
ggplot(data = sharp.ratio.melt, aes(x = date, y = Sharp_Ratio)) +
geom_line(alpha = 0.3) +
geom_hline(yintercept = 0) +
facet_grid(company ~.) +
ggtitle('30-Day Rolling Sharp Ratio for Bayer, Merck, Abbvie & Pfizer \n (2016.10-2017.10)') +
xlab('Time') +
ylab('Shart Ratio')
count.sharp <- apply(sharp.ratio > 0, 2, sum)
# From the result we found that TWTR has the most days with positive sharpe ratio
### Current Basic Metrics
Basic metrics for four stocks, in order to frame a general picture and details of the four stocks.
what_m <- yahooQF(c("Price/Sales",
"P/E Ratio",
"Price/EPS Estimate Next Year",
"PEG Ratio",
"Dividend Yield",
"Market Capitalization"))
ms <- getQuote(paste(tickers, sep="", collapse=";"), what=what_m)
ms <- data.frame(Symbol=tickers, ms[,1:length(ms)])
colnames(ms) <- c("Symbol", "Revenue Multiple", "Earnings Multiple", "Price-to-Earnings-Growth", "Div Yield", "Market Cap")
DT::datatable(ms)
### Monthly Return
Monthly return analysis, to see if it’s possible to get the highest monthly return for short term investment.
m.rt.BAYRY <- monthlyReturn(BAYRY)
m.rt.MRK <- monthlyReturn(MRK)
m.rt.ABBV <- monthlyReturn(ABBV)
m.rt.PFE <- monthlyReturn(PFE)
mg.return <- merge.xts(m.rt.BAYRY,m.rt.MRK, m.rt.ABBV, m.rt.PFE)
colnames(mg.return) <- c('Bayer','Merck','Abbvie','Pfizer')
dygraph(mg.return, main = "Monthly Return") %>%
dyAxis("y", label = "Return") %>%
dyOptions(colors = RColorBrewer::brewer.pal(5, "Set2")) %>%
dyHighlight(highlightSeriesBackgroundAlpha = 0.5,
highlightSeriesOpts = list(strokeWidth = 4)) %>%
dyRangeSelector(height = 55)
tickers <- c("BAYRY", "MRK", "ABBV", "PFE")
symbol <- getSymbols(tickers, src = 'yahoo', auto.assign = TRUE, warnings = FALSE, from = '2017-09-27')
BAYRY_return<-dailyReturn(BAYRY$BAYRY.Adjusted)
plot(BAYRY_return, xlab="Time",ylab = "Return",main = "Bayer Daily Return in October 2017")
MRK_return<-dailyReturn(MRK$MRK.Adjusted)
plot(MRK_return, xlab="Time",ylab = "Return",main = "Merck Daily Return in June 2017")
ABBV_return<-dailyReturn(ABBV$ABBV.Adjusted)
plot(ABBV_return, xlab="Time",ylab = "Return",main = "Abbvie Daily Return in June 2017")
PFE_return<-dailyReturn(PFE$PFE.Adjusted)
plot(PFE_return, xlab="Time",ylab = "Return",main = "Pfizer Daily Return in June 2017")