Hint: Use library().
library(tidyquant)
library(tidyverse)
Hint: Use tq_get() from the tidyquant package.
# Import data
from = today() - years(1)
stock <- tq_get("WMT", get = "stock.prices", from = from)
stock
## # A tibble: 251 x 7
## date open high low close volume adjusted
## <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2018-03-21 87.9 89.7 87.7 88.2 11836000 86.2
## 2 2018-03-22 87.5 88.3 87.0 87.1 12248200 85.2
## 3 2018-03-23 87.3 87.7 85.3 85.4 10125100 83.5
## 4 2018-03-26 86.2 87.7 86.1 87.5 9199800 85.5
## 5 2018-03-27 88.0 88.0 85.5 86.1 6545800 84.1
## 6 2018-03-28 86.3 88.2 86.2 87.8 11576300 85.8
## 7 2018-03-29 87.9 89.3 87.8 89.0 9274600 87.0
## 8 2018-04-02 88 88.2 84.8 85.6 19128900 83.6
## 9 2018-04-03 86.2 86.9 85.6 86.8 9695500 84.8
## 10 2018-04-04 85.6 87.5 85.4 87.2 6536600 85.2
## # ... with 241 more rows
Hint: Take stock, pipe it to tidyquant::tq_mutate to calculate 20-day moving averages, pipe it to tidyquant::tq_mutate to calculate 20-day running standard deviation, pipe it to rename() to rename value to SD, and assign the result to stock. To calculate the running standard deviation, use runSD in place of SMA. You can see all the available functions in tidyquant::tq_mutate using tq_mutate_fun_options().
stock <-
stock %>%
tq_mutate(select = close, mutate_fun = SMA, n = 20) %>%
rename(SMA.20 = SMA) %>%
tq_mutate(select = close, mutate_fun = runSD, n = 20) %>%
rename(SD = value)
stock
## # A tibble: 251 x 9
## date open high low close volume adjusted SMA.20 SD
## <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2018-03-21 87.9 89.7 87.7 88.2 11836000 86.2 NA NA
## 2 2018-03-22 87.5 88.3 87.0 87.1 12248200 85.2 NA NA
## 3 2018-03-23 87.3 87.7 85.3 85.4 10125100 83.5 NA NA
## 4 2018-03-26 86.2 87.7 86.1 87.5 9199800 85.5 NA NA
## 5 2018-03-27 88.0 88.0 85.5 86.1 6545800 84.1 NA NA
## 6 2018-03-28 86.3 88.2 86.2 87.8 11576300 85.8 NA NA
## 7 2018-03-29 87.9 89.3 87.8 89.0 9274600 87.0 NA NA
## 8 2018-04-02 88 88.2 84.8 85.6 19128900 83.6 NA NA
## 9 2018-04-03 86.2 86.9 85.6 86.8 9695500 84.8 NA NA
## 10 2018-04-04 85.6 87.5 85.4 87.2 6536600 85.2 NA NA
## # ... with 241 more rows
Hint: Take stock, pipe it to mutate(sd2up = SMA + 2 * SD, sd2down = SMA - 2 * SD), and assign the result to stock.
stock <-
stock %>%
mutate(sd2up = SMA.20 + 2 * SD, sd2down = SMA.20 - 2 * SD)
stock
## # A tibble: 251 x 11
## date open high low close volume adjusted SMA.20 SD sd2up
## <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2018-03-21 87.9 89.7 87.7 88.2 1.18e7 86.2 NA NA NA
## 2 2018-03-22 87.5 88.3 87.0 87.1 1.22e7 85.2 NA NA NA
## 3 2018-03-23 87.3 87.7 85.3 85.4 1.01e7 83.5 NA NA NA
## 4 2018-03-26 86.2 87.7 86.1 87.5 9.20e6 85.5 NA NA NA
## 5 2018-03-27 88.0 88.0 85.5 86.1 6.55e6 84.1 NA NA NA
## 6 2018-03-28 86.3 88.2 86.2 87.8 1.16e7 85.8 NA NA NA
## 7 2018-03-29 87.9 89.3 87.8 89.0 9.27e6 87.0 NA NA NA
## 8 2018-04-02 88 88.2 84.8 85.6 1.91e7 83.6 NA NA NA
## 9 2018-04-03 86.2 86.9 85.6 86.8 9.70e6 84.8 NA NA NA
## 10 2018-04-04 85.6 87.5 85.4 87.2 6.54e6 85.2 NA NA NA
## # ... with 241 more rows, and 1 more variable: sd2down <dbl>
Hint: Take stock, pipe it to dplyr::select to keep date, close, SMA, sd2up, and sd2down, and assign the result to stock_selected.
stock_selected <-
stock %>%
dplyr::select(date, close, SMA.20, sd2up, sd2down)
stock_selected
## # A tibble: 251 x 5
## date close SMA.20 sd2up sd2down
## <date> <dbl> <dbl> <dbl> <dbl>
## 1 2018-03-21 88.2 NA NA NA
## 2 2018-03-22 87.1 NA NA NA
## 3 2018-03-23 85.4 NA NA NA
## 4 2018-03-26 87.5 NA NA NA
## 5 2018-03-27 86.1 NA NA NA
## 6 2018-03-28 87.8 NA NA NA
## 7 2018-03-29 89.0 NA NA NA
## 8 2018-04-02 85.6 NA NA NA
## 9 2018-04-03 86.8 NA NA NA
## 10 2018-04-04 87.2 NA NA NA
## # ... with 241 more rows
Hint: Take stock_selected, pipe it to gather(key = type, value = price, close:sd2down), and assign the result to stock_long.
stock_long <-
stock_selected %>%
gather(key = type, value = price, close:sd2down)
stock_long
## # A tibble: 1,004 x 3
## date type price
## <date> <chr> <dbl>
## 1 2018-03-21 close 88.2
## 2 2018-03-22 close 87.1
## 3 2018-03-23 close 85.4
## 4 2018-03-26 close 87.5
## 5 2018-03-27 close 86.1
## 6 2018-03-28 close 87.8
## 7 2018-03-29 close 89.0
## 8 2018-04-02 close 85.6
## 9 2018-04-03 close 86.8
## 10 2018-04-04 close 87.2
## # ... with 994 more rows
Hint: Take stock_selected and pipe it to ggplot(). Map date to the x-axis, price to the y-axis, and type to color in the line chart.
stock_long %>%
ggplot(aes(x = date, y = price, color = type)) +
geom_line()
If the stock price falls below the lower band, the stock may be considered oversold.
When the bands narrow, it may be used as an indication that volatility is about to rise.
Buying points happen at July of 2018 and again in Deember of 2018 Selling points happen around mid August of 2018 and again mid November 2018.
You would have aboout $20 more per share of the stock meaning you would have a lot more than that of $1million today.