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

## # A tibble: 251 x 7
##    date        open  high   low close   volume adjusted
##    <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 2018-10-15  221.  222.  217.  217. 30791000     214.
##  2 2018-10-16  219.  223.  217.  222. 29184000     219.
##  3 2018-10-17  222.  223.  219.  221. 22885400     218.
##  4 2018-10-18  218.  220.  213   216. 32581300     213.
##  5 2018-10-19  218.  221.  217.  219. 33078700     216.
##  6 2018-10-22  220.  223.  219.  221. 28792100     217.
##  7 2018-10-23  216.  223.  215.  223. 38767800     219.
##  8 2018-10-24  223.  224.  215.  215. 40925500     212.
##  9 2018-10-25  218.  221.  217.  220. 29855800     216.
## 10 2018-10-26  216.  220.  213.  216. 47258400     213.
## # … with 241 more rows
## # A tibble: 251 x 9
##    date        open  high   low close   volume adjusted SMA.short SMA.long
##    <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>     <dbl>    <dbl>
##  1 2018-10-15  221.  222.  217.  217. 30791000     214.        NA       NA
##  2 2018-10-16  219.  223.  217.  222. 29184000     219.        NA       NA
##  3 2018-10-17  222.  223.  219.  221. 22885400     218.        NA       NA
##  4 2018-10-18  218.  220.  213   216. 32581300     213.        NA       NA
##  5 2018-10-19  218.  221.  217.  219. 33078700     216.        NA       NA
##  6 2018-10-22  220.  223.  219.  221. 28792100     217.        NA       NA
##  7 2018-10-23  216.  223.  215.  223. 38767800     219.        NA       NA
##  8 2018-10-24  223.  224.  215.  215. 40925500     212.        NA       NA
##  9 2018-10-25  218.  221.  217.  220. 29855800     216.        NA       NA
## 10 2018-10-26  216.  220.  213.  216. 47258400     213.        NA       NA
## # … with 241 more rows
## # A tibble: 753 x 3
##    date       type  price
##    <date>     <chr> <dbl>
##  1 2018-10-15 close  217.
##  2 2018-10-16 close  222.
##  3 2018-10-17 close  221.
##  4 2018-10-18 close  216.
##  5 2018-10-19 close  219.
##  6 2018-10-22 close  221.
##  7 2018-10-23 close  223.
##  8 2018-10-24 close  215.
##  9 2018-10-25 close  220.
## 10 2018-10-26 close  216.
## # … with 743 more rows

Q1 Import S&P500 for the last one year.

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

## # A tibble: 251 x 7
##    date        open  high   low close     volume adjusted
##    <date>     <dbl> <dbl> <dbl> <dbl>      <dbl>    <dbl>
##  1 2018-10-15 2764. 2776. 2749. 2751. 3300140000    2751.
##  2 2018-10-16 2767. 2813. 2767. 2810. 3428340000    2810.
##  3 2018-10-17 2812. 2817. 2782. 2809. 3321710000    2809.
##  4 2018-10-18 2802  2806. 2755. 2769. 3616440000    2769.
##  5 2018-10-19 2776. 2798. 2760. 2768. 3566490000    2768.
##  6 2018-10-22 2774. 2779. 2749. 2756. 3307140000    2756.
##  7 2018-10-23 2721. 2754. 2691. 2741. 4348580000    2741.
##  8 2018-10-24 2738. 2743. 2652. 2656. 4709310000    2656.
##  9 2018-10-25 2675. 2723. 2668. 2706. 4634770000    2706.
## 10 2018-10-26 2668. 2692. 2628. 2659. 4803150000    2659.
## # … 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.

## # A tibble: 251 x 9
##    date        open  high   low close    volume adjusted SMA.short SMA.long
##    <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>     <dbl>    <dbl>
##  1 2018-10-15 2764. 2776. 2749. 2751.    3.30e9    2751.        NA       NA
##  2 2018-10-16 2767. 2813. 2767. 2810.    3.43e9    2810.        NA       NA
##  3 2018-10-17 2812. 2817. 2782. 2809.    3.32e9    2809.        NA       NA
##  4 2018-10-18 2802  2806. 2755. 2769.    3.62e9    2769.        NA       NA
##  5 2018-10-19 2776. 2798. 2760. 2768.    3.57e9    2768.        NA       NA
##  6 2018-10-22 2774. 2779. 2749. 2756.    3.31e9    2756.        NA       NA
##  7 2018-10-23 2721. 2754. 2691. 2741.    4.35e9    2741.        NA       NA
##  8 2018-10-24 2738. 2743. 2652. 2656.    4.71e9    2656.        NA       NA
##  9 2018-10-25 2675. 2723. 2668. 2706.    4.63e9    2706.        NA       NA
## 10 2018-10-26 2668. 2692. 2628. 2659.    4.80e9    2659.        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.

