The code for the moving average analysis below is taken from: https://www.r-bloggers.com/tidyquant-bringing-quantitative-financial-analysis-to-the-tidyverse/.

library(tidyquant)
library(tidyverse)

# Conduct the trend analysis (moving average) for the Apple stock. 

# Import data
from = today() - years(1)
stock <- tq_get("AMZN", get = "stock.prices", from = from)
stock
## # A tibble: 251 x 8
##    symbol date        open  high   low close  volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>
##  1 AMZN   2019-04-12 1848. 1852. 1841. 1843. 3114400    1843.
##  2 AMZN   2019-04-15 1842  1847. 1819. 1845. 3724400    1845.
##  3 AMZN   2019-04-16 1851. 1870. 1848  1863. 3044600    1863.
##  4 AMZN   2019-04-17 1873. 1876. 1860. 1865. 2893500    1865.
##  5 AMZN   2019-04-18 1869. 1871. 1859. 1862. 2749900    1862.
##  6 AMZN   2019-04-22 1855. 1888. 1846. 1887. 3373800    1887.
##  7 AMZN   2019-04-23 1891. 1929. 1890. 1924. 4640400    1924.
##  8 AMZN   2019-04-24 1925  1930. 1898. 1902. 3675800    1902.
##  9 AMZN   2019-04-25 1917  1922. 1900. 1902. 6099100    1902.
## 10 AMZN   2019-04-26 1929  1951  1898  1951. 8432600    1951.
## # … with 241 more rows
# Calculate 50-day and 100-day simple moving average 
stock <-
  stock %>%
  # Calculate 50-day simple moving average
  tq_mutate(select = close, mutate_fun = SMA, n = 15) %>%
  # Rename 50-day SMA to SMA.short
  rename(SMA.short = SMA) %>%
  tq_mutate(select = close, mutate_fun = SMA, n = 50) %>%
  rename(SMA.long = SMA)

stock
## # A tibble: 251 x 10
##    symbol date        open  high   low close  volume adjusted SMA.short SMA.long
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>     <dbl>    <dbl>
##  1 AMZN   2019-04-12 1848. 1852. 1841. 1843. 3114400    1843.        NA       NA
##  2 AMZN   2019-04-15 1842  1847. 1819. 1845. 3724400    1845.        NA       NA
##  3 AMZN   2019-04-16 1851. 1870. 1848  1863. 3044600    1863.        NA       NA
##  4 AMZN   2019-04-17 1873. 1876. 1860. 1865. 2893500    1865.        NA       NA
##  5 AMZN   2019-04-18 1869. 1871. 1859. 1862. 2749900    1862.        NA       NA
##  6 AMZN   2019-04-22 1855. 1888. 1846. 1887. 3373800    1887.        NA       NA
##  7 AMZN   2019-04-23 1891. 1929. 1890. 1924. 4640400    1924.        NA       NA
##  8 AMZN   2019-04-24 1925  1930. 1898. 1902. 3675800    1902.        NA       NA
##  9 AMZN   2019-04-25 1917  1922. 1900. 1902. 6099100    1902.        NA       NA
## 10 AMZN   2019-04-26 1929  1951  1898  1951. 8432600    1951.        NA       NA
## # … with 241 more rows
# Transform to long form to wide form for graphing
stock_long <-
  stock %>%
  select(date, close, SMA.short, SMA.long) %>%
  # Transform to long form 
  gather(key = "type", value = "price", close:SMA.long)

stock_long
## # A tibble: 753 x 3
##    date       type  price
##    <date>     <chr> <dbl>
##  1 2019-04-12 close 1843.
##  2 2019-04-15 close 1845.
##  3 2019-04-16 close 1863.
##  4 2019-04-17 close 1865.
##  5 2019-04-18 close 1862.
##  6 2019-04-22 close 1887.
##  7 2019-04-23 close 1924.
##  8 2019-04-24 close 1902.
##  9 2019-04-25 close 1902.
## 10 2019-04-26 close 1951.
## # … with 743 more rows
# Visualize
stock_long %>%
  ggplot(aes(x = date, y = price, col = type)) +
  geom_line() + 
  theme(legend.position="bottom") +
  labs(title = "Simple Moving Averages are a Breeze with tidyquant",
       x = NULL,
       y = "Stock Prices")

Q1 Import S&P500 for the last one year.

Hint: Copy and revise the importing part of the code from above.

library(tidyquant)
library(tidyverse)

# Conduct the trend analysis (moving average) for the Apple stock. 

