Q1 Import S&P500 for the last five years.

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

library(tidyquant)
library(tidyverse)

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

# Import data
from = today() - years(5)
stock <- tq_get("^GSPC", get = "stock.prices", from = from)
stock
## # A tibble: 1,258 x 7
##    date        open  high   low close     volume adjusted
##    <date>     <dbl> <dbl> <dbl> <dbl>      <dbl>    <dbl>
##  1 2014-03-19 1872. 1874. 1850. 1861. 3289210000    1861.
##  2 2014-03-20 1860. 1873. 1855. 1872. 3327540000    1872.
##  3 2014-03-21 1875. 1884. 1863. 1867. 5270710000    1867.
##  4 2014-03-24 1868. 1873. 1850. 1857. 3409000000    1857.
##  5 2014-03-25 1859. 1872. 1856. 1866. 3200560000    1866.
##  6 2014-03-26 1867. 1876. 1853. 1853. 3480850000    1853.
##  7 2014-03-27 1852. 1856. 1842. 1849. 3733430000    1849.
##  8 2014-03-28 1850. 1867. 1850. 1858. 2955520000    1858.
##  9 2014-03-31 1859. 1875. 1859. 1872. 3274300000    1872.
## 10 2014-04-01 1874. 1886. 1874. 1886. 3336190000    1886.
## # ... with 1,248 more rows

Q2 Calculate 100-day and 200-day simple moving averages.

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

# Add the 100-day and 200-day simple moving average by passing close prices 
stock <-
  stock %>%
    tq_mutate(select = close, mutate_fun = SMA, n = 100) %>%
    rename(SMA.short = SMA) %>%
    tq_mutate(select = close, mutate_fun = SMA, n = 200) %>%
    rename(SMA.long = SMA)

stock
## # A tibble: 1,258 x 9
##    date        open  high   low close    volume adjusted SMA.short SMA.long
##    <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>     <dbl>    <dbl>
##  1 2014-03-19 1872. 1874. 1850. 1861.    3.29e9    1861.        NA       NA
##  2 2014-03-20 1860. 1873. 1855. 1872.    3.33e9    1872.        NA       NA
##  3 2014-03-21 1875. 1884. 1863. 1867.    5.27e9    1867.        NA       NA
##  4 2014-03-24 1868. 1873. 1850. 1857.    3.41e9    1857.        NA       NA
##  5 2014-03-25 1859. 1872. 1856. 1866.    3.20e9    1866.        NA       NA
##  6 2014-03-26 1867. 1876. 1853. 1853.    3.48e9    1853.        NA       NA
##  7 2014-03-27 1852. 1856. 1842. 1849.    3.73e9    1849.        NA       NA
##  8 2014-03-28 1850. 1867. 1850. 1858.    2.96e9    1858.        NA       NA
##  9 2014-03-31 1859. 1875. 1859. 1872.    3.27e9    1872.        NA       NA
## 10 2014-04-01 1874. 1886. 1874. 1886.    3.34e9    1886.        NA       NA
## # ... with 1,248 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) %>%
  gather(key = type, value = price, close:SMA.long)

stock_long
## # A tibble: 3,774 x 3
##    date       type  price
##    <date>     <chr> <dbl>
##  1 2014-03-19 close 1861.
##  2 2014-03-20 close 1872.
##  3 2014-03-21 close 1867.
##  4 2014-03-24 close 1857.
##  5 2014-03-25 close 1866.
##  6 2014-03-26 close 1853.
##  7 2014-03-27 close 1849.
##  8 2014-03-28 close 1858.
##  9 2014-03-31 close 1872.
## 10 2014-04-01 close 1886.
## # ... with 3,764 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 both bullish or bearish crossovers with dates and closing prices.

Bullish crossover happens on September 21st to 22nd in 2015, short = 2068.185 and long = 2068.048, then changes over to short = 2066.757 and long = 2067.403

