# Loads tidyquant, tidyverse, lubridate, xts, quantmod, TTR 
library(tidyquant)
CVS <- tq_get("CVS", get = "stock.prices", from = "2011-01-01", to = today())
CVS
## # A tibble: 1,997 x 7
##    date        open  high   low close   volume adjusted
##    <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 2011-01-03  34.9  35.2  34.8  35.0  8164600     30.2
##  2 2011-01-04  35    35.1  34.4  35.0  9891900     30.2
##  3 2011-01-05  34.9  35.4  34.8  35.4 10946500     30.5
##  4 2011-01-06  35.3  35.3  34.8  35.0  8680000     30.2
##  5 2011-01-07  35    35.3  34.8  35.0  8220600     30.2
##  6 2011-01-10  35.0  35.3  35.0  35.2  6579400     30.3
##  7 2011-01-11  35.2  35.7  35.1  35.3  8869300     30.5
##  8 2011-01-12  35.4  36.0  34.7  34.8 13151500     30.0
##  9 2011-01-13  34.8  34.9  34.5  34.9  8165500     30.1
## 10 2011-01-14  34.9  35.2  34.6  35.1  7016700     30.2
## # ... with 1,987 more rows
CVS_MA <-
  CVS %>%
    tq_mutate(select = close, mutate_fun = SMA, n = 50) %>%
    rename(SMA.50 = SMA) %>%
    tq_mutate(select = close, mutate_fun = SMA, n = 200) %>%
    rename(SMA.200 = SMA)

CVS_MA
## # A tibble: 1,997 x 9
##    date        open  high   low close   volume adjusted SMA.50 SMA.200
##    <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>  <dbl>   <dbl>
##  1 2011-01-03  34.9  35.2  34.8  35.0  8164600     30.2     NA      NA
##  2 2011-01-04  35    35.1  34.4  35.0  9891900     30.2     NA      NA
##  3 2011-01-05  34.9  35.4  34.8  35.4 10946500     30.5     NA      NA
##  4 2011-01-06  35.3  35.3  34.8  35.0  8680000     30.2     NA      NA
##  5 2011-01-07  35    35.3  34.8  35.0  8220600     30.2     NA      NA
##  6 2011-01-10  35.0  35.3  35.0  35.2  6579400     30.3     NA      NA
##  7 2011-01-11  35.2  35.7  35.1  35.3  8869300     30.5     NA      NA
##  8 2011-01-12  35.4  36.0  34.7  34.8 13151500     30.0     NA      NA
##  9 2011-01-13  34.8  34.9  34.5  34.9  8165500     30.1     NA      NA
## 10 2011-01-14  34.9  35.2  34.6  35.1  7016700     30.2     NA      NA
## # ... with 1,987 more rows
CVS_MA %>%
  select(date, close, SMA.50, SMA.200) %>%
  gather(key = type, value = price, close:SMA.200)
## # A tibble: 5,991 x 3
##    date       type  price
##    <date>     <chr> <dbl>
##  1 2011-01-03 close  35.0
##  2 2011-01-04 close  35.0
##  3 2011-01-05 close  35.4
##  4 2011-01-06 close  35.0
##  5 2011-01-07 close  35.0
##  6 2011-01-10 close  35.2
##  7 2011-01-11 close  35.3
##  8 2011-01-12 close  34.8
##  9 2011-01-13 close  34.9
## 10 2011-01-14 close  35.1
## # ... with 5,981 more rows
# Transform to long form to wide form for graphing
CVS_MA %>%
  select(date, close, SMA.50, SMA.200) %>%
  gather(key = type, value = price, close:SMA.200)
## # A tibble: 5,991 x 3
##    date       type  price
##    <date>     <chr> <dbl>
##  1 2011-01-03 close  35.0
##  2 2011-01-04 close  35.0
##  3 2011-01-05 close  35.4
##  4 2011-01-06 close  35.0
##  5 2011-01-07 close  35.0
##  6 2011-01-10 close  35.2
##  7 2011-01-11 close  35.3
##  8 2011-01-12 close  34.8
##  9 2011-01-13 close  34.9
## 10 2011-01-14 close  35.1
## # ... with 5,981 more rows

# Visualize Moving Average Crossover
CVS_MA %>%
  select(date, close, SMA.50, SMA.200) %>%
  gather(key = type, value = price, close:SMA.200) %>%
  filter(date >= "2018-01-01") %>%
  ggplot(aes(x = date, y = price, col = type)) +
  geom_line() + 
  theme(legend.position="bottom") +
  labs(x = NULL,
       y = "Stock Prices")

```