# Import data
from = today() - years(1)
stock <- tq_get("^GSPC", get = "stock.prices", from = from)
stock
## # A tibble: 251 x 8
##    symbol date        open  high   low close     volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>      <dbl>    <dbl>
##  1 ^GSPC  2019-04-12 2901. 2911. 2898. 2907. 3688490000    2907.
##  2 ^GSPC  2019-04-15 2908. 2910. 2896. 2906. 3088330000    2906.
##  3 ^GSPC  2019-04-16 2912. 2916. 2901. 2907. 3402210000    2907.
##  4 ^GSPC  2019-04-17 2916. 2918  2895. 2900. 3602300000    2900.
##  5 ^GSPC  2019-04-18 2905. 2908. 2892. 2905. 3506850000    2905.
##  6 ^GSPC  2019-04-22 2899. 2910. 2896. 2908. 2997950000    2908.
##  7 ^GSPC  2019-04-23 2910. 2936. 2909. 2934. 3635030000    2934.
##  8 ^GSPC  2019-04-24 2934  2937. 2926. 2927. 3448960000    2927.
##  9 ^GSPC  2019-04-25 2929. 2933. 2913. 2926. 3425280000    2926.
## 10 ^GSPC  2019-04-26 2926. 2940. 2918. 2940. 3248500000    2940.
## # … with 241 more rows

Q2 Calculate 15-day and 50-day simple moving averages.

Hint: Copy and revise the moving average part of the code from above.

# Calculate 50-day and 100-day simple moving average 
stock <-
  stock %>%
  # Calculate 50-day simple moving average
  tq_mutate(select = close, mutate_fun = SMA, n = 15) %>%
  # Rename 50-day SMA to SMA.short
  rename(SMA.short = SMA) %>%
  tq_mutate(select = close, mutate_fun = SMA, n = 50) %>%
  rename(SMA.long = SMA)

stock
## # A tibble: 251 x 10
##    symbol date        open  high   low close  volume adjusted SMA.short SMA.long
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>     <dbl>    <dbl>
##  1 ^GSPC  2019-04-12 2901. 2911. 2898. 2907.  3.69e9    2907.        NA       NA
##  2 ^GSPC  2019-04-15 2908. 2910. 2896. 2906.  3.09e9    2906.        NA       NA
##  3 ^GSPC  2019-04-16 2912. 2916. 2901. 2907.  3.40e9    2907.        NA       NA
##  4 ^GSPC  2019-04-17 2916. 2918  2895. 2900.  3.60e9    2900.        NA       NA
##  5 ^GSPC  2019-04-18 2905. 2908. 2892. 2905.  3.51e9    2905.        NA       NA
##  6 ^GSPC  2019-04-22 2899. 2910. 2896. 2908.  3.00e9    2908.        NA       NA
##  7 ^GSPC  2019-04-23 2910. 2936. 2909. 2934.  3.64e9    2934.        NA       NA
##  8 ^GSPC  2019-04-24 2934  2937. 2926. 2927.  3.45e9    2927.        NA       NA
##  9 ^GSPC  2019-04-25 2929. 2933. 2913. 2926.  3.43e9    2926.        NA       NA
## 10 ^GSPC  2019-04-26 2926. 2940. 2918. 2940.  3.25e9    2940.        NA       NA
## # … with 241 more rows

Q3 Transform data to long form from wide form for graphing.

Hint: Copy and revise the transformation part of the code from above.

# Transform to long form to wide form for graphing
stock_long <-
  stock %>%
  select(date, close, SMA.short, SMA.long) %>%
  # Transform to long form 
  gather(key = "type", value = "price", close:SMA.long)

stock_long
## # A tibble: 753 x 3
##    date       type  price
##    <date>     <chr> <dbl>
##  1 2019-04-12 close 2907.
##  2 2019-04-15 close 2906.
##  3 2019-04-16 close 2907.
##  4 2019-04-17 close 2900.
##  5 2019-04-18 close 2905.
##  6 2019-04-22 close 2908.
##  7 2019-04-23 close 2934.
##  8 2019-04-24 close 2927.
##  9 2019-04-25 close 2926.
## 10 2019-04-26 close 2940.
## # … with 743 more rows

Q4 Visualize data.

Hint: Copy and revise the visualization part of the code from above.

# Visualize
stock_long %>%
  ggplot(aes(x = date, y = price, col = type)) +
  geom_line() + 
  theme(legend.position="bottom") +
  labs(title = "Simple Moving Averages are a Breeze with tidyquant",
       x = NULL,
       y = "Stock Prices")

Q5 List all bullish or bearish crossovers with dates and closing prices.

