Hint: Use library().
Hint: Use tq_get() from the tidyquant package.
## # A tibble: 251 x 7
## date open high low close volume adjusted
## <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2018-03-28 68.7 71.8 68.7 69.2 8052400 66.9
## 2 2018-03-29 69.2 69.9 68.9 69.4 7594300 67.1
## 3 2018-04-02 69.0 69.4 67.4 69.0 5903000 66.7
## 4 2018-04-03 69.4 69.9 69.0 69.5 3484200 67.2
## 5 2018-04-04 68.9 71.9 68.8 71.9 5200200 69.5
## 6 2018-04-05 72.0 72.6 70.7 72.4 4525100 70.1
## 7 2018-04-06 71.8 73.4 71.5 72.3 6360100 69.9
## 8 2018-04-09 72.4 72.7 71.4 71.5 3093300 69.1
## 9 2018-04-10 72.9 73.0 71.8 72.8 4653900 70.4
## 10 2018-04-11 72.5 73.5 72.1 72.8 3545700 70.4
## # ... 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().
## # A tibble: 251 x 9
## date open high low close volume adjusted SMA SD
## <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2018-03-28 68.7 71.8 68.7 69.2 8052400 66.9 NA NA
## 2 2018-03-29 69.2 69.9 68.9 69.4 7594300 67.1 NA NA
## 3 2018-04-02 69.0 69.4 67.4 69.0 5903000 66.7 NA NA
## 4 2018-04-03 69.4 69.9 69.0 69.5 3484200 67.2 NA NA
## 5 2018-04-04 68.9 71.9 68.8 71.9 5200200 69.5 NA NA
## 6 2018-04-05 72.0 72.6 70.7 72.4 4525100 70.1 NA NA
## 7 2018-04-06 71.8 73.4 71.5 72.3 6360100 69.9 NA NA
## 8 2018-04-09 72.4 72.7 71.4 71.5 3093300 69.1 NA NA
## 9 2018-04-10 72.9 73.0 71.8 72.8 4653900 70.4 NA NA
## 10 2018-04-11 72.5 73.5 72.1 72.8 3545700 70.4 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.
## # A tibble: 251 x 11
## date open high low close volume adjusted SMA SD sd2up
## <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2018-03-28 68.7 71.8 68.7 69.2 8.05e6 66.9 NA NA NA
## 2 2018-03-29 69.2 69.9 68.9 69.4 7.59e6 67.1 NA NA NA
## 3 2018-04-02 69.0 69.4 67.4 69.0 5.90e6 66.7 NA NA NA
## 4 2018-04-03 69.4 69.9 69.0 69.5 3.48e6 67.2 NA NA NA
## 5 2018-04-04 68.9 71.9 68.8 71.9 5.20e6 69.5 NA NA NA
## 6 2018-04-05 72.0 72.6 70.7 72.4 4.53e6 70.1 NA NA NA
## 7 2018-04-06 71.8 73.4 71.5 72.3 6.36e6 69.9 NA NA NA
## 8 2018-04-09 72.4 72.7 71.4 71.5 3.09e6 69.1 NA NA NA
## 9 2018-04-10 72.9 73.0 71.8 72.8 4.65e6 70.4 NA NA NA
## 10 2018-04-11 72.5 73.5 72.1 72.8 3.55e6 70.4 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.
## # A tibble: 251 x 5
## date close SMA sd2up sd2down
## <date> <dbl> <dbl> <dbl> <dbl>
## 1 2018-03-28 69.2 NA NA NA
## 2 2018-03-29 69.4 NA NA NA
## 3 2018-04-02 69.0 NA NA NA
## 4 2018-04-03 69.5 NA NA NA
## 5 2018-04-04 71.9 NA NA NA
## 6 2018-04-05 72.4 NA NA NA
## 7 2018-04-06 72.3 NA NA NA
## 8 2018-04-09 71.5 NA NA NA
## 9 2018-04-10 72.8 NA NA NA
## 10 2018-04-11 72.8 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.
## # A tibble: 1,004 x 3
## date type price
## <date> <chr> <dbl>
## 1 2018-03-28 close 69.2
## 2 2018-03-29 close 69.4
## 3 2018-04-02 close 69.0
## 4 2018-04-03 close 69.5
## 5 2018-04-04 close 71.9
## 6 2018-04-05 close 72.4
## 7 2018-04-06 close 72.3
## 8 2018-04-09 close 71.5
## 9 2018-04-10 close 72.8
## 10 2018-04-11 close 72.8
## # ... 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.
Hint: There are many resources on the Web. For example, Google something like “ggplot2 adding subtitle”.
-See question 5 above.
Hint: Take stock, pipe it to dplyr::select to keep only three variables (date, close, and sd2down), and pipe it to dplyr::filter to select the rows where closing prices are smaller than the lower band.
## # A tibble: 12 x 3
## date close sd2down
## <date> <dbl> <dbl>
## 1 2018-05-07 69.3 69.7
## 2 2018-10-03 86.0 86.2
## 3 2018-10-04 84.8 85.6
## 4 2018-10-05 84.5 85.0
## 5 2018-10-11 82.8 83.7
## 6 2018-11-16 79.7 80.0
## 7 2018-11-19 77.8 78.9
## 8 2018-11-20 69.0 74.9
## 9 2018-11-21 69.3 72.1
## 10 2018-11-23 67.3 69.3
## 11 2018-12-19 62.8 63.4
## 12 2018-12-20 61.7 62.2
-The first buying opportunity is on May 7th, 2018 and the closing price for that day was $69.33.
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.
Hint: Take stock, pipe it to dplyr::select to keep only three variables (date, close, and sd2up), and pipe it to dplyr::filter to select the rows where closing prices are greater than the upper band.
## # A tibble: 13 x 3
## date close sd2up
## <date> <dbl> <dbl>
## 1 2018-05-16 75.2 74.2
## 2 2018-05-17 75.8 75.1
## 3 2018-05-18 75.9 75.8
## 4 2018-05-21 76.9 76.7
## 5 2018-07-24 79.4 79.2
## 6 2018-07-25 80.0 79.7
## 7 2018-07-30 81.2 80.9
## 8 2018-08-22 85.9 85.0
## 9 2018-08-23 86.7 85.8
## 10 2018-08-24 87.3 86.7
## 11 2018-11-08 87.6 87.4
## 12 2019-03-05 76 74.9
## 13 2019-03-06 76.9 75.8
-Intially when buying the stocks with 1,000,000 you would get 14,423.77 shares and you would sell the stocks at 75.23 eventually you end up making 1,085,100.22. Which therefore would lead you to gaining a profit in the amount of money you made by making 85,100.22 more than what you had invested in. Typically people buy low and sell high when you make your initial investment.
Hint: Change echo and results in the chunk options. The published webpage should display charts.