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
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
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
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")
Hint: Elaborate your calculation. One word/number anwer is not enough.
February 19 to March 17 would make it a 27 day 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.
The reduced lag time is about 14 days.
Hint: Use message, echo and results in the chunk options. Refer to the RMarkdown Reference Guide.