# Loads tidyquant, tidyverse, lubridate, xts, quantmod, TTR 
library(tidyquant)
TSLA <- tq_get("TSLA", get = "stock.prices", from = "2011-01-01", to = today())
TSLA
## # A tibble: 1,995 x 7
##    date        open  high   low close  volume adjusted
##    <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>
##  1 2011-01-03  26.8  27    25.9  26.6 1283000     26.6
##  2 2011-01-04  26.7  27.0  26.0  26.7 1187400     26.7
##  3 2011-01-05  26.5  26.9  26.2  26.8 1446700     26.8
##  4 2011-01-06  26.8  28    26.8  27.9 2061200     27.9
##  5 2011-01-07  28    28.6  27.9  28.2 2247900     28.2
##  6 2011-01-10  28.2  28.7  28.0  28.5 1342700     28.5
##  7 2011-01-11  28.6  28.7  26.9  27.0 1710200     27.0
##  8 2011-01-12  27.0  27.4  26.5  27.0  964400     27.0
##  9 2011-01-13  27.0  27.0  26.2  26.2  723600     26.2
## 10 2011-01-14  26.2  26.6  25.6  25.8 1192000     25.8
## # ... with 1,985 more rows
TSLA_MA <-
  TSLA %>%
    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)

TSLA_MA
## # A tibble: 1,995 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  26.8  27    25.9  26.6 1283000     26.6     NA      NA
##  2 2011-01-04  26.7  27.0  26.0  26.7 1187400     26.7     NA      NA
##  3 2011-01-05  26.5  26.9  26.2  26.8 1446700     26.8     NA      NA
##  4 2011-01-06  26.8  28    26.8  27.9 2061200     27.9     NA      NA
##  5 2011-01-07  28    28.6  27.9  28.2 2247900     28.2     NA      NA
##  6 2011-01-10  28.2  28.7  28.0  28.5 1342700     28.5     NA      NA
##  7 2011-01-11  28.6  28.7  26.9  27.0 1710200     27.0     NA      NA
##  8 2011-01-12  27.0  27.4  26.5  27.0  964400     27.0     NA      NA
##  9 2011-01-13  27.0  27.0  26.2  26.2  723600     26.2     NA      NA
## 10 2011-01-14  26.2  26.6  25.6  25.8 1192000     25.8     NA      NA
## # ... with 1,985 more rows
TSLA_MA %>%
  select(date, close, SMA.50, SMA.200) %>%
  gather(key = type, value = price, close:SMA.200)
## # A tibble: 5,985 x 3
##    date       type  price
##    <date>     <chr> <dbl>
##  1 2011-01-03 close  26.6
##  2 2011-01-04 close  26.7
##  3 2011-01-05 close  26.8
##  4 2011-01-06 close  27.9
##  5 2011-01-07 close  28.2
##  6 2011-01-10 close  28.5
##  7 2011-01-11 close  27.0
##  8 2011-01-12 close  27.0
##  9 2011-01-13 close  26.2
## 10 2011-01-14 close  25.8
## # ... with 5,975 more rows
# Visualize Moving Average Crossover

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

F <- tq_get("F", get = "stock.prices", from = "2011-01-01", to = today())
F
## # A tibble: 1,995 x 7
##    date        open  high   low close    volume adjusted
##    <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
##  1 2011-01-03  17.0  17.3  17    17.2  70794200     13.1
##  2 2011-01-04  17.4  17.7  17.0  17.4 106477200     13.2
##  3 2011-01-05  17.3  18.1  17.2  17.9 112378000     13.6
##  4 2011-01-06  18.0  18.3  18.0  18.2  96284400     13.9
##  5 2011-01-07  18.4  18.4  18.1  18.3  99029500     13.9
##  6 2011-01-10  18.4  18.5  18.2  18.3  65348400     14.0
##  7 2011-01-11  18.5  18.5  18.1  18.3  61312500     13.9
##  8 2011-01-12  18.4  18.8  18.3  18.7  66841400     14.3
##  9 2011-01-13  18.8  19.0  18.5  18.7 100459700     14.2
## 10 2011-01-14  18.6  18.8  18.6  18.6  50279400     14.2
## # ... with 1,985 more rows
F_MA <-
  F %>%
    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)