Q6 If you had invested $1 million on the day of the first bullish crossover and sold your shares on the following bearish crosover, how much would you have won or lost?

Q7 Try 50-day and 100-day simple moving average instead. Does your answer in Q6 change?

library(tidyquant)
library(tidyverse)

# Conduct the trend analysis (moving average) for the Apple stock. 

# Import data
from = today() - years(1)
stock <- tq_get("^GSPC", get = "stock.prices", from = from)
stock
## # A tibble: 251 x 8
##    symbol date        open  high   low close     volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>      <dbl>    <dbl>
##  1 ^GSPC  2019-04-12 2901. 2911. 2898. 2907. 3688490000    2907.
##  2 ^GSPC  2019-04-15 2908. 2910. 2896. 2906. 3088330000    2906.
##  3 ^GSPC  2019-04-16 2912. 2916. 2901. 2907. 3402210000    2907.
##  4 ^GSPC  2019-04-17 2916. 2918  2895. 2900. 3602300000    2900.
##  5 ^GSPC  2019-04-18 2905. 2908. 2892. 2905. 3506850000    2905.
##  6 ^GSPC  2019-04-22 2899. 2910. 2896. 2908. 2997950000    2908.
##  7 ^GSPC  2019-04-23 2910. 2936. 2909. 2934. 3635030000    2934.
##  8 ^GSPC  2019-04-24 2934  2937. 2926. 2927. 3448960000    2927.
##  9 ^GSPC  2019-04-25 2929. 2933. 2913. 2926. 3425280000    2926.
## 10 ^GSPC  2019-04-26 2926. 2940. 2918. 2940. 3248500000    2940.
## # … with 241 more rows
# Calculate 50-day and 100-day simple moving average 
stock <-
  stock %>%
  # Calculate 50-day simple moving average
  tq_mutate(select = close, mutate_fun = SMA, n = 50) %>%
  # Rename 50-day SMA to SMA.short
  rename(SMA.short = SMA) %>%
  tq_mutate(select = close, mutate_fun = SMA, n = 100) %>%
  rename(SMA.long = SMA)

stock
## # A tibble: 251 x 10
##    symbol date        open  high   low close  volume adjusted SMA.short SMA.long
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>     <dbl>    <dbl>
##  1 ^GSPC  2019-04-12 2901. 2911. 2898. 2907.  3.69e9    2907.        NA       NA
##  2 ^GSPC  2019-04-15 2908. 2910. 2896. 2906.  3.09e9    2906.        NA       NA
##  3 ^GSPC  2019-04-16 2912. 2916. 2901. 2907.  3.40e9    2907.        NA       NA
##  4 ^GSPC  2019-04-17 2916. 2918  2895. 2900.  3.60e9    2900.        NA       NA
##  5 ^GSPC  2019-04-18 2905. 2908. 2892. 2905.  3.51e9    2905.        NA       NA
##  6 ^GSPC  2019-04-22 2899. 2910. 2896. 2908.  3.00e9    2908.        NA       NA
##  7 ^GSPC  2019-04-23 2910. 2936. 2909. 2934.  3.64e9    2934.        NA       NA
##  8 ^GSPC  2019-04-24 2934  2937. 2926. 2927.  3.45e9    2927.        NA       NA
##  9 ^GSPC  2019-04-25 2929. 2933. 2913. 2926.  3.43e9    2926.        NA       NA
## 10 ^GSPC  2019-04-26 2926. 2940. 2918. 2940.  3.25e9    2940.        NA       NA
## # … with 241 more rows
# Transform to long form to wide form for graphing
stock_long <-
  stock %>%
  select(date, close, SMA.short, SMA.long) %>%
  # Transform to long form 
  gather(key = "type", value = "price", close:SMA.long)

stock_long
## # A tibble: 753 x 3
##    date       type  price
##    <date>     <chr> <dbl>
##  1 2019-04-12 close 2907.
##  2 2019-04-15 close 2906.
##  3 2019-04-16 close 2907.
##  4 2019-04-17 close 2900.
##  5 2019-04-18 close 2905.
##  6 2019-04-22 close 2908.
##  7 2019-04-23 close 2934.
##  8 2019-04-24 close 2927.
##  9 2019-04-25 close 2926.
## 10 2019-04-26 close 2940.
## # … with 743 more rows
# Visualize
stock_long %>%
  ggplot(aes(x = date, y = price, col = type)) +
  geom_line() + 
  theme(legend.position="bottom") +
  labs(title = "Simple Moving Averages are a Breeze with tidyquant",
       x = NULL,
       y = "Stock Prices")

Q8 Hide the messages and the code, but display results of the code from 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.