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.

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

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.

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.

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.

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.

The first buying point is on May 7, 2018. This is known to be the first buying point as it is the first time that the closing price fell below the lower band. The closing price on that day was 69.33.

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.

The first selling point that aligns with the first buying point is on May 16, 2018. This is known to be a selling point as it is the first time the closing price goes above the upper band. The price at the first selling point is 75.23

If you had invested $1 million into the first buying point you would have bought about 14423 shares of stock(1,000,000/69.33=14423).

Q7 How much would you have made or lost?

If you had sold those shares at the first selling point, you would have made about $85,042.29 (14423*75.23=1,085,042.29).

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.