Column

Closing Price Comparison

Banco Santander

Deutsche Bank

Wells Fargo

HSBC

Row

Stock Price - Wells Fargo

Stock Price - HSBC

Stock Price - Deutsche Bank

Stock Price - Banco Santander

---
title: "ANLY 512 - Dashboard Laboratory"
author: "235208"
date: "`r Sys.Date()`"
output:
    flexdashboard::flex_dashboard:
      source_code: embed


---


```{r setup, message=FALSE, warning=FALSE}
if (!require("flexdashboard")) {
install.packages("flexdashboard")
library(flexdashboard)
}
if (!require("plyr")) {
install.packages("plyr")
library(plyr)
}
if (!require("DT")) {
install.packages("DT")
library(DT)
}
if (!require("dygraphs")) {
install.packages("dygraphs")
library(dygraphs)
}
if (!require("rlang")) {
install.packages("rlang")
library(rlang)
}
if (!require("colorspace")) {
install.packages("colorspace")
library(colorspace)
}
if (!require("tidyverse")) {
install.packages("tidyverse")
library(tidyverse)
}
if (!require("tidyquant")) {
install.packages("tidyquant")
library(tidyquant)
}

#knitr::opts_chunk$set(echo = FALSE, source_code = TRUE)
```


Column {data-width=650 .tabset .tabset-fade}
--------------


### Closing Price Comparison
```{r, echo=FALSE, message = FALSE}

ticker <- c("SAN", "DB", "WFC", "HSBC")
invisible(getSymbols(ticker, from="2016-01-01", to="2019-01-15"))
closing_price <- do.call(merge, lapply(ticker, function(x) Cl(get(x))))

dateperiod<-c("2016-01-01", "2019-01-15")
dygraph(closing_price, main="Closing Price Since 2016 (USD)", group="Stock") %>%
    dyAxis("y", label="Closing Price (USD)") %>%
    dyOptions(axisLineWidth = 2.0,  colors = RColorBrewer::brewer.pal(5, "Set2")) %>%
    dyHighlight(highlightSeriesBackgroundAlpha = 1.0,
              highlightSeriesOpts = list(strokeWidth = 3)) %>%
    dyRangeSelector(height = 40)
```


### Banco Santander 
```{r message=FALSE, warning=FALSE} 
startdate <- ymd("2016-01-01")
invisible(getSymbols("SAN", src = "yahoo", from=startdate))
SAN <- SAN[,-5]
colnames(SAN) <- c("Open","High","Low","Close","Adjusted")
```

```{r message=FALSE, warning=FALSE}
dygraph(SAN, main = "Banco Santander Stock Price Since 2016 (USD)") %>%
  dyAxis("y", label="Price (USD)") %>%
  dyOptions(axisLineWidth = 1.5,  colors = RColorBrewer::brewer.pal(5, "Set1")) %>%
  dyCandlestick()
```

### Deutsche Bank
```{r message=FALSE, warning=FALSE}
startdate <- ymd("2016-01-01")
invisible(getSymbols("DB", src = "yahoo", from=startdate))
DB <- DB[,-5]
colnames(DB) <-c ("Open","High","Low","Close","Adjusted")
```

```{r message=FALSE, warning=FALSE}
dygraph(DB, main = "Deutsche Bank Stock Price Since 2016 (USD)") %>%
  dyAxis("y", label="Price (USD)") %>%
  dyOptions(axisLineWidth = 1.5,  colors = RColorBrewer::brewer.pal(5, "Set1")) %>%
  dyCandlestick()
```

### Wells Fargo
```{r message=FALSE, warning=FALSE}
startdate <- ymd("2016-01-01")
invisible(getSymbols("WFC", src = "yahoo", from=startdate))
WFC <- WFC[,-5]
colnames(WFC) <- c("Open","High","Low","Close","Adjusted")
```

```{r message=FALSE, warning=FALSE}
dygraph(WFC, main = "Wells Fargo Stock Price Since 2016 (USD)") %>%
  dyAxis("y", label="Price (USD)") %>%
  dyOptions(axisLineWidth = 1.5,  colors = RColorBrewer::brewer.pal(5, "Set1")) %>%
  dyCandlestick()
```

### HSBC
```{r message=FALSE, warning=FALSE}
startdate <- ymd("2016-01-01")
invisible(getSymbols("HSBC", src = "yahoo", from=startdate))
HSBC <- HSBC[,-5]
colnames(HSBC) <- c("Open","High","Low","Close","Adjusted")
```

