Column

SWIR GEOM CANDLESTICK

TXN GEOM CANDLESTICK

QCOM GEOM CANDLESTICK

CSCO GEOM CANDLESTICK

Column

FANG Candlestick Chart

FANG Bar Chart

Multiple Stocks at once

Row ————————————-

Dark Theme

Column

COMPARING KEY STATS

# A tibble: 4 x 7
  symbol   Bid Bid.Size   Ask Ask.Size  Open Change
   <chr> <dbl>    <dbl> <dbl>    <dbl> <dbl>  <dbl>
1   SWIR 21.40      100 22.50      300 21.80  -0.30
2    TXN 81.85      300 82.28      100 82.32  -0.69
3   QCOM 50.05      100 50.09     1700 51.86  -2.02
4   CSCO 31.67     1000 31.80     1500 32.15  -0.68

CSCO - COMPARING HISTORIC DATA TO CURRENT DATA

# A tibble: 5 x 6
    Ask Ask.Size   Bid Bid.Size  Open Change
  <dbl>    <dbl> <dbl>    <dbl> <dbl>  <dbl>
1  31.8     1500 31.67     1000 32.15  -0.68
2  31.8     1500 31.67     1000 32.15  -0.68
3  31.8     1500 31.67     1000 32.15  -0.68
4  31.8     1500 31.67     1000 32.15  -0.68
5  31.8     1500 31.67     1000 32.15  -0.68
[1] 16.64

CSCO - VALUATION RATIOS

# A tibble: 10 x 3
            category       date    value
               <chr>     <date>    <dbl>
 1 Price to Earnings 2007-12-31 19.28093
 2 Price to Earnings 2008-12-31 10.36912
 3 Price to Earnings 2009-12-31 19.00030
 4 Price to Earnings 2010-12-31 12.67564
 5 Price to Earnings 2011-12-30 13.02332
 6 Price to Earnings 2012-12-31 11.41318
 7 Price to Earnings 2013-12-31 10.67210
 8 Price to Earnings 2014-12-31 17.04923
 9 Price to Earnings 2015-12-31 14.60613
10 Price to Earnings 2016-12-30 13.94740
---
title: "Dashboard"
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)
```

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

### SWIR GEOM CANDLESTICK


```{r, echo=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 GEOM CANDLESTICK

```{r, echo=FALSE}
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 GEOM CANDLESTICK

```{r, echo=FALSE}
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 GEOM CANDLESTICK
```{r, echo=FALSE}
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}
-------------------------------------
###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()
```





```{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))
```

### 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()
```



```{r, include=FALSE}
## BOLLINGER BANDS
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 %>% 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 %>% 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 %>% 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))
```

### Multiple Stocks at once
```{r, echo=FALSE}
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()
```
Row
-------------------------------------

### Dark Theme
```{r, echo=FALSE}

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")
```