Witht the given stock, conduct the Bollinger Bands analysis by answering the questions below.
## Loading required package: lubridate
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
## Loading required package: PerformanceAnalytics
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
##
## legend
## Loading required package: quantmod
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## == Need to Learn tidyquant? ====================================================
## Business Science offers a 1-hour course - Learning Lab #9: Performance Analysis & Portfolio Optimization with tidyquant!
## </> Learn more at: https://university.business-science.io/p/learning-labs-pro </>
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.3 v purrr 0.3.4
## v tibble 3.0.6 v dplyr 1.0.4
## v tidyr 1.1.2 v stringr 1.4.0
## v readr 1.4.0 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x lubridate::as.difftime() masks base::as.difftime()
## x lubridate::date() masks base::date()
## x dplyr::filter() masks stats::filter()
## x dplyr::first() masks xts::first()
## x lubridate::intersect() masks base::intersect()
## x dplyr::lag() masks stats::lag()
## x dplyr::last() masks xts::last()
## x lubridate::setdiff() masks base::setdiff()
## x lubridate::union() masks base::union()
## # A tibble: 313 x 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 MSFT 2020-01-02 159. 161. 158. 161. 22622100 159.
## 2 MSFT 2020-01-03 158. 160. 158. 159. 21116200 157.
## 3 MSFT 2020-01-06 157. 159. 157. 159. 20813700 157.
## 4 MSFT 2020-01-07 159. 160. 157. 158. 21634100 156.
## 5 MSFT 2020-01-08 159. 161. 158. 160. 27746500 158.
## 6 MSFT 2020-01-09 162. 162. 161. 162. 21385000 160.
## 7 MSFT 2020-01-10 163. 163. 161. 161. 20725900 159.
## 8 MSFT 2020-01-13 162. 163. 161. 163. 21626500 161.
## 9 MSFT 2020-01-14 163. 164. 162. 162. 23477400 160.
## 10 MSFT 2020-01-15 163. 164. 163. 163. 21417900 161.
## # ... with 303 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: 313 x 10
## symbol date open high low close volume adjusted SMA SD
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 MSFT 2020-01-02 159. 161. 158. 161. 22622100 159. NA NA
## 2 MSFT 2020-01-03 158. 160. 158. 159. 21116200 157. NA NA
## 3 MSFT 2020-01-06 157. 159. 157. 159. 20813700 157. NA NA
## 4 MSFT 2020-01-07 159. 160. 157. 158. 21634100 156. NA NA
## 5 MSFT 2020-01-08 159. 161. 158. 160. 27746500 158. NA NA
## 6 MSFT 2020-01-09 162. 162. 161. 162. 21385000 160. NA NA
## 7 MSFT 2020-01-10 163. 163. 161. 161. 20725900 159. NA NA
## 8 MSFT 2020-01-13 162. 163. 161. 163. 21626500 161. NA NA
## 9 MSFT 2020-01-14 163. 164. 162. 162. 23477400 160. NA NA
## 10 MSFT 2020-01-15 163. 164. 163. 163. 21417900 161. NA NA
## # ... with 303 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: 313 x 12
## symbol date open high low close volume adjusted SMA SD sd2up
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 MSFT 2020-01-02 159. 161. 158. 161. 2.26e7 159. NA NA NA
## 2 MSFT 2020-01-03 158. 160. 158. 159. 2.11e7 157. NA NA NA
## 3 MSFT 2020-01-06 157. 159. 157. 159. 2.08e7 157. NA NA NA
## 4 MSFT 2020-01-07 159. 160. 157. 158. 2.16e7 156. NA NA NA
## 5 MSFT 2020-01-08 159. 161. 158. 160. 2.77e7 158. NA NA NA
## 6 MSFT 2020-01-09 162. 162. 161. 162. 2.14e7 160. NA NA NA
## 7 MSFT 2020-01-10 163. 163. 161. 161. 2.07e7 159. NA NA NA
## 8 MSFT 2020-01-13 162. 163. 161. 163. 2.16e7 161. NA NA NA
## 9 MSFT 2020-01-14 163. 164. 162. 162. 2.35e7 160. NA NA NA
## 10 MSFT 2020-01-15 163. 164. 163. 163. 2.14e7 161. NA NA NA
## # ... with 303 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: 313 x 5
## date close SMA sd2up sd2down
## <date> <dbl> <dbl> <dbl> <dbl>
## 1 2020-01-02 161. NA NA NA
## 2 2020-01-03 159. NA NA NA
## 3 2020-01-06 159. NA NA NA
## 4 2020-01-07 158. NA NA NA
## 5 2020-01-08 160. NA NA NA
## 6 2020-01-09 162. NA NA NA
## 7 2020-01-10 161. NA NA NA
## 8 2020-01-13 163. NA NA NA
## 9 2020-01-14 162. NA NA NA
## 10 2020-01-15 163. NA NA NA
## # ... with 303 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,252 x 3
## date type price
## <date> <chr> <dbl>
## 1 2020-01-02 close 161.
## 2 2020-01-03 close 159.
## 3 2020-01-06 close 159.
## 4 2020-01-07 close 158.
## 5 2020-01-08 close 160.
## 6 2020-01-09 close 162.
## 7 2020-01-10 close 161.
## 8 2020-01-13 close 163.
## 9 2020-01-14 close 162.
## 10 2020-01-15 close 163.
## # ... with 1,242 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.
## Warning: Removed 57 row(s) containing missing values (geom_path).
Hint: A correct answer must show the per-share profit each scenario, the difference between the buying price and the selling price.
The upper band if from the average than the lower band, there fore there is more room to yield a positive return and statistically a higher chance.
Hint: Use message, echo and results in the chunk options. Refer to the RMarkdown Reference Guide.