```{r message=FALSE, warning=FALSE}
dygraph(HSBC, main = "HSBC Stock Price Since 2016 (USD)") %>%
  dyAxis("y", label="Price (USD)") %>%
  dyOptions(axisLineWidth = 1.5,  colors = RColorBrewer::brewer.pal(5, "Set1")) %>%
  dyCandlestick()
```


Row {.tabset .tabset-fade}
--------------

```{r message=FALSE, warning=FALSE}

what_metrics <- yahooQF(c("Price/Sales", 
                          "P/E Ratio",
                          "Price/EPS Estimate Next Year",
#                         "PEG Ratio",
                          "Dividend Yield", 
                          "Market Capitalization"))

tickers <- c("SAN", "DB", "WFC", "HSBC")
# Not all the metrics are returned by Yahoo.
metrics <- getQuote(paste(tickers, sep="", collapse=";"), what=what_metrics)

#Add tickers as the first column and remove the first column which had date stamps
metrics <- data.frame(Symbol=tickers, metrics[,2:length(metrics)]) 

#Change colnames
colnames(metrics) <- c("Symbol", "P-E Ratio", "Price EPS Estimate Next Year", "Div Yield", "Market Cap")

#Persist this to the csv file
#write.csv(metrics, "FinancialMetrics.csv", row.names=FALSE)
#DT::datatable(metrics) %>%
#formatRound(columns=c('P-E Ratio', 'Price EPS Estimate Next Year', 'Div Yield'), digits=3)

datatable(head(metrics, 20), options = list(
  initComplete = JS(
    "function(settings, json) {",
    "$(this.api().table().header()).css({'background-color': 'grey', 'color': '#fff'});",
    "}")
))

  
```



### Stock Price - Wells Fargo

```{r}
race_prices  <- tq_get("WFC", get = "stock.prices", from = " 2016-01-01")
race_prices %>%
   ggplot(aes(x = date, y = close)) +
    geom_candlestick(aes(open = open, close = close, high = high, low = low)) + 
    geom_bbands(aes(high = high, low = low, close = close),
                ma_fun = SMA, n = 20, sd = 2, size = 1) + 
    labs(title = "Candlestick Charts: Wells Fargo",
         x = "stock price (2016~2019)", y = "Closing Price") +
  coord_x_date(xlim = c(ymd("2019-01-15") - weeks(20), ymd("2019-01-15")))

```

### Stock Price - HSBC

```{r}
tm_prices  <- tq_get("HSBC", get = "stock.prices", from = " 2016-01-01")
tm_prices %>%
   ggplot(aes(x = date, y = close)) +
    geom_candlestick(aes(open = open, close = close, high = high, low = low)) + 
    geom_bbands(aes(high = high, low = low, close = close),
                ma_fun = SMA, n = 20, sd = 2, size = 1) + 
    labs(title = "Candlestick Charts: HSBC",
         x = "stock price (2016~2019)", y = "Closing Price") +
  coord_x_date(xlim = c(ymd("2019-01-15") - weeks(20), ymd("2019-01-15")))
```


### Stock Price - Deutsche Bank

```{r}
tsla_prices  <- tq_get("DB", get = "stock.prices", from = " 2016-01-01")
tsla_prices %>%
   ggplot(aes(x = date, y = close)) +
    geom_candlestick(aes(open = open, close = close, high = high, low = low)) + 
    geom_bbands(aes(high = high, low = low, close = close),
                ma_fun = SMA, n = 20, sd = 2, size = 1) + 
    labs(title = "Candlestick Charts: Deutsche Bank",
         x = "stock price (2016~2019)", y = "Closing Price") +
  coord_x_date(xlim = c(ymd("2019-01-15") - weeks(20), ymd("2019-01-15")))

```

### Stock Price - Banco Santander

```{r}
gm_prices <- tq_get("SAN", get = "stock.prices", from = "2016-01-01")
gm_prices %>%
  
  ggplot(aes(x = date, y = close)) +
    geom_candlestick(aes(open = open, high = high, low = low, close = close)) + 
    geom_bbands(aes(high = high, low = low, close = close),
                ma_fun = SMA, n = 20, sd = 2, size = 1) + 
    labs(title = "Candlestick Charts: Banco Santander",
         x = "stock price (2016~2019)", y = "Closing Price") +
    coord_x_date(xlim = c(ymd("2019-01-15") - weeks(20), ymd("2019-01-15")))
```