Q1 Import Tesla for the last ten months.

Hint: Revise the given code below.

library(tidyquant)
library(tidyverse)

# Import data
from = today() - months(10)
stock <- tq_get("TSLA", get = "stock.prices", from = from)
stock
## # A tibble: 209 x 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 TSLA   2019-06-17  215.  227   214.  225. 12316800     225.
##  2 TSLA   2019-06-18  229.  235.  223.  225. 12715800     225.
##  3 TSLA   2019-06-19  225.  228.  221.  226.  6575100     226.
##  4 TSLA   2019-06-20  223   227.  216.  220. 11863500     220.
##  5 TSLA   2019-06-21  216.  222.  216.  222.  8202100     222.
##  6 TSLA   2019-06-24  223.  226.  221.  224.  5750800     224.
##  7 TSLA   2019-06-25  224.  225.  219.  220.  6182100     220.
##  8 TSLA   2019-06-26  220.  227.  218.  219.  8507200     219.
##  9 TSLA   2019-06-27  219.  223.  217.  223.  6339700     223.
## 10 TSLA   2019-06-28  221.  225.  221.  223.  6851400     223.
## # … with 199 more rows

Q2 Calculate 15-day and 50-day simple moving averages.

Hint: Rename 15-day moving average to SMA.short and 50-day moving average to SMA.long.

stock <-
  stock %>%
  # Calculate 50-day simple moving average
  tq_mutate(select = close, mutate_fun = SMA, n = 15) %>%
  # Rename 50-day SMA to SMA.short
  rename(SMA.short = SMA) %>%
  tq_mutate(select = close, mutate_fun = SMA, n = 50) %>%
  rename(SMA.long = SMA)

stock
## # A tibble: 209 x 10
##    symbol date        open  high   low close  volume adjusted SMA.short SMA.long
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>     <dbl>    <dbl>
##  1 TSLA   2019-06-17  215.  227   214.  225.  1.23e7     225.        NA       NA
##  2 TSLA   2019-06-18  229.  235.  223.  225.  1.27e7     225.        NA       NA
##  3 TSLA   2019-06-19  225.  228.  221.  226.  6.58e6     226.        NA       NA
##  4 TSLA   2019-06-20  223   227.  216.  220.  1.19e7     220.        NA       NA
##  5 TSLA   2019-06-21  216.  222.  216.  222.  8.20e6     222.        NA       NA
##  6 TSLA   2019-06-24  223.  226.  221.  224.  5.75e6     224.        NA       NA
##  7 TSLA   2019-06-25  224.  225.  219.  220.  6.18e6     220.        NA       NA
##  8 TSLA   2019-06-26  220.  227.  218.  219.  8.51e6     219.        NA       NA
##  9 TSLA   2019-06-27  219.  223.  217.  223.  6.34e6     223.        NA       NA
## 10 TSLA   2019-06-28  221.  225.  221.  223.  6.85e6     223.        NA       NA
## # … with 199 more rows

Q3 Transform data to long form from wide form for graphing.

Hint: Select date, close, SMA.short, and SMA.long. Then, tranform the data to long from so that you could have all three variables (close, SMA.short, and SMA.long) in one graph.


stock_long <-
  stock %>%
  select(date, close, SMA.short, SMA.long) %>%
  
  gather(key = "type", value = "price", close:SMA.long)

stock_long
## # A tibble: 627 x 3
##    date       type  price
##    <date>     <chr> <dbl>
##  1 2019-06-17 close  225.
##  2 2019-06-18 close  225.
##  3 2019-06-19 close  226.
##  4 2019-06-20 close  220.
##  5 2019-06-21 close  222.
##  6 2019-06-24 close  224.
##  7 2019-06-25 close  220.
##  8 2019-06-26 close  219.
##  9 2019-06-27 close  223.
## 10 2019-06-28 close  223.
## # … with 617 more rows

Q4 Visualize data.

Hint: Map date to the x-axis and stock prices (close, SMA.short, and SMA.long) to the y-axis.