Bearish crossover happens on June 9th to 10th in 2016, short = 2013.078 and long = 2013.423, then changes over to short = 2015.226 and long = 2014.565

Q6 If you had invested $1 million on the day of the first bullish crossover and followed the moving average strategy throughout the considered period, would you have more or less than $1 million today?

You would have a lot more than 1 million simply because the price of S&P has went from about 2100 at the time to now being over 2750 per share. More specifically you would have reinvested in mid 2016 which would have shown huge gains for the next 2 year until the next bullish crossover.

Q7 Repeat Q2- Q6 for 50-day and 100-day simple moving average.

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

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

# Add the 50-day and 100-day simple moving average by passing close prices 
stock <-
  stock %>%
    tq_mutate(select = close, mutate_fun = SMA, n = 50) %>%
    rename(SMA.50 = SMA) %>%
    tq_mutate(select = close, mutate_fun = SMA, n = 100) %>%
    rename(SMA.100 = SMA)

stock
## # A tibble: 1,258 x 11
##    date        open  high   low close volume adjusted SMA.short SMA.long
##    <date>     <dbl> <dbl> <dbl> <dbl>  <dbl>    <dbl>     <dbl>    <dbl>
##  1 2014-03-19 1872. 1874. 1850. 1861. 3.29e9    1861.        NA       NA
##  2 2014-03-20 1860. 1873. 1855. 1872. 3.33e9    1872.        NA       NA
##  3 2014-03-21 1875. 1884. 1863. 1867. 5.27e9    1867.        NA       NA
##  4 2014-03-24 1868. 1873. 1850. 1857. 3.41e9    1857.        NA       NA
##  5 2014-03-25 1859. 1872. 1856. 1866. 3.20e9    1866.        NA       NA
##  6 2014-03-26 1867. 1876. 1853. 1853. 3.48e9    1853.        NA       NA
##  7 2014-03-27 1852. 1856. 1842. 1849. 3.73e9    1849.        NA       NA
##  8 2014-03-28 1850. 1867. 1850. 1858. 2.96e9    1858.        NA       NA
##  9 2014-03-31 1859. 1875. 1859. 1872. 3.27e9    1872.        NA       NA
## 10 2014-04-01 1874. 1886. 1874. 1886. 3.34e9    1886.        NA       NA
## # ... with 1,248 more rows, and 2 more variables: SMA.50 <dbl>,
## #   SMA.100 <dbl>

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.50, SMA.100) %>%
  gather(key = type, value = price, close:SMA.100)

stock_long
## # A tibble: 3,774 x 3
##    date       type  price
##    <date>     <chr> <dbl>
##  1 2014-03-19 close 1861.
##  2 2014-03-20 close 1872.
##  3 2014-03-21 close 1867.
##  4 2014-03-24 close 1857.
##  5 2014-03-25 close 1866.
##  6 2014-03-26 close 1853.
##  7 2014-03-27 close 1849.
##  8 2014-03-28 close 1858.
##  9 2014-03-31 close 1872.
## 10 2014-04-01 close 1886.
## # ... with 3,764 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 both bullish or bearish crossovers with dates and closing prices.

Bullish crossover happens on August 5th and 6th in 2015, 50 = 2097.965 and 100 = 2097.582, then changes over to 50 = 2097.167 and 100 = 2097.606

Bearish crossover happens on December 6th and 7th in 2015, 50 = 2033.836 and 100 = 2035.227, then changes over to 50 = 2036.572 and 100 = 2035.026

Q6 If you had invested $1 million on the day of the first bullish crossover and followed the moving average strategy throughout the considered period, would you have more or less than $1 million today?

You would have a lot more than 1 million simply because the price of S&P has went from about 2100 at the time to now being over 2750 per share. More specifically you would have reinvested in throughout 2015, 2016, 2017, and 2018, which would have shown huge gains for the next 4 year until the next bullish crossover.