## # A tibble: 753 x 3
##    date       type  price
##    <date>     <chr> <dbl>
##  1 2018-10-15 close 2751.
##  2 2018-10-16 close 2810.
##  3 2018-10-17 close 2809.
##  4 2018-10-18 close 2769.
##  5 2018-10-19 close 2768.
##  6 2018-10-22 close 2756.
##  7 2018-10-23 close 2741.
##  8 2018-10-24 close 2656.
##  9 2018-10-25 close 2706.
## 10 2018-10-26 close 2659.
## # … with 743 more rows

Q4 Visualize data.

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

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

There are 5 crossovers, first is a bullish crossover at Jan. 28 and a bearish crossover at May 23, then a bullish crossover at June 24 and a bearish crossover at Aug. 14, then a last bullish crossover at Sep. 16.

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?

You would have gained $66,806.72

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

## # A tibble: 251 x 7
##    date        open  high   low close     volume adjusted
##    <date>     <dbl> <dbl> <dbl> <dbl>      <dbl>    <dbl>
##  1 2018-10-15 2764. 2776. 2749. 2751. 3300140000    2751.
##  2 2018-10-16 2767. 2813. 2767. 2810. 3428340000    2810.
##  3 2018-10-17 2812. 2817. 2782. 2809. 3321710000    2809.
##  4 2018-10-18 2802  2806. 2755. 2769. 3616440000    2769.
##  5 2018-10-19 2776. 2798. 2760. 2768. 3566490000    2768.
##  6 2018-10-22 2774. 2779. 2749. 2756. 3307140000    2756.
##  7 2018-10-23 2721. 2754. 2691. 2741. 4348580000    2741.
##  8 2018-10-24 2738. 2743. 2652. 2656. 4709310000    2656.
##  9 2018-10-25 2675. 2723. 2668. 2706. 4634770000    2706.
## 10 2018-10-26 2668. 2692. 2628. 2659. 4803150000    2659.
## # … with 241 more rows
## # A tibble: 251 x 9
##    date        open  high   low close    volume adjusted SMA.short SMA.long
##    <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>     <dbl>    <dbl>
##  1 2018-10-15 2764. 2776. 2749. 2751.    3.30e9    2751.        NA       NA
##  2 2018-10-16 2767. 2813. 2767. 2810.    3.43e9    2810.        NA       NA
##  3 2018-10-17 2812. 2817. 2782. 2809.    3.32e9    2809.        NA       NA
##  4 2018-10-18 2802  2806. 2755. 2769.    3.62e9    2769.        NA       NA
##  5 2018-10-19 2776. 2798. 2760. 2768.    3.57e9    2768.        NA       NA
##  6 2018-10-22 2774. 2779. 2749. 2756.    3.31e9    2756.        NA       NA
##  7 2018-10-23 2721. 2754. 2691. 2741.    4.35e9    2741.        NA       NA
##  8 2018-10-24 2738. 2743. 2652. 2656.    4.71e9    2656.        NA       NA
##  9 2018-10-25 2675. 2723. 2668. 2706.    4.63e9    2706.        NA       NA
## 10 2018-10-26 2668. 2692. 2628. 2659.    4.80e9    2659.        NA       NA
## # … with 241 more rows
## # A tibble: 753 x 3
##    date       type  price
##    <date>     <chr> <dbl>
##  1 2018-10-15 close 2751.
##  2 2018-10-16 close 2810.
##  3 2018-10-17 close 2809.
##  4 2018-10-18 close 2769.
##  5 2018-10-19 close 2768.
##  6 2018-10-22 close 2756.
##  7 2018-10-23 close 2741.
##  8 2018-10-24 close 2656.
##  9 2018-10-25 close 2706.
## 10 2018-10-26 close 2659.
## # … with 743 more rows

There are no crossovers in this plot.

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.