stock_long %>%
  ggplot(aes(x = date, y = price, col = type)) +
  geom_line() + 
  theme(legend.position="bottom") +
  labs(title = "Tesla Moving Averages",
       x = NULL,
       y = "Stock Prices")

Q5 If you had invested $1 million on the day of the first bullish crossover and sold your shares on the following bearish crosover, how much would you have won or lost?

Hint: Elaborate your calculation.

The first Bullish crossover was on 9/19/2019 with a closing price of $246.60. This means that you could purchase 4055 stocks with 1 million dollars. The following bearish crossover was on 3/17/2020 with a closing price of $430.20. In conclusion if you sold 4055 stocks at $430.20/stock the total would be $1,744,461; Profit of $744,461.

Q6 The bearish crossover missed the actual high. How long (in days) was the time lag?

Hint: Elaborate your calculation. One word/number anwer is not enough.

The actual high of Tesla’s stock price was on 2/19/2020 at $917.42. This means the time lag from 2/19/2020 to 3/17/2020 was 19 market days.

Q7 What would you change in the moving average model to reduce the time lag?

Hint: Elaborate your answer. One word answer is not enough.

If we changed the short term moving averages and the long term moving averages we can reduce the time lag.

Q7.a Create another chart below with the change, and count the reduced time lag (in days).

Bstock <-
  stock %>%
  tq_mutate(select = close, mutate_fun = SMA, n = 5) %>%
  rename(NEWSMA.short = SMA) %>%
  tq_mutate(select = close, mutate_fun = SMA, n = 10) %>%
  rename(NEWSMA.long = SMA)

Bstock
## # A tibble: 209 x 12
##    symbol date        open  high   low close volume adjusted SMA.short SMA.long
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>  <dbl>    <dbl>     <dbl>    <dbl>
##  1 TSLA   2019-06-17  215.  227   214.  225. 1.23e7     225.        NA       NA
##  2 TSLA   2019-06-18  229.  235.  223.  225. 1.27e7     225.        NA       NA
##  3 TSLA   2019-06-19  225.  228.  221.  226. 6.58e6     226.        NA       NA
##  4 TSLA   2019-06-20  223   227.  216.  220. 1.19e7     220.        NA       NA
##  5 TSLA   2019-06-21  216.  222.  216.  222. 8.20e6     222.        NA       NA
##  6 TSLA   2019-06-24  223.  226.  221.  224. 5.75e6     224.        NA       NA
##  7 TSLA   2019-06-25  224.  225.  219.  220. 6.18e6     220.        NA       NA
##  8 TSLA   2019-06-26  220.  227.  218.  219. 8.51e6     219.        NA       NA
##  9 TSLA   2019-06-27  219.  223.  217.  223. 6.34e6     223.        NA       NA
## 10 TSLA   2019-06-28  221.  225.  221.  223. 6.85e6     223.        NA       NA
## # … with 199 more rows, and 2 more variables: NEWSMA.short <dbl>,
## #   NEWSMA.long <dbl>
Bstock_long <-
  Bstock %>%
  select(date, close, NEWSMA.short, NEWSMA.long) %>%
  gather(key = "type", value = "price", close:NEWSMA.long)

Bstock_long
## # A tibble: 627 x 3
##    date       type  price
##    <date>     <chr> <dbl>
##  1 2019-06-17 close  225.
##  2 2019-06-18 close  225.
##  3 2019-06-19 close  226.
##  4 2019-06-20 close  220.
##  5 2019-06-21 close  222.
##  6 2019-06-24 close  224.
##  7 2019-06-25 close  220.
##  8 2019-06-26 close  219.
##  9 2019-06-27 close  223.
## 10 2019-06-28 close  223.
## # … with 617 more rows
Bstock_long %>%
  ggplot(aes(x = date, y = price, col = type)) +
  geom_line() + 
  theme(legend.position="bottom")

I changed the SMA short and Long but I didnt notice a difference in the Bearish crossover.

Q8 Hide the messages and the code, but display results of the code from the webpage.

Hint: Use message, echo and results in the chunk options. Refer to the RMarkdown Reference Guide.

Q9 Display the title and your name correctly at the top of the webpage.

Q10 Use the correct slug.