Conduct the trend analysis for the Tesla stock.

For Q1 - Q3

library(tidyquant)

# Import data
from = today() - years(4)
TSLA <- tq_get("TSLA", get = "stock.prices", from = from)
TSLA
## # A tibble: 1,006 x 7
##    date        open  high   low close  volume adjusted
##    <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>
##  1 2015-02-19  205   212.  204.  212. 5154100     212.
##  2 2015-02-20  211.  218.  210.  217. 5982100     217.
##  3 2015-02-23  216.  218.  206.  207. 8499800     207.
##  4 2015-02-24  207.  207.  202.  204. 6584900     204.
##  5 2015-02-25  205.  207.  203.  204. 3909500     204.
##  6 2015-02-26  204   211.  202.  207. 6472900     207.
##  7 2015-02-27  207.  209.  203.  203. 3882100     203.
##  8 2015-03-02  203.  203.  196.  197. 7922100     197.
##  9 2015-03-03  197.  200.  195.  200. 4432300     200.
## 10 2015-03-04  199.  203.  197.  202. 4212000     202.
## # ... with 996 more rows
# Add the 100-day and 200-day simple moving average by passing close prices 
TSLA <-
  TSLA %>%
    tq_mutate(select = close, mutate_fun = SMA, n = 100) %>%
    rename(SMA.100 = SMA) %>%
    tq_mutate(select = close, mutate_fun = SMA, n = 200) %>%
    rename(SMA.200 = SMA)

TSLA
## # A tibble: 1,006 x 9
##    date        open  high   low close  volume adjusted SMA.100 SMA.200
##    <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>   <dbl>   <dbl>
##  1 2015-02-19  205   212.  204.  212. 5154100     212.      NA      NA
##  2 2015-02-20  211.  218.  210.  217. 5982100     217.      NA      NA
##  3 2015-02-23  216.  218.  206.  207. 8499800     207.      NA      NA
##  4 2015-02-24  207.  207.  202.  204. 6584900     204.      NA      NA
##  5 2015-02-25  205.  207.  203.  204. 3909500     204.      NA      NA
##  6 2015-02-26  204   211.  202.  207. 6472900     207.      NA      NA
##  7 2015-02-27  207.  209.  203.  203. 3882100     203.      NA      NA
##  8 2015-03-02  203.  203.  196.  197. 7922100     197.      NA      NA
##  9 2015-03-03  197.  200.  195.  200. 4432300     200.      NA      NA
## 10 2015-03-04  199.  203.  197.  202. 4212000     202.      NA      NA
## # ... with 996 more rows
# Transform to long form to wide form for graphing
TSLA <-
  TSLA %>%
  select(date, close, SMA.100, SMA.200) %>%
  gather(key = type, value = price, close:SMA.200)

TSLA
## # A tibble: 3,018 x 3
##    date       type  price
##    <date>     <chr> <dbl>
##  1 2015-02-19 close  212.
##  2 2015-02-20 close  217.
##  3 2015-02-23 close  207.
##  4 2015-02-24 close  204.
##  5 2015-02-25 close  204.
##  6 2015-02-26 close  207.
##  7 2015-02-27 close  203.
##  8 2015-03-02 close  197.
##  9 2015-03-03 close  200.
## 10 2015-03-04 close  202.
## # ... with 3,008 more rows
# Visualize
TSLA %>%
  ggplot(aes(x = date, y = price, col = type)) +
  geom_line() + 
  theme(legend.position="bottom") +
  labs(title = "Simple Moving Averages are a Breeze with tidyquant",
       x = NULL,
       y = "Stock Prices")

Q1 Import NASDAQ for the last five years, and save the data under Stock, instead of TSLA.

Hint: Insert the code below (only importing code).

# Import data
from = today() - years(5)
Stock <- tq_get("^IXIC", get = "stock.prices", from = from) 
Stock
## # A tibble: 1,258 x 7
##    date        open  high   low close     volume adjusted
##    <date>     <dbl> <dbl> <dbl> <dbl>      <dbl>    <dbl>
##  1 2014-02-19 4261. 4274. 4232. 4238. 1956720000    4238.
##  2 2014-02-20 4241. 4272. 4227. 4268. 1992780000    4268.
##  3 2014-02-21 4282. 4285. 4262. 4263. 2138250000    4263.
##  4 2014-02-24 4273. 4311. 4272. 4293. 2161300000    4293.
##  5 2014-02-25 4298. 4308. 4276. 4288. 2137150000    4288.
##  6 2014-02-26 4300. 4317. 4279. 4292. 2108270000    4292.
##  7 2014-02-27 4291. 4322. 4285. 4319. 2049160000    4319.
##  8 2014-02-28 4324. 4343. 4276. 4308. 2617730000    4308.
##  9 2014-03-03 4261. 4284. 4240. 4277. 2077500000    4277.
## 10 2014-03-04 4328. 4357. 4328. 4352. 2477850000    4352.
## # ... with 1,248 more rows

Q2 Add 50-day and 100-day simple moving average

Hint: Insert the code below (only moving average code).

Stock <-
  Stock %>%
    tq_mutate(select = close, mutate_fun = SMA, n = 50) %>%
    rename(SMA.50 = SMA) %>%
    tq_mutate(select = close, mutate_fun = SMA, n = 100) %>%
    rename(SMA.100 = SMA)

Stock %>% View()

Q3 Insert below the code to transform data to long form from wide form for graphing

Stock <-
  Stock %>%
  select(date, close, SMA.50, SMA.100) %>%
  gather(key = type, value = price, close:SMA.100)

Stock
## # A tibble: 3,774 x 3
##    date       type  price
##    <date>     <chr> <dbl>
##  1 2014-02-19 close 4238.
##  2 2014-02-20 close 4268.
##  3 2014-02-21 close 4263.
##  4 2014-02-24 close 4293.
##  5 2014-02-25 close 4288.
##  6 2014-02-26 close 4292.
##  7 2014-02-27 close 4319.
##  8 2014-02-28 close 4308.
##  9 2014-03-03 close 4277.
## 10 2014-03-04 close 4352.
## # ... with 3,764 more rows

Q4 Insert below the code to visualize

Stock %>%
  ggplot(aes(x = date, y = price, col = type)) +
  geom_line() + 
  theme(legend.position="bottom") +
  labs(title = "Simple Moving Averages are a Breeze with tidyquant",
       x = NULL,
       y = "Stock Prices")

Q5 Does the chart identify a bullish or bearish crossover?

-Bullish Crossover

Q6 What day is the crossover?

Q7 Did the analysis correctly identify a buying (or selling) opportunity? Elaborate.

Compare with the crossover with the actual price. -Buying Opportunity

Q8 Repeat Q2- Q7 for 100-day and 200-day simple moving average.

Hint: Insert the code below.