---
title: "Dashboard Lab"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
source_code: embed
---
```{r, include=FALSE}
library(flexdashboard)
library(tidyquant)
SWIR <- tq_get("SWIR", get = "stock.prices", from = "2007-01-01", to = "2017-01-01")
TXN <- tq_get("TXN", get = "stock.prices", from = "2007-01-01", to = "2017-01-01")
QCOM <- tq_get("QCOM", get = "stock.prices", from = "2007-01-01", to = "2017-01-01")
CSCO <- tq_get("CSCO", get = "stock.prices", from = "2007-01-01", to = "2017-01-01")
FANG <- c("SWIR", "TXN", "QCOM", "CSCO") %>% tq_get(get = "stock.prices", from = "2007-01-01", to = "2017-01-01")
```
```{r,include=FALSE}
## set up dates
end <- ymd("2017-01-01")
start <- end - weeks(20)
```
```{r, include=FALSE}
## GEOM CANDLESTICK
SWIR %>% ggplot(aes(x = date, y = close)) + geom_candlestick(aes(open = open, close = close, high = high, low = low)) +labs(title = "SWIR: New Candlestick Geom!",subtitle = "Visually shows open, high, low, and close information, along with direction",x = "", y = "Closing Price") +coord_x_date(xlim = c(start, end), ylim = c(10, 100))
TXN %>% ggplot(aes(x = date, y = close)) +geom_candlestick(aes(open = open, close = close, high = high, low = low)) +labs(title = "TXN: New Candlestick Geom!", subtitle = "Visually shows open, high, low, and close information, along with direction",x = "", y = "Closing Price") +coord_x_date(xlim = c(start, end), ylim = c(10, 100))
QCOM %>% ggplot(aes(x = date, y = close)) + geom_candlestick(aes(open = open, close = close, high = high, low = low)) +labs(title = "QCOM: New Candlestick Geom!",subtitle = "Visually shows open, high, low, and close information, along with direction",x = "", y = "Closing Price") + coord_x_date(xlim = c(start, end),ylim = c(10, 100))
CSCO %>% ggplot(aes(x = date, y = close)) +geom_candlestick(aes(open = open, close = close, high = high, low = low)) +labs(title = "CSCO: New Candlestick Geom!",subtitle = "Visually shows open, high, low, and close information, along with direction",x = "", y = "Closing Price") +coord_x_date(xlim = c(start, end),ylim = c(10, 100))
```
Column {.tabset-fade .tabset data-width=300}
-----------------------------------------------------------------------
### SWIR BOLLINGER BANDS
```{r, echo=FALSE}
SWIR %>% 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 = "SWIR: New Candlestick Geom + BBands!",subtitle = "Quickly visualize volatility", x = "", y = "Closing Price") +coord_x_date(xlim = c(start, end), ylim = c(10, 100))
```
### TXN BOLLINGER BANDS
```{r, echo=FALSE}
TXN %>% 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 = "TXN: New Candlestick Geom + BBands!",subtitle = "Quickly visualize volatility",x = "", y = "Closing Price") + coord_x_date(xlim = c(start, end),ylim = c(10, 100))
```
### QCOM BOLLINGER BANDS
```{r, echo=FALSE}
QCOM %>% 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 = "QCOM: New Candlestick Geom + BBands!",subtitle = "Quickly visualize volatility",x = "", y = "Closing Price") + coord_x_date(xlim = c(start, end),ylim = c(10, 100))
```
### CSCO BOLLINGER BANDS
```{r, echo=FALSE}
CSCO %>% 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 = "CSCO: New Candlestick Geom + BBands!",subtitle = "Quickly visualize volatility",x = "", y = "Closing Price") + coord_x_date(xlim = c(start, end),ylim = c(10, 100))
```
```{r, include=FALSE}
## THE MOVING AVERAGE GEOM
SWIR %>% ggplot(aes(x = date, y = close)) +geom_candlestick(aes(open = open, close = close, high = high, low = low)) +geom_ma(ma_fun = SMA, n = 15, size = 1) + geom_ma(ma_fun = SMA, n = 50, color = "red", linetype = 4, size = 1) +labs(title = "SWIR: New Candlestick Geom + Moving Averages!",subtitle = "Adding MA's is fast and easy enabling rapid prototyping",x = "", y = "Closing Price") +coord_x_date(xlim = c(start, end),ylim = c(10, 100))
TXN %>% ggplot(aes(x = date, y = close)) +geom_candlestick(aes(open = open, close = close, high = high, low = low)) +geom_ma(ma_fun = SMA, n = 15, size = 1) + geom_ma(ma_fun = SMA, n = 50, color = "red", linetype = 4, size = 1) + labs(title = "TXN: New Candlestick Geom + Moving Averages!",subtitle = "Adding MA's is fast and easy enabling rapid prototyping",x = "", y = "Closing Price") +coord_x_date(xlim = c(start, end),ylim = c(10, 100))
QCOM %>% ggplot(aes(x = date, y = close)) +geom_candlestick(aes(open = open, close = close, high = high, low = low)) + geom_ma(ma_fun = SMA, n = 15, size = 1) + geom_ma(ma_fun = SMA, n = 50, color = "red", linetype = 4, size = 1) +labs(title = "QCOM: New Candlestick Geom + Moving Averages!",subtitle = "Adding MA's is fast and easy enabling rapid prototyping",x = "", y = "Closing Price") +coord_x_date(xlim = c(start, end),ylim = c(10, 100))
CSCO %>% ggplot(aes(x = date, y = close)) + geom_candlestick(aes(open = open, close = close, high = high, low = low)) + geom_ma(ma_fun = SMA, n = 15, size = 1) + geom_ma(ma_fun = SMA, n = 50, color = "red", linetype = 4, size = 1) +labs(title = "CSCO: New Candlestick Geom + Moving Averages!",subtitle = "Adding MA's is fast and easy enabling rapid prototyping",x = "", y = "Closing Price") +coord_x_date(xlim = c(start, end),ylim = c(10, 100))
```
Column {.tabset-fade .tabset data-width=300}
-----------------------------------------------------------------------
### FANG CANDLESTICK CHART
```{r, echo=FALSE}
FANG %>% filter(date >= start - days(2 * 15)) %>% ggplot(aes(x = date, y = close, group = symbol)) +geom_candlestick(aes(open = open, high = high, low = low, close = close)) +labs(title = "FANG Candlestick Chart",subtitle = "Experimenting with Mulitple Stocks", y = "Closing Price", x = "") + coord_x_date(xlim = c(start, end)) + facet_wrap(~ symbol, ncol = 2, scale = "free_y") + theme_tq()
```
### FANG BAR CHART
```{r,echo=FALSE}
FANG %>% filter(date >= start - days(2 * 50)) %>% ggplot(aes(x = date, y = close, volume = volume, group = symbol)) + geom_candlestick(aes(open = open, high = high, low = low, close = close)) + geom_ma(ma_fun = VWMA, n = 15, wilder = TRUE, linetype = 5) + geom_ma(ma_fun = VWMA, n = 50, wilder = TRUE, color = "red") +labs(title = "FANG Bar Chart",subtitle = "50 and 200-Day EMA, Experimenting with Multiple Stocks", y = "Closing Price", x = "") + coord_x_date(xlim = c(start, end)) +facet_wrap(~ symbol, ncol = 2, scales = "free_y") + theme_tq()
```
### MULTIPLE STOCKS AT ONCE!
```{r,echo=FALSE}
## Multiple Stocks at once
n <- 20
FANG %>% filter(date >= start - days(2 * n)) %>% ggplot(aes(x = date, y = close, group = symbol)) + geom_candlestick(aes(open = open, close = close, high = high, low = low)) + geom_bbands(aes(high = high, low = low, close = close),ma_fun = SMA, n = n, sd = 2, size = 0.5) + labs(title = "Multiple Stocks at Once!",subtitle = "Quickly visualize the volatility of four stocks at once",x = "", y = "Closing Price") +coord_x_date(xlim = c(start, end)) +facet_wrap(~ symbol, scales = "free_y") +theme_tq()
```
### DARK THEME
```{r, echo=FALSE}
### DARK THEME
n_mavg <- 50 # Number of periods (days) for moving average
FANG %>% filter(date >= start - days(2 * n_mavg)) %>% ggplot(aes(x = date, y = close, color = symbol)) +geom_line(size = 1) + geom_ma(n = 15, color = "darkblue", size = 1) + geom_ma(n = n_mavg, color = "red", size = 1) +labs(title = "Dark Theme", x = "", y = "Closing Price") + coord_x_date(xlim = c(start, end)) + facet_wrap(~ symbol, scales = "free_y") + theme_tq_dark() +scale_color_tq(theme = "dark") +scale_y_continuous(labels = scales::dollar)
```
```{r, include=FALSE}
## GETTING KEY STATS
SWIR_key_stats <- tq_get("SWIR", get = "key.stats")
colnames(SWIR_key_stats)[1:10]
TXN_key_stats <- tq_get("TXN", get = "key.stats")
colnames(TXN_key_stats)[1:10]
QCOM_key_stats <- tq_get("QCOM", get = "key.stats")
colnames(QCOM_key_stats)[1:10]
CSCO_key_stats <- tq_get("CSCO", get = "key.stats")
colnames(CSCO_key_stats)[1:10]
```
Column {.tabset-fade .tabset data-width=300}
-------------------------------------
### COMPARING KEY STATS
```{r, echo=FALSE}
c("SWIR", "TXN", "QCOM", "CSCO") %>% tq_get(get = "key.stats") %>% select(symbol, Bid, Bid.Size, Ask, Ask.Size, Open, Change)
```
### CSCO - COMPARING HISTORIC DATA TO CURRENT DATA
```{r,echo=FALSE}
collect_real_time_data <- function(x, interval_sec, n) {data <- tibble()
while (n > 0) {
data <- bind_rows(data, tq_get(x, get = "key.stats"))
Sys.sleep(interval_sec)
n <- n - 1}
return(data)}
collect_real_time_data("CSCO", interval_sec = 3, n = 5) %>%select(Ask, Ask.Size, Bid, Bid.Size, Open, Change)
CSCO_key_stats$PE.Ratio
```
```{r, include=FALSE}
### GROUPED BY SECTION TYPE
CSCO_key_ratios <- tq_get("CSCO", get = "key.ratios")
CSCO_key_ratios
```
### CSCO VALUATION RATIOS
```{r, echo=FALSE}
CSCO_historical_pe_ratios <- CSCO_key_ratios %>%
filter(section == "Valuation Ratios") %>%
unnest() %>%
filter(category == "Price to Earnings") %>%
select(category, date, value)
CSCO_historical_pe_ratios
```
```{r, include=FALSE}
### SCALLING ANALYSIS
c("CSCO", "TXN", "QCOM") %>% tq_get(get = "stock.prices")
```