---
title: "ANLY-512 Data Visualization"
Author: "Anirudh Gurnani"
output:
flexdashboard::flex_dashboard:
orientation: rows
social: menu
source_code: embed
---
```{r setup, include=FALSE}
library(quantmod)
library(flexdashboard)
library(dygraphs)
library(xts)
library(tidyverse)
library(TTR)
library(DT)
yahooTickerSymbols = c("FB", "BABA", "NFLX", "GOOGL", "^DJI", "^GSPC")
oneYear = Sys.Date() - 365
getSymbols(yahooTickerSymbols, src = 'yahoo', auto.assign = TRUE, warnings = FALSE, from = oneYear)
yahooTickerSymbols = c("FB", "BABA", "NFLX", "GOOGL", "DJI", "GSPC")
FB = cbind(FB[,-5],FB[,5])
BABA = cbind(BABA[,-5],BABA[,5])
NFLX = cbind(NFLX[,-5],NFLX[,5])
GOOGL = cbind(GOOGL[,-5],GOOGL[,5])
DJI = cbind(DJI[,-5],DJI[,5])
GSPC = cbind(GSPC[,-5],GSPC[,5])
openingPrice = do.call(merge, lapply(yahooTickerSymbols, function(x) {
y = get(x)
if (has.Op(y))
return(y[, grep("Open", colnames(y), ignore.case = TRUE)])
stop("subscript out of bounds: no column name containing \"Open\"")
}))
colnames(openingPrice) = yahooTickerSymbols
closingPrice = do.call(merge, lapply(yahooTickerSymbols, function(x) {
y = get(x)
if (has.Cl(y))
return(y[, grep("Close", colnames(y), ignore.case = TRUE)])
stop("Out of bound subscripts")
}))
colnames(closingPrice) = yahooTickerSymbols
lowPrice = do.call(merge, lapply(yahooTickerSymbols, function(x) {
y = get(x)
if (has.Lo(y))
return(y[, grep("Low", colnames(y), ignore.case = TRUE)])
stop("Out of bound subscripts")
}))
colnames(lowPrice) = yahooTickerSymbols
highPrice = do.call(merge, lapply(yahooTickerSymbols, function(x) {
y = get(x)
if (has.Hi(y))
return(y[, grep("High", colnames(y), ignore.case = TRUE)])
stop("Out of bound subscripts")
}))
colnames(highPrice) = yahooTickerSymbols
volume = do.call(merge, lapply(yahooTickerSymbols, function(x) {
y = get(x)
if (has.Vo(y))
return(y[, grep("Volume", colnames(y), ignore.case = TRUE)])
stop("Out of bound subscripts")
}))
colnames(volume) = yahooTickerSymbols
openClosePercentageChange = do.call(merge, lapply(yahooTickerSymbols, function(x) OpCl(get(x))*100))
colnames(openClosePercentageChange) = yahooTickerSymbols
closeClosePercentageChange = do.call(merge, lapply(yahooTickerSymbols, function(x) ClCl(get(x))*100))[-1,]
colnames(closeClosePercentageChange) = yahooTickerSymbols
mov.avgs<-function(stock.close){
x<-data.frame(stock.close, SMA(stock.close), SMA(stock.close, 25))
colnames(x)<-c(names(stock.close), 'MA_10','MA_25')
return(x)
}
closingPriceWithMA<-lapply(closingPrice, mov.avgs)
FB.CS = cbind(FB[,-6], closingPriceWithMA$FB$MA_10, closingPriceWithMA$FB$MA_25)
colnames(FB.CS)[6] = "MA_10"
colnames(FB.CS)[7] = "MA_25"
BABA.CS = cbind(BABA[,-6], closingPriceWithMA$BABA$MA_10, closingPriceWithMA$BABA$MA_25)
colnames(BABA.CS)[6] = "MA_10"
colnames(BABA.CS)[7] = "MA_25"
NFLX.CS = cbind(NFLX[,-6], closingPriceWithMA$NFLX$MA_10, closingPriceWithMA$NFLX$MA_25)
colnames(NFLX.CS)[6] = "MA_10"
colnames(NFLX.CS)[7] = "MA_25"
GOOGL.CS = cbind(GOOGL[,-6], closingPriceWithMA$GOOGL$MA_10, closingPriceWithMA$GOOGL$MA_25)
colnames(GOOGL.CS)[6] = "MA_10"
colnames(GOOGL.CS)[7] = "MA_25"
DJI.CS = cbind(DJI[,-6], closingPriceWithMA$DJI$MA_10, closingPriceWithMA$DJI$MA_25)
colnames(DJI.CS)[6] = "MA_10"
colnames(DJI.CS)[7] = "MA_25"
GSPC.CS = cbind(GSPC[,-6], closingPriceWithMA$GSPC$MA_10, closingPriceWithMA$GSPC$MA_25)
colnames(GSPC.CS)[6] = "MA_10"
colnames(GSPC.CS)[7] = "MA_25"
# NOT_USED START
# what_metrics <- yahooQF(c("P/E Ratio",
# "Price/EPS Estimate Next Year",
# "200-day Moving Average",
# "Market Capitalization",
# "Percent Change From 200-day Moving Average",
# "52-week High",
# "52-week Low"
# ))
# symbols = "FB;BABA;NFLX;GOOGL;"
# metrics <- data.frame(getQuote(paste(symbols, sep="", collapse=";"),what=what_metrics))[, -1]
# metrics$Market.Capitalization = metrics$Market.Cap/1000000000
# metrics$Market.Capitalization = round(metrics$Market.Cap, digits = 2)
# colnames(metrics) = c("PE.Ratio", "EPS.Estimate.Next.Year", "Market.Cap.In.Billions",
# "200.Day.MA", "Percentage.Change.From.200.Day.MA", "52.Week.High", "52.Week.Low")
# NOT_USED END
closingPrice.df = data.frame(closingPrice)
endIndex = length(closingPrice.df$FB)
Fb_KPI = round((closingPrice.df$FB[endIndex] - closingPrice.df$FB[1])*100/closingPrice.df$FB[1], digits = 2)
Baba_KPI = round((closingPrice.df$BABA[endIndex] - closingPrice.df$BABA[1])*100/closingPrice.df$BABA[1], digits = 2)
Nflx_KPI = round((closingPrice.df$NFLX[endIndex] - closingPrice.df$NFLX[1])*100/closingPrice.df$NFLX[1], digits = 2)
Googl_KPI = round((closingPrice.df$GOOGL[endIndex] - closingPrice.df$GOOGL[1])*100/closingPrice.df$GOOGL[1], digits = 2)
DJI_KPI = round((closingPrice.df$DJI[endIndex] - closingPrice.df$DJI[1])*100/closingPrice.df$DJI[1], digits = 2)
GSPC_KPI = round((closingPrice.df$GSPC[endIndex] - closingPrice.df$GSPC[1])*100/closingPrice.df$GSPC[1], digits = 2)
get.color = function(metric){
retColor = ""
if (metric > 10) {
retColor = "success"
}else if (metric > 5) {
retColor = "primary"
}else if (metric <= 5 & metric > 0) {
retColor = "warning"
}else {
retColor = "danger"
}
return(retColor)
}
convert.kpis = function(vector){
vectorString = c()
for (i in 1: length(vector)){
if (vector[i] > 0){
vectorString[i] = paste("+", toString(vector[i]), "%", sep = "")
}else{
vectorString[i] = paste("-", toString(vector[i]), "%", sep = "")
}
}
return(vectorString)
}
KPIS = c(Fb_KPI, Baba_KPI, Nflx_KPI, Googl_KPI, DJI_KPI, GSPC_KPI)
StringKPIS = convert.kpis(KPIS)
```
Key Performance Indices and Candle Sticks {data-orientation=rows}
==========================================================================
Sidebar {.sidebar}
-----------------------------------------------------------------------
### Stock Analyses
In this dashboard, the prices of 4 companies: Facebook, Alibaba, Netflix and Google from the last 1 year are analysed. The prices of each of the stocks are compared against the Dow-Jones Index and S&P 500.
Row {data-height=200}
-----------------------------------------------------------------------
### Facebook
```{r}
valueBox(value = StringKPIS[1],
color = get.color(Fb_KPI))
```
### AliBaba
```{r}
valueBox(value = StringKPIS[2],
color = get.color(Baba_KPI))
```
### Netflix
```{r}
valueBox(value = StringKPIS[3],
color = get.color(Nflx_KPI))
```
### Google
```{r}
valueBox(value = StringKPIS[4],
color = get.color(Googl_KPI))
```
### Dow Jones Index
```{r}
valueBox(value = StringKPIS[5],
color = get.color(DJI_KPI))
```
### S&P 500 Index
```{r}
valueBox(value = StringKPIS[6],
color = get.color(GSPC_KPI))
```
Row {.tabset .tabset-fade}
-----------------------------------------------------------------------
### Facebook - FB
```{r}
dyLegend(dyRangeSelector(dyHighlight(dyOptions(dyCandlestick(dyAxis(dyUnzoom(dygraph(FB.CS, main = "Facebook")), "y", label="Price")), colors= c(RColorBrewer::brewer.pal(5, "Set2"), "red", "blue")), highlightCircleSize = 2, highlightSeriesOpts = list(strokeWidth = 2)), height = 25), show = "onmouseover")
```
### Alibaba - BABA
```{r}
dyLegend(dyRangeSelector(dyHighlight(dyOptions(dyCandlestick(dyAxis(dyUnzoom(dygraph(BABA.CS, main = "Alibaba")), "y", label="Price")), colors= c(RColorBrewer::brewer.pal(5, "Set2"), "red", "blue")), highlightCircleSize = 2, highlightSeriesOpts = list(strokeWidth = 2)), height = 25), show = "onmouseover")
```
### Netflix - NFLX
```{r}
dyLegend(dyRangeSelector(dyHighlight(dyOptions(dyCandlestick(dyAxis(dyUnzoom(dygraph(NFLX.CS, main = "Netflix")), "y", label="Price")), colors= c(RColorBrewer::brewer.pal(5, "Set2"), "red", "blue")), highlightCircleSize = 2, highlightSeriesOpts = list(strokeWidth = 2)), height = 25), show = "onmouseover")
```
### Google - GOOGL
```{r}
dyLegend(dyRangeSelector(dyHighlight(dyOptions(dyCandlestick(dyAxis(dyUnzoom(dygraph(GOOGL.CS, main = "Google")), "y", label="Price")), colors= c(RColorBrewer::brewer.pal(5, "Set2"), "red", "blue")), highlightCircleSize = 2, highlightSeriesOpts = list(strokeWidth = 2)), height = 25), show = "onmouseover")
```
### Dow Jones Index - DJI
```{r}
dyLegend(dyRangeSelector(dyHighlight(dyOptions(dyCandlestick(dyAxis(dyUnzoom(dygraph(DJI.CS, main = "Dow Jones Index")), "y", label="Price")), colors= c(RColorBrewer::brewer.pal(5, "Set2"), "red", "blue")), highlightCircleSize = 2, highlightSeriesOpts = list(strokeWidth = 2)), height = 25), show = "onmouseover")
```
### S&P 500 Index - GSPC
```{r}
dyLegend(dyRangeSelector(dyHighlight(dyOptions(dyCandlestick(dyAxis(dyUnzoom(dygraph(GSPC.CS, main = "S&P 500 Index")), "y", label="Price")), colors= c(RColorBrewer::brewer.pal(5, "Set2"), "red", "blue")), highlightCircleSize = 2, highlightSeriesOpts = list(strokeWidth = 2)), height = 25), show = "onmouseover")
```
Stock Return {data-orientation=rows}
==========================================================================
Sidebar {.sidebar}
-----------------------------------------------------------------------
### Payment Stocks Analyses
In this dashboard, the stock returns, opening price, closing price, volume for each of the stock: Facebook, Alibaba, Netflix and Google is plotted. The return is calculated relative to the adjustment value on the first day.
Row {.tabset .tabset-fade}
-----------------------------------------------------------------------
### Stock Returns
```{r}
stocks <- cbind(FB$FB.Adjusted, BABA$BABA.Adjusted, NFLX$NFLX.Adjusted, GOOGL$GOOGL.Adjusted)
stock_return = as.xts(t(apply(stocks, 1, function(x) {x / stocks[1,]})))
dyRangeSelector(dyHighlight(dyOptions(dyAxis(dyUnzoom(dygraph(stock_return, main = "Stock Returns")), "y", label="Stock Returns"), colors = RColorBrewer::brewer.pal(6, "Set2")), highlightSeriesOpts = list(strokeWidth = 3)), height = 25)
```
### Opening Prices
```{r}
dyRangeSelector(dyHighlight(dyOptions(dyAxis(dyUnzoom(dygraph(openingPrice[,1:4], main = "Opening Price")), "y", label="Opening Price"), colors = RColorBrewer::brewer.pal(6, "Set2")), highlightSeriesOpts = list(strokeWidth = 3)), height = 25)
```
### Closing Prices
```{r}
dyRangeSelector(dyHighlight(dyOptions(dyAxis(dyUnzoom(dygraph(closingPrice[,1:4], main = "Closing Price")), "y", label="Closing Price"), colors = RColorBrewer::brewer.pal(6, "Set2")), highlightSeriesOpts = list(strokeWidth = 3)), height = 25)
```
### Volume
```{r}
dyRangeSelector(dyHighlight(dyOptions(dyAxis(dyUnzoom(dygraph(volume[,1:4], main = "Volume")), "y", label="Volume"), colors = RColorBrewer::brewer.pal(6, "Set2")), highlightSeriesOpts = list(strokeWidth = 3)), height = 25)
```