# Load packages
library(tidyquant)
library(tidyverse)

# Import data
from = today() - months(6)
stock <- tq_get("^GSPC", get = "stock.prices", from = from)
stock
## # A tibble: 127 x 7
##    date        open  high   low close     volume adjusted
##    <date>     <dbl> <dbl> <dbl> <dbl>      <dbl>    <dbl>
##  1 2019-04-17 2916. 2918  2895. 2900. 3602300000    2900.
##  2 2019-04-18 2905. 2908. 2892. 2905. 3506850000    2905.
##  3 2019-04-22 2899. 2910. 2896. 2908. 2997950000    2908.
##  4 2019-04-23 2910. 2936. 2909. 2934. 3635030000    2934.
##  5 2019-04-24 2934  2937. 2926. 2927. 3448960000    2927.
##  6 2019-04-25 2929. 2933. 2913. 2926. 3425280000    2926.
##  7 2019-04-26 2926. 2940. 2918. 2940. 3248500000    2940.
##  8 2019-04-29 2941. 2950. 2939. 2943. 3118780000    2943.
##  9 2019-04-30 2937. 2948. 2924. 2946. 3919330000    2946.
## 10 2019-05-01 2952. 2954. 2923. 2924. 3645850000    2924.
## # … with 117 more rows
# Calculate 20-day moving averages and 20-day running standard deviation
stock <-
  stock %>%
    tq_mutate(select = close, mutate_fun = SMA, n = 20) %>%
    tq_mutate(select = close, mutate_fun = runSD, n = 20) %>%
    rename(SD = value)

stock
## # A tibble: 127 x 9
##    date        open  high   low close     volume adjusted   SMA    SD
##    <date>     <dbl> <dbl> <dbl> <dbl>      <dbl>    <dbl> <dbl> <dbl>
##  1 2019-04-17 2916. 2918  2895. 2900. 3602300000    2900.    NA    NA
##  2 2019-04-18 2905. 2908. 2892. 2905. 3506850000    2905.    NA    NA
##  3 2019-04-22 2899. 2910. 2896. 2908. 2997950000    2908.    NA    NA
##  4 2019-04-23 2910. 2936. 2909. 2934. 3635030000    2934.    NA    NA
##  5 2019-04-24 2934  2937. 2926. 2927. 3448960000    2927.    NA    NA
##  6 2019-04-25 2929. 2933. 2913. 2926. 3425280000    2926.    NA    NA
##  7 2019-04-26 2926. 2940. 2918. 2940. 3248500000    2940.    NA    NA
##  8 2019-04-29 2941. 2950. 2939. 2943. 3118780000    2943.    NA    NA
##  9 2019-04-30 2937. 2948. 2924. 2946. 3919330000    2946.    NA    NA
## 10 2019-05-01 2952. 2954. 2923. 2924. 3645850000    2924.    NA    NA
## # … with 117 more rows
# Calculate Bollinger Bands
stock <-
  stock %>%
  mutate(sd2up = SMA + 2 * SD,
         sd2down = SMA - 2 * SD)

stock
## # A tibble: 127 x 11
##    date        open  high   low close volume adjusted   SMA    SD sd2up
##    <date>     <dbl> <dbl> <dbl> <dbl>  <dbl>    <dbl> <dbl> <dbl> <dbl>
##  1 2019-04-17 2916. 2918  2895. 2900. 3.60e9    2900.    NA    NA    NA
##  2 2019-04-18 2905. 2908. 2892. 2905. 3.51e9    2905.    NA    NA    NA
##  3 2019-04-22 2899. 2910. 2896. 2908. 3.00e9    2908.    NA    NA    NA
##  4 2019-04-23 2910. 2936. 2909. 2934. 3.64e9    2934.    NA    NA    NA
##  5 2019-04-24 2934  2937. 2926. 2927. 3.45e9    2927.    NA    NA    NA
##  6 2019-04-25 2929. 2933. 2913. 2926. 3.43e9    2926.    NA    NA    NA
##  7 2019-04-26 2926. 2940. 2918. 2940. 3.25e9    2940.    NA    NA    NA
##  8 2019-04-29 2941. 2950. 2939. 2943. 3.12e9    2943.    NA    NA    NA
##  9 2019-04-30 2937. 2948. 2924. 2946. 3.92e9    2946.    NA    NA    NA
## 10 2019-05-01 2952. 2954. 2923. 2924. 3.65e9    2924.    NA    NA    NA
## # … with 117 more rows, and 1 more variable: sd2down <dbl>
# Select variables of interest
stock_selected <-
  stock %>%
  select(date, close, SMA, sd2up, sd2down)

