## # A tibble: 129 x 8
##    symbol date        open  high   low close     volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>      <dbl>    <dbl>
##  1 ^GSPC  2019-05-01 2952. 2954. 2923. 2924. 3645850000    2924.
##  2 ^GSPC  2019-05-02 2922. 2932. 2900. 2918. 3778890000    2918.
##  3 ^GSPC  2019-05-03 2929. 2948. 2929. 2946. 3338120000    2946.
##  4 ^GSPC  2019-05-06 2909. 2937. 2898. 2932. 3181520000    2932.
##  5 ^GSPC  2019-05-07 2913. 2913. 2863. 2884. 3767100000    2884.
##  6 ^GSPC  2019-05-08 2880. 2898. 2873. 2879. 3485790000    2879.
##  7 ^GSPC  2019-05-09 2860. 2876. 2836. 2871. 3638820000    2871.
##  8 ^GSPC  2019-05-10 2863. 2891. 2825. 2881. 3529600000    2881.
##  9 ^GSPC  2019-05-13 2840. 2840. 2801. 2812. 3894030000    2812.
## 10 ^GSPC  2019-05-14 2820. 2853. 2820. 2834. 3322720000    2834.
## # … with 119 more rows
## # A tibble: 129 x 10
##    symbol date        open  high   low close     volume adjusted   SMA    SD
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>      <dbl>    <dbl> <dbl> <dbl>
##  1 ^GSPC  2019-05-01 2952. 2954. 2923. 2924. 3645850000    2924.    NA    NA
##  2 ^GSPC  2019-05-02 2922. 2932. 2900. 2918. 3778890000    2918.    NA    NA
##  3 ^GSPC  2019-05-03 2929. 2948. 2929. 2946. 3338120000    2946.    NA    NA
##  4 ^GSPC  2019-05-06 2909. 2937. 2898. 2932. 3181520000    2932.    NA    NA
##  5 ^GSPC  2019-05-07 2913. 2913. 2863. 2884. 3767100000    2884.    NA    NA
##  6 ^GSPC  2019-05-08 2880. 2898. 2873. 2879. 3485790000    2879.    NA    NA
##  7 ^GSPC  2019-05-09 2860. 2876. 2836. 2871. 3638820000    2871.    NA    NA
##  8 ^GSPC  2019-05-10 2863. 2891. 2825. 2881. 3529600000    2881.    NA    NA
##  9 ^GSPC  2019-05-13 2840. 2840. 2801. 2812. 3894030000    2812.    NA    NA
## 10 ^GSPC  2019-05-14 2820. 2853. 2820. 2834. 3322720000    2834.    NA    NA
## # … with 119 more rows
## # A tibble: 129 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 ^GSPC  2019-05-01 2952. 2954. 2923. 2924. 3.65e9    2924.    NA    NA    NA
##  2 ^GSPC  2019-05-02 2922. 2932. 2900. 2918. 3.78e9    2918.    NA    NA    NA
##  3 ^GSPC  2019-05-03 2929. 2948. 2929. 2946. 3.34e9    2946.    NA    NA    NA
##  4 ^GSPC  2019-05-06 2909. 2937. 2898. 2932. 3.18e9    2932.    NA    NA    NA
##  5 ^GSPC  2019-05-07 2913. 2913. 2863. 2884. 3.77e9    2884.    NA    NA    NA
##  6 ^GSPC  2019-05-08 2880. 2898. 2873. 2879. 3.49e9    2879.    NA    NA    NA
##  7 ^GSPC  2019-05-09 2860. 2876. 2836. 2871. 3.64e9    2871.    NA    NA    NA
##  8 ^GSPC  2019-05-10 2863. 2891. 2825. 2881. 3.53e9    2881.    NA    NA    NA
##  9 ^GSPC  2019-05-13 2840. 2840. 2801. 2812. 3.89e9    2812.    NA    NA    NA
## 10 ^GSPC  2019-05-14 2820. 2853. 2820. 2834. 3.32e9    2834.    NA    NA    NA
## # … with 119 more rows, and 1 more variable: sd2down <dbl>
## # A tibble: 129 x 5
##    date       close   SMA sd2up sd2down
##    <date>     <dbl> <dbl> <dbl>   <dbl>
##  1 2019-05-01 2924.    NA    NA      NA
##  2 2019-05-02 2918.    NA    NA      NA
##  3 2019-05-03 2946.    NA    NA      NA
##  4 2019-05-06 2932.    NA    NA      NA
##  5 2019-05-07 2884.    NA    NA      NA
##  6 2019-05-08 2879.    NA    NA      NA
##  7 2019-05-09 2871.    NA    NA      NA
##  8 2019-05-10 2881.    NA    NA      NA
##  9 2019-05-13 2812.    NA    NA      NA
## 10 2019-05-14 2834.    NA    NA      NA
## # … with 119 more rows
## # A tibble: 516 x 3
##    date       type  price
##    <date>     <chr> <dbl>
##  1 2019-05-01 close 2924.
##  2 2019-05-02 close 2918.
##  3 2019-05-03 close 2946.
##  4 2019-05-06 close 2932.
##  5 2019-05-07 close 2884.
##  6 2019-05-08 close 2879.
##  7 2019-05-09 close 2871.
##  8 2019-05-10 close 2881.
##  9 2019-05-13 close 2812.
## 10 2019-05-14 close 2834.
## # … with 506 more rows

