Q1 Import Tesla for the last ten months.

Hint: Revise the given code below.

library(tidyquant)
library(tidyverse)

# Import data
from = today() - months(24)
stock <- tq_get("TSLA", get = "stock.prices", from = from)
stock
## # A tibble: 504 x 8
##    symbol date        open  high   low close  volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>
##  1 TSLA   2018-04-17  289.  292.  283.  288. 7000000     288.
##  2 TSLA   2018-04-18  291.  300.  288.  293. 6557700     293.
##  3 TSLA   2018-04-19  291.  301.  289.  300. 6090600     300.
##  4 TSLA   2018-04-20  295.  300.  290.  290. 5627900     290.
##  5 TSLA   2018-04-23  291.  292.  282.  283. 4893400     283.
##  6 TSLA   2018-04-24  285   287.  278.  283. 5685300     283.
##  7 TSLA   2018-04-25  284.  285.  277.  281. 4013600     281.
##  8 TSLA   2018-04-26  279.  286.  276.  285. 4356000     285.
##  9 TSLA   2018-04-27  285.  294.  284.  294. 4364600     294.
## 10 TSLA   2018-04-30  294.  299.  292.  294. 4228200     294.
## # … with 494 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 %>%

  tq_mutate(select = close, mutate_fun = SMA, n = 15) %>%

  rename(SMA.short = SMA) %>%
  tq_mutate(select = close, mutate_fun = SMA, n = 50) %>%
  rename(SMA.long = SMA)

stock
## # A tibble: 504 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   2018-04-17  289.  292.  283.  288. 7000000     288.        NA       NA
##  2 TSLA   2018-04-18  291.  300.  288.  293. 6557700     293.        NA       NA
##  3 TSLA   2018-04-19  291.  301.  289.  300. 6090600     300.        NA       NA
##  4 TSLA   2018-04-20  295.  300.  290.  290. 5627900     290.        NA       NA
##  5 TSLA   2018-04-23  291.  292.  282.  283. 4893400     283.        NA       NA
##  6 TSLA   2018-04-24  285   287.  278.  283. 5685300     283.        NA       NA
##  7 TSLA   2018-04-25  284.  285.  277.  281. 4013600     281.        NA       NA
##  8 TSLA   2018-04-26  279.  286.  276.  285. 4356000     285.        NA       NA
##  9 TSLA   2018-04-27  285.  294.  284.  294. 4364600     294.        NA       NA
## 10 TSLA   2018-04-30  294.  299.  292.  294. 4228200     294.        NA       NA
## # … with 494 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: 1,512 x 3
##    date       type  price
##    <date>     <chr> <dbl>
##  1 2018-04-17 close  288.
##  2 2018-04-18 close  293.
##  3 2018-04-19 close  300.
##  4 2018-04-20 close  290.
##  5 2018-04-23 close  283.
##  6 2018-04-24 close  283.
##  7 2018-04-25 close  281.
##  8 2018-04-26 close  285.
##  9 2018-04-27 close  294.
## 10 2018-04-30 close  294.
## # … with 1,502 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 = "Simple Moving Averages of Tesla",
       x = "date",
       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.

From where the crossovers were, the profit would be about 1.83 million.

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.

February 19 to March 17 would make it a 27 day lag.

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.

I would make the SMA averages lower than 15 and 50 to reduce lag time.

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

The reduced lag time is about 14 days.

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.