F_MA
## # A tibble: 1,995 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  17.0  17.3  17    17.2  70794200     13.1     NA      NA
##  2 2011-01-04  17.4  17.7  17.0  17.4 106477200     13.2     NA      NA
##  3 2011-01-05  17.3  18.1  17.2  17.9 112378000     13.6     NA      NA
##  4 2011-01-06  18.0  18.3  18.0  18.2  96284400     13.9     NA      NA
##  5 2011-01-07  18.4  18.4  18.1  18.3  99029500     13.9     NA      NA
##  6 2011-01-10  18.4  18.5  18.2  18.3  65348400     14.0     NA      NA
##  7 2011-01-11  18.5  18.5  18.1  18.3  61312500     13.9     NA      NA
##  8 2011-01-12  18.4  18.8  18.3  18.7  66841400     14.3     NA      NA
##  9 2011-01-13  18.8  19.0  18.5  18.7 100459700     14.2     NA      NA
## 10 2011-01-14  18.6  18.8  18.6  18.6  50279400     14.2     NA      NA
## # ... with 1,985 more rows
F_MA %>%
  select(date, close, SMA.50, SMA.200) %>%
  gather(key = type, value = price, close:SMA.200)
## # A tibble: 5,985 x 3
##    date       type  price
##    <date>     <chr> <dbl>
##  1 2011-01-03 close  17.2
##  2 2011-01-04 close  17.4
##  3 2011-01-05 close  17.9
##  4 2011-01-06 close  18.2
##  5 2011-01-07 close  18.3
##  6 2011-01-10 close  18.3
##  7 2011-01-11 close  18.3
##  8 2011-01-12 close  18.7
##  9 2011-01-13 close  18.7
## 10 2011-01-14 close  18.6
## # ... with 5,975 more rows
F_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")

GM <- tq_get("GM", get = "stock.prices", from = "2011-01-01", to = today())
GM
## # A tibble: 1,995 x 7
##    date        open  high   low close   volume adjusted
##    <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 2011-01-03  37.3  38    37.0  37.1 24874900     30.2
##  2 2011-01-04  37.1  38.0  36.7  37.9 32363400     30.9
##  3 2011-01-05  37.5  38.3  37.5  38.1 22503900     31.0
##  4 2011-01-06  38.2  39.5  38.1  38.9 38556900     31.7
##  5 2011-01-07  38.8  39.3  38.5  39.0 19901100     31.8
##  6 2011-01-10  39.3  39.4  38.4  38.6 18341600     31.4
##  7 2011-01-11  38.7  39.4  38.5  38.8 14856500     31.6
##  8 2011-01-12  39.0  39.4  38.4  38.6 16773900     31.5
##  9 2011-01-13  38.7  38.7  38.1  38.3 11358300     31.2
## 10 2011-01-14  38.2  38.5  38.0  38.2  5894200     31.1
## # ... with 1,985 more rows
GM_MA <-
  GM %>%
    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)

GM_MA
## # A tibble: 1,995 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  37.3  38    37.0  37.1 24874900     30.2     NA      NA
##  2 2011-01-04  37.1  38.0  36.7  37.9 32363400     30.9     NA      NA
##  3 2011-01-05  37.5  38.3  37.5  38.1 22503900     31.0     NA      NA
##  4 2011-01-06  38.2  39.5  38.1  38.9 38556900     31.7     NA      NA
##  5 2011-01-07  38.8  39.3  38.5  39.0 19901100     31.8     NA      NA
##  6 2011-01-10  39.3  39.4  38.4  38.6 18341600     31.4     NA      NA
##  7 2011-01-11  38.7  39.4  38.5  38.8 14856500     31.6     NA      NA
##  8 2011-01-12  39.0  39.4  38.4  38.6 16773900     31.5     NA      NA
##  9 2011-01-13  38.7  38.7  38.1  38.3 11358300     31.2     NA      NA
## 10 2011-01-14  38.2  38.5  38.0  38.2  5894200     31.1     NA      NA
## # ... with 1,985 more rows
GM_MA %>%
  select(date, close, SMA.50, SMA.200) %>%
  gather(key = type, value = price, close:SMA.200)
## # A tibble: 5,985 x 3
##    date       type  price
##    <date>     <chr> <dbl>
##  1 2011-01-03 close  37.1
##  2 2011-01-04 close  37.9
##  3 2011-01-05 close  38.1
##  4 2011-01-06 close  38.9
##  5 2011-01-07 close  39.0
##  6 2011-01-10 close  38.6
##  7 2011-01-11 close  38.8
##  8 2011-01-12 close  38.6
##  9 2011-01-13 close  38.3
## 10 2011-01-14 close  38.2
## # ... with 5,975 more rows
GM_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")