stock_selected
## # A tibble: 127 x 5
##    date       close   SMA sd2up sd2down
##    <date>     <dbl> <dbl> <dbl>   <dbl>
##  1 2019-04-17 2900.    NA    NA      NA
##  2 2019-04-18 2905.    NA    NA      NA
##  3 2019-04-22 2908.    NA    NA      NA
##  4 2019-04-23 2934.    NA    NA      NA
##  5 2019-04-24 2927.    NA    NA      NA
##  6 2019-04-25 2926.    NA    NA      NA
##  7 2019-04-26 2940.    NA    NA      NA
##  8 2019-04-29 2943.    NA    NA      NA
##  9 2019-04-30 2946.    NA    NA      NA
## 10 2019-05-01 2924.    NA    NA      NA
## # … with 117 more rows
# Transform to long form to wide form for graphing
stock_long <-
  stock_selected %>%
  gather(key = type, value = price, close:sd2down)

stock_long
## # A tibble: 508 x 3
##    date       type  price
##    <date>     <chr> <dbl>
##  1 2019-04-17 close 2900.
##  2 2019-04-18 close 2905.
##  3 2019-04-22 close 2908.
##  4 2019-04-23 close 2934.
##  5 2019-04-24 close 2927.
##  6 2019-04-25 close 2926.
##  7 2019-04-26 close 2940.
##  8 2019-04-29 close 2943.
##  9 2019-04-30 close 2946.
## 10 2019-05-01 close 2924.
## # … with 498 more rows
# Visualize
stock_long %>%
  ggplot(aes(x = date, y = price, color = type)) +
  geom_line() + 
  theme(legend.position="bottom") +
  labs(title = "Bollinger Bands",
       x = NULL,
       y = "Stock Prices")

Q1 Import Walmart for the last one year. Save the result under stock and print it.

library(tidyquant)
library(tidyverse)

# Import data
from = today() - months(12)
stockw <- tq_get("WMT", get = "stock.prices", from = from)
stockw
## # A tibble: 251 x 7
##    date        open  high   low close   volume adjusted
##    <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 2018-10-17  95.2  96.6  94.9  96.6  9539400     94.6
##  2 2018-10-18  96.4  97.2  95.7  96.2 11398400     94.2
##  3 2018-10-19  96    97.7  96    97.2  8950700     95.1
##  4 2018-10-22  97.3  97.6  96.7  97.1  7122900     95.1
##  5 2018-10-23  96.5  98.0  96.1  97.8  9405800     95.8
##  6 2018-10-24  97.5  99.2  97.3  97.6 10364700     95.5
##  7 2018-10-25  98.0  99.5  97    99.2 10737100     97.1
##  8 2018-10-26  98.8  99.7  98.0  98.9 14925600     96.9
##  9 2018-10-29  99.5 102.   99.1  99.8 12706100     97.7
## 10 2018-10-30 100.  103.  100.  102.  12708500    100. 
## # … with 241 more rows

Hint: Use tq_get() from the tidyquant package.

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

stockw <-
  stock %>%
    tq_mutate(select = close, mutate_fun = SMA, n = 20) %>%
    tq_mutate(select = close, mutate_fun = runSD, n = 20) %>%
    rename(SD = value)

stockw
## # A tibble: 127 x 13
##    date        open  high   low close volume adjusted   SMA    SD sd2up
##    <date>     <dbl> <dbl> <dbl> <dbl>  <dbl>    <dbl> <dbl> <dbl> <dbl>
##  1 2019-04-17 2916. 2918  2895. 2900. 3.60e9    2900.    NA    NA    NA
##  2 2019-04-18 2905. 2908. 2892. 2905. 3.51e9    2905.    NA    NA    NA
##  3 2019-04-22 2899. 2910. 2896. 2908. 3.00e9    2908.    NA    NA    NA
##  4 2019-04-23 2910. 2936. 2909. 2934. 3.64e9    2934.    NA    NA    NA
##  5 2019-04-24 2934  2937. 2926. 2927. 3.45e9    2927.    NA    NA    NA
##  6 2019-04-25 2929. 2933. 2913. 2926. 3.43e9    2926.    NA    NA    NA
##  7 2019-04-26 2926. 2940. 2918. 2940. 3.25e9    2940.    NA    NA    NA
##  8 2019-04-29 2941. 2950. 2939. 2943. 3.12e9    2943.    NA    NA    NA
##  9 2019-04-30 2937. 2948. 2924. 2946. 3.92e9    2946.    NA    NA    NA
## 10 2019-05-01 2952. 2954. 2923. 2924. 3.65e9    2924.    NA    NA    NA
## # … with 117 more rows, and 3 more variables: sd2down <dbl>, SMA..1 <dbl>,
## #   SD <dbl>

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 Calculate the Bollinger Bands. Save the result under stock and print it.

