Q1 Load tidyquant and tidyverse packages.

Hint: Use library().

Q2 Import Target, a retail giant, for the last one year. Save the result under stock and print it.

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

Q3.a 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: 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

Q3.b 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: 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>

Q3.c Keep 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: 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

Q3.d 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: 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

Q3.e 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.

Q4 Add a subtitle to the chart that states “Target during the last one year”

Hint: There are many resources on the Web. For example, Google something like “ggplot2 adding subtitle”.

Q5 Identify the first buying opportunity with the date and closing price.

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

Q6 If you had invested $1 million on the day of the first buying point and followed the Bollinger Bands strategy, when would you have sold the stocks and at what price? Consider only one selling point immediately after the buying point in Q5.

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: 219 x 3
##    date       close sd2up
##    <date>     <dbl> <dbl>
##  1 2018-04-25  71.1  73.8
##  2 2018-04-26  71.9  73.7
##  3 2018-04-27  72.8  73.8
##  4 2018-04-30  72.6  73.6
##  5 2018-05-01  71.1  73.4
##  6 2018-05-02  71.4  73.4
##  7 2018-05-03  71.2  73.4
##  8 2018-05-04  71.1  73.3
##  9 2018-05-07  69.3  73.5
## 10 2018-05-08  69.4  73.5
## # ... with 209 more rows

Q7 How much would you have made or lost?

you would have made 85,000

Q8 Hide the code but display the results of the code on the webpage.

Hint: Change echo and results in the chunk options. The published webpage should display charts.

Q9 Display the title and your name correctly at the top of the webpage.

Q10 Use the correct slug.