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).

library(tidyquant)

# 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-26 4300. 4317. 4279. 4292. 2108270000    4292.
##  2 2014-02-27 4291. 4322. 4285. 4319. 2049160000    4319.
##  3 2014-02-28 4324. 4343. 4276. 4308. 2617730000    4308.
##  4 2014-03-03 4261. 4284. 4240. 4277. 2077500000    4277.
##  5 2014-03-04 4328. 4357. 4328. 4352. 2477850000    4352.
##  6 2014-03-05 4353. 4362. 4344. 4358. 2215980000    4358.
##  7 2014-03-06 4369. 4372. 4341  4352. 2136260000    4352.
##  8 2014-03-07 4371. 4371. 4319. 4336. 2175560000    4336.
##  9 2014-03-10 4333. 4340. 4308. 4334. 2111610000    4334.
## 10 2014-03-11 4343. 4354. 4295. 4307. 2477780000    4307.
## # ... with 1,248 more rows

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

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

library(tidyquant)

# Import data
from = today() - years(5)
stock <- tq_get("^IXIC", get = "stock.prices", from = from)

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
## # A tibble: 1,258 x 9
##    date        open  high   low close     volume adjusted SMA.50 SMA.100
##    <date>     <dbl> <dbl> <dbl> <dbl>      <dbl>    <dbl>  <dbl>   <dbl>
##  1 2014-02-26 4300. 4317. 4279. 4292. 2108270000    4292.     NA      NA
##  2 2014-02-27 4291. 4322. 4285. 4319. 2049160000    4319.     NA      NA
##  3 2014-02-28 4324. 4343. 4276. 4308. 2617730000    4308.     NA      NA
##  4 2014-03-03 4261. 4284. 4240. 4277. 2077500000    4277.     NA      NA
##  5 2014-03-04 4328. 4357. 4328. 4352. 2477850000    4352.     NA      NA
##  6 2014-03-05 4353. 4362. 4344. 4358. 2215980000    4358.     NA      NA
##  7 2014-03-06 4369. 4372. 4341  4352. 2136260000    4352.     NA      NA
##  8 2014-03-07 4371. 4371. 4319. 4336. 2175560000    4336.     NA      NA
##  9 2014-03-10 4333. 4340. 4308. 4334. 2111610000    4334.     NA      NA
## 10 2014-03-11 4343. 4354. 4295. 4307. 2477780000    4307.     NA      NA
## # ... with 1,248 more rows

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

# Transform to long form to 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-26 close 4292.
##  2 2014-02-27 close 4319.
##  3 2014-02-28 close 4308.
##  4 2014-03-03 close 4277.
##  5 2014-03-04 close 4352.
##  6 2014-03-05 close 4358.
##  7 2014-03-06 close 4352.
##  8 2014-03-07 close 4336.
##  9 2014-03-10 close 4334.
## 10 2014-03-11 close 4307.
## # ... with 3,764 more rows

Q4 Insert below the code to visualize

# 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?

The chart shows a bearish crossover around the end of 2015, and around the first quarter mark of 2016 with the last one being mid 2018. It also shows bullish crossovers at end of 2015, beginning of 2016, mid 2018 and end of 3rd quarter in 2018.

Q6 What day is the crossover?

view(stock) Using this function we see a bullish crossover at

769 2015-08-27 SMA.50 - 5033.371

770 2015-08-27 SMA.100 - 5031.499

771 2015-08-28 SMA.50 - 5027.278

772 2015-08-28 SMA.100 - 5030.274

We then see a bearish crossover at

903 2015-12-02 SMA.50 - 4942.473

904 2015-12-02 SMA.100 - 4945.124

905 2015-12-03 SMA.50 - 4948.169

906 2015-12-03 SMA.100 - 4944.450

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

Compare with the crossover with the actual price.

When we see bullish crossovers we also see a price increase in the actual stock price meaning you should buy, where as when you a bearish crossover you should consider selling as the stock seems to be going down in price.

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

Hint: Insert the code below.

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

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

library(tidyquant)

# Import data
from = today() - years(5)
stock <- tq_get("^IXIC", get = "stock.prices", from = from)

stock <-
  stock %>%
    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)

stock
## # A tibble: 1,258 x 9
##    date        open  high   low close     volume adjusted SMA.100 SMA.200
##    <date>     <dbl> <dbl> <dbl> <dbl>      <dbl>    <dbl>   <dbl>   <dbl>
##  1 2014-02-26 4300. 4317. 4279. 4292. 2108270000    4292.      NA      NA
##  2 2014-02-27 4291. 4322. 4285. 4319. 2049160000    4319.      NA      NA
##  3 2014-02-28 4324. 4343. 4276. 4308. 2617730000    4308.      NA      NA
##  4 2014-03-03 4261. 4284. 4240. 4277. 2077500000    4277.      NA      NA
##  5 2014-03-04 4328. 4357. 4328. 4352. 2477850000    4352.      NA      NA
##  6 2014-03-05 4353. 4362. 4344. 4358. 2215980000    4358.      NA      NA
##  7 2014-03-06 4369. 4372. 4341  4352. 2136260000    4352.      NA      NA
##  8 2014-03-07 4371. 4371. 4319. 4336. 2175560000    4336.      NA      NA
##  9 2014-03-10 4333. 4340. 4308. 4334. 2111610000    4334.      NA      NA
## 10 2014-03-11 4343. 4354. 4295. 4307. 2477780000    4307.      NA      NA
## # ... with 1,248 more rows

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

# Transform to long form to wide form for graphing
stock <-
  stock %>%
  select(date, close, SMA.100, SMA.200) %>%
  gather(key = type, value = price, close:SMA.200)

stock
## # A tibble: 3,774 x 3
##    date       type  price
##    <date>     <chr> <dbl>
##  1 2014-02-26 close 4292.
##  2 2014-02-27 close 4319.
##  3 2014-02-28 close 4308.
##  4 2014-03-03 close 4277.
##  5 2014-03-04 close 4352.
##  6 2014-03-05 close 4358.
##  7 2014-03-06 close 4352.
##  8 2014-03-07 close 4336.
##  9 2014-03-10 close 4334.
## 10 2014-03-11 close 4307.
## # ... with 3,764 more rows

Q4 Insert below the code to visualize

# 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?

The chart shows a bullish crossover around the beginning of 2016 and closer to today and a bearish crossover around mid 2016.

Q6 What day is the crossover?

view(stock) Using this function we see a bullish crossover at

857 2015-10-29 SMA.100 - 4936.272

858 2015-10-29 SMA.200 - 4933.277

859 2015-10-30 SMA.100 - 4936.043

860 2015-10-30 SMA.200 - 4935.691

and also at

2455 2019-01-03 SMA.100 - 7460.404

2456 2019-01-03 SMA.200 - 7458.355

2457 2019-01-04 SMA.100 - 7449.401

2458 2019-01-04 SMA.200 - 7455.228

We then see a bearish crossover at

1215 2016-07-18 SMA.100 - 4829.696

1216 2016-07-18 SMA.200 - 4829.937

1217 2016-07-19 SMA.100 - 4834.237

1218 2016-07-19 SMA.200 - 4831.984

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

Compare with the crossover with the actual price. When we see bullish crossovers we also see a price increase in the actual stock price meaning you should buy, where as when you a bearish crossover you should consider selling as the stock seems to be going down in price.