stockw <-
  stock %>%
  mutate(sd2up = SMA + 2 * SD,
         sd2down = SMA - 2 * SD)

stockw
## # A tibble: 127 x 11
##    date        open  high   low close volume adjusted   SMA    SD sd2up
##    <date>     <dbl> <dbl> <dbl> <dbl>  <dbl>    <dbl> <dbl> <dbl> <dbl>
##  1 2019-04-17 2916. 2918  2895. 2900. 3.60e9    2900.    NA    NA    NA
##  2 2019-04-18 2905. 2908. 2892. 2905. 3.51e9    2905.    NA    NA    NA
##  3 2019-04-22 2899. 2910. 2896. 2908. 3.00e9    2908.    NA    NA    NA
##  4 2019-04-23 2910. 2936. 2909. 2934. 3.64e9    2934.    NA    NA    NA
##  5 2019-04-24 2934  2937. 2926. 2927. 3.45e9    2927.    NA    NA    NA
##  6 2019-04-25 2929. 2933. 2913. 2926. 3.43e9    2926.    NA    NA    NA
##  7 2019-04-26 2926. 2940. 2918. 2940. 3.25e9    2940.    NA    NA    NA
##  8 2019-04-29 2941. 2950. 2939. 2943. 3.12e9    2943.    NA    NA    NA
##  9 2019-04-30 2937. 2948. 2924. 2946. 3.92e9    2946.    NA    NA    NA
## 10 2019-05-01 2952. 2954. 2923. 2924. 3.65e9    2924.    NA    NA    NA
## # … with 117 more rows, and 1 more variable: sd2down <dbl>

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

Q4 Keep variables to build the Bollinger Bands. Save the result under stock_selected and print it.

stock_selected <-
  stockw %>%
  select(date, close, SMA, sd2up, sd2down)

stock_selected
## # A tibble: 127 x 5
##    date       close   SMA sd2up sd2down
##    <date>     <dbl> <dbl> <dbl>   <dbl>
##  1 2019-04-17 2900.    NA    NA      NA
##  2 2019-04-18 2905.    NA    NA      NA
##  3 2019-04-22 2908.    NA    NA      NA
##  4 2019-04-23 2934.    NA    NA      NA
##  5 2019-04-24 2927.    NA    NA      NA
##  6 2019-04-25 2926.    NA    NA      NA
##  7 2019-04-26 2940.    NA    NA      NA
##  8 2019-04-29 2943.    NA    NA      NA
##  9 2019-04-30 2946.    NA    NA      NA
## 10 2019-05-01 2924.    NA    NA      NA
## # … with 117 more rows

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

stock_long <-
  stock_selected %>%
  gather(key = type, value = price, close:sd2down)

stock_long
## # A tibble: 508 x 3
##    date       type  price
##    <date>     <chr> <dbl>
##  1 2019-04-17 close 2900.
##  2 2019-04-18 close 2905.
##  3 2019-04-22 close 2908.
##  4 2019-04-23 close 2934.
##  5 2019-04-24 close 2927.
##  6 2019-04-25 close 2926.
##  7 2019-04-26 close 2940.
##  8 2019-04-29 close 2943.
##  9 2019-04-30 close 2946.
## 10 2019-05-01 close 2924.
## # … with 498 more rows

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

Q6 Visualize data.

stock_long %>%
  ggplot(aes(x = date, y = price, color = type)) +
  geom_line() + 
  theme(legend.position="bottom") +
  labs(title = "Bollinger Bands",
       x = NULL,
       y = "Stock Prices")

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?

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.