Q1 Import Walmart for the same period. Save the result under stock and print it.

Hint: Use tq_get() from the tidyquant package.

## # A tibble: 129 x 8
##    symbol date        open  high   low close  volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>
##  1 WMT    2019-05-01 103.  103.  101.  101.  5337200     99.4
##  2 WMT    2019-05-02 101.  102.  100.  101.  5007000     99.2
##  3 WMT    2019-05-03 102.  103.  102.  102.  5316900    100. 
##  4 WMT    2019-05-06 101.  103.  101.  102.  4613700    101. 
##  5 WMT    2019-05-07 102.  102.  101.  101.  4575600     99.4
##  6 WMT    2019-05-08 100.  101.   99.9 100.  5882000     98.4
##  7 WMT    2019-05-09  99.3  99.9  98.8  99.5 6240100     98.2
##  8 WMT    2019-05-10  99.8 102.   99.7 102.  8742700    101. 
##  9 WMT    2019-05-13 100.  101.   99.0  99.9 7361500     98.5
## 10 WMT    2019-05-14 100.  101.   99.9 100.  6353700     98.9
## # … with 119 more rows

Q2 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: 129 x 10
##    symbol date        open  high   low close  volume adjusted   SMA    SD
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl> <dbl> <dbl>
##  1 WMT    2019-05-01 103.  103.  101.  101.  5337200     99.4    NA    NA
##  2 WMT    2019-05-02 101.  102.  100.  101.  5007000     99.2    NA    NA
##  3 WMT    2019-05-03 102.  103.  102.  102.  5316900    100.     NA    NA
##  4 WMT    2019-05-06 101.  103.  101.  102.  4613700    101.     NA    NA
##  5 WMT    2019-05-07 102.  102.  101.  101.  4575600     99.4    NA    NA
##  6 WMT    2019-05-08 100.  101.   99.9 100.  5882000     98.4    NA    NA
##  7 WMT    2019-05-09  99.3  99.9  98.8  99.5 6240100     98.2    NA    NA
##  8 WMT    2019-05-10  99.8 102.   99.7 102.  8742700    101.     NA    NA
##  9 WMT    2019-05-13 100.  101.   99.0  99.9 7361500     98.5    NA    NA
## 10 WMT    2019-05-14 100.  101.   99.9 100.  6353700     98.9    NA    NA
## # … with 119 more rows

Q3 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: 129 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 WMT    2019-05-01 103.  103.  101.  101.  5.34e6     99.4    NA    NA    NA
##  2 WMT    2019-05-02 101.  102.  100.  101.  5.01e6     99.2    NA    NA    NA
##  3 WMT    2019-05-03 102.  103.  102.  102.  5.32e6    100.     NA    NA    NA
##  4 WMT    2019-05-06 101.  103.  101.  102.  4.61e6    101.     NA    NA    NA
##  5 WMT    2019-05-07 102.  102.  101.  101.  4.58e6     99.4    NA    NA    NA
##  6 WMT    2019-05-08 100.  101.   99.9 100.  5.88e6     98.4    NA    NA    NA
##  7 WMT    2019-05-09  99.3  99.9  98.8  99.5 6.24e6     98.2    NA    NA    NA
##  8 WMT    2019-05-10  99.8 102.   99.7 102.  8.74e6    101.     NA    NA    NA
##  9 WMT    2019-05-13 100.  101.   99.0  99.9 7.36e6     98.5    NA    NA    NA
## 10 WMT    2019-05-14 100.  101.   99.9 100.  6.35e6     98.9    NA    NA    NA
## # … with 119 more rows, and 1 more variable: sd2down <dbl>

Q4 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: 129 x 5
##    date       close   SMA sd2up sd2down
##    <date>     <dbl> <dbl> <dbl>   <dbl>
##  1 2019-05-01 101.     NA    NA      NA
##  2 2019-05-02 101.     NA    NA      NA
##  3 2019-05-03 102.     NA    NA      NA
##  4 2019-05-06 102.     NA    NA      NA
##  5 2019-05-07 101.     NA    NA      NA
##  6 2019-05-08 100.     NA    NA      NA
##  7 2019-05-09  99.5    NA    NA      NA
##  8 2019-05-10 102.     NA    NA      NA
##  9 2019-05-13  99.9    NA    NA      NA
## 10 2019-05-14 100.     NA    NA      NA
## # … with 119 more rows

Q5 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: 516 x 3
##    date       type  price
##    <date>     <chr> <dbl>
##  1 2019-05-01 close 101. 
##  2 2019-05-02 close 101. 
##  3 2019-05-03 close 102. 
##  4 2019-05-06 close 102. 
##  5 2019-05-07 close 101. 
##  6 2019-05-08 close 100. 
##  7 2019-05-09 close  99.5
##  8 2019-05-10 close 102. 
##  9 2019-05-13 close  99.9
## 10 2019-05-14 close 100. 
## # … with 506 more rows

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

Q7 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?

July 31st 2019 the closing price goes below the lower band (sd2down) at a price of $110 dollars. With 1 million, you can buy 9090 shares. You can see that the closing price goes above the upper band (sd2up) on October 11th 2019 at price of $120 dollars. You are selling 9090 shares at the new price of $120 dollars. 9090*120= 1090909. This means you made $90909 dollars, approximately 9%.

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.