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
## Version 0.4-0 included new data defaults. See ?getSymbols.
## == 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.2     v purrr   0.3.4
## v tibble  3.0.3     v dplyr   1.0.2
## v tidyr   1.1.2     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.5.0
## -- 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: 189 x 8
##    symbol date        open  high   low close  volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>
##  1 AMZN   2020-01-02 1875  1898. 1864. 1898. 4029000    1898.
##  2 AMZN   2020-01-03 1864. 1886. 1864. 1875. 3764400    1875.
##  3 AMZN   2020-01-06 1860  1904. 1860  1903. 4061800    1903.
##  4 AMZN   2020-01-07 1904. 1914. 1892. 1907. 4044900    1907.
##  5 AMZN   2020-01-08 1898. 1911  1886. 1892. 3508000    1892.
##  6 AMZN   2020-01-09 1910. 1918. 1896. 1901. 3167300    1901.
##  7 AMZN   2020-01-10 1905. 1907. 1880  1883. 2853700    1883.
##  8 AMZN   2020-01-13 1891. 1898  1881. 1891. 2780800    1891.
##  9 AMZN   2020-01-14 1886. 1887. 1859. 1869. 3440900    1869.
## 10 AMZN   2020-01-15 1872. 1879. 1855. 1862. 2896600    1862.
## # ... with 179 more rows

Q1 Calculate 20-day moving averages and 20-day running standard deviation. Save the result under stock and print it.

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: 189 x 10
##    symbol date        open  high   low close  volume adjusted   SMA    SD
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl> <dbl> <dbl>
##  1 AMZN   2020-01-02 1875  1898. 1864. 1898. 4029000    1898.    NA    NA
##  2 AMZN   2020-01-03 1864. 1886. 1864. 1875. 3764400    1875.    NA    NA
##  3 AMZN   2020-01-06 1860  1904. 1860  1903. 4061800    1903.    NA    NA
##  4 AMZN   2020-01-07 1904. 1914. 1892. 1907. 4044900    1907.    NA    NA
##  5 AMZN   2020-01-08 1898. 1911  1886. 1892. 3508000    1892.    NA    NA
##  6 AMZN   2020-01-09 1910. 1918. 1896. 1901. 3167300    1901.    NA    NA
##  7 AMZN   2020-01-10 1905. 1907. 1880  1883. 2853700    1883.    NA    NA
##  8 AMZN   2020-01-13 1891. 1898  1881. 1891. 2780800    1891.    NA    NA
##  9 AMZN   2020-01-14 1886. 1887. 1859. 1869. 3440900    1869.    NA    NA
## 10 AMZN   2020-01-15 1872. 1879. 1855. 1862. 2896600    1862.    NA    NA
## # ... with 179 more rows

Q2 Calculate the Bollinger Bands. Save the result under stock and print it.

Hint: Take stock, pipe it to mutate(sd2up = SMA + 2 * SD, sd2down = SMA - 2 * SD), and assign the result to stock.

## # A tibble: 189 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 AMZN   2020-01-02 1875  1898. 1864. 1898. 4.03e6    1898.    NA    NA    NA
##  2 AMZN   2020-01-03 1864. 1886. 1864. 1875. 3.76e6    1875.    NA    NA    NA
##  3 AMZN   2020-01-06 1860  1904. 1860  1903. 4.06e6    1903.    NA    NA    NA
##  4 AMZN   2020-01-07 1904. 1914. 1892. 1907. 4.04e6    1907.    NA    NA    NA
##  5 AMZN   2020-01-08 1898. 1911  1886. 1892. 3.51e6    1892.    NA    NA    NA
##  6 AMZN   2020-01-09 1910. 1918. 1896. 1901. 3.17e6    1901.    NA    NA    NA
##  7 AMZN   2020-01-10 1905. 1907. 1880  1883. 2.85e6    1883.    NA    NA    NA
##  8 AMZN   2020-01-13 1891. 1898  1881. 1891. 2.78e6    1891.    NA    NA    NA
##  9 AMZN   2020-01-14 1886. 1887. 1859. 1869. 3.44e6    1869.    NA    NA    NA
## 10 AMZN   2020-01-15 1872. 1879. 1855. 1862. 2.90e6    1862.    NA    NA    NA
## # ... with 179 more rows, and 1 more variable: sd2down <dbl>

Q3 Select variables to build the Bollinger Bands. Save the result under stock_selected and print it.

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: 189 x 5
##    date       close   SMA sd2up sd2down
##    <date>     <dbl> <dbl> <dbl>   <dbl>
##  1 2020-01-02 1898.    NA    NA      NA
##  2 2020-01-03 1875.    NA    NA      NA
##  3 2020-01-06 1903.    NA    NA      NA
##  4 2020-01-07 1907.    NA    NA      NA
##  5 2020-01-08 1892.    NA    NA      NA
##  6 2020-01-09 1901.    NA    NA      NA
##  7 2020-01-10 1883.    NA    NA      NA
##  8 2020-01-13 1891.    NA    NA      NA
##  9 2020-01-14 1869.    NA    NA      NA
## 10 2020-01-15 1862.    NA    NA      NA
## # ... with 179 more rows

Q4 Transform data to long form from wide form for graphing.Save the result under stock_long and print it.

Hint: Take stock_selected, pipe it to gather(key = type, value = price, close:sd2down), and assign the result to stock_long.

## # A tibble: 756 x 3
##    date       type  price
##    <date>     <chr> <dbl>
##  1 2020-01-02 close 1898.
##  2 2020-01-03 close 1875.
##  3 2020-01-06 close 1903.
##  4 2020-01-07 close 1907.
##  5 2020-01-08 close 1892.
##  6 2020-01-09 close 1901.
##  7 2020-01-10 close 1883.
##  8 2020-01-13 close 1891.
##  9 2020-01-14 close 1869.
## 10 2020-01-15 close 1862.
## # ... with 746 more rows

Q5 Visualize data.

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).

Q6 If you had invested $1 million on the day of the first buying point and sold all the shares on the following selling point, how much would you have won or lost?

More than 1 million. I would have won $105,512

Q7 An analyst may tinker with the width of the Bollinger Bands depending the volatility of the stock. Which of the following scenarios would have yield the largest profit? Elaborate your answer.

Hint: A correct answer must show the per-share profit each scenario, the difference between the buying price and the selling price.

  1. the upper band being 2 sd up from the average and the lower band 2 sd below (standard)
  2. the upper band being 2.5 sd up from the average and the lower band 2 sd below

Q8 Hide the messages, the code, and its results on 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.