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-22 220. 223. 219. 221. 28792100 217.
## 2 2018-10-23 216. 223. 215. 223. 38767800 219.
## 3 2018-10-24 223. 224. 215. 215. 40925500 212.
## 4 2018-10-25 218. 221. 217. 220. 29855800 216.
## 5 2018-10-26 216. 220. 213. 216. 47258400 213.
## 6 2018-10-29 219. 220. 206. 212. 45935500 209.
## 7 2018-10-30 211. 215. 209. 213. 36660000 210.
## 8 2018-10-31 217. 220. 217. 219. 38358900 216.
## 9 2018-11-01 219. 222. 217. 222. 58323200 219.
## 10 2018-11-02 210. 214. 205. 207. 91328700 204.
## # … 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-22 220. 223. 219. 221. 28792100 217. NA NA
## 2 2018-10-23 216. 223. 215. 223. 38767800 219. NA NA
## 3 2018-10-24 223. 224. 215. 215. 40925500 212. NA NA
## 4 2018-10-25 218. 221. 217. 220. 29855800 216. NA NA
## 5 2018-10-26 216. 220. 213. 216. 47258400 213. NA NA
## 6 2018-10-29 219. 220. 206. 212. 45935500 209. NA NA
## 7 2018-10-30 211. 215. 209. 213. 36660000 210. NA NA
## 8 2018-10-31 217. 220. 217. 219. 38358900 216. NA NA
## 9 2018-11-01 219. 222. 217. 222. 58323200 219. NA NA
## 10 2018-11-02 210. 214. 205. 207. 91328700 204. NA NA
## # … with 241 more rows
## # A tibble: 753 x 3
## date type price
## <date> <chr> <dbl>
## 1 2018-10-22 close 221.
## 2 2018-10-23 close 223.
## 3 2018-10-24 close 215.
## 4 2018-10-25 close 220.
## 5 2018-10-26 close 216.
## 6 2018-10-29 close 212.
## 7 2018-10-30 close 213.
## 8 2018-10-31 close 219.
## 9 2018-11-01 close 222.
## 10 2018-11-02 close 207.
## # … with 743 more rows
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-22 2774. 2779. 2749. 2756. 3307140000 2756.
## 2 2018-10-23 2721. 2754. 2691. 2741. 4348580000 2741.
## 3 2018-10-24 2738. 2743. 2652. 2656. 4709310000 2656.
## 4 2018-10-25 2675. 2723. 2668. 2706. 4634770000 2706.
## 5 2018-10-26 2668. 2692. 2628. 2659. 4803150000 2659.
## 6 2018-10-29 2683. 2707. 2604. 2641. 4673700000 2641.
## 7 2018-10-30 2641. 2685. 2635. 2683. 5106380000 2683.
## 8 2018-10-31 2706. 2737. 2706. 2712. 5112420000 2712.
## 9 2018-11-01 2718. 2742. 2709. 2740. 4708420000 2740.
## 10 2018-11-02 2745. 2757. 2700. 2723. 4237930000 2723.
## # … 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.
## Q3 Transform data to long form from wide form for graphing.
Hint: Copy and revise the transformation part of the code from above.
## Q4 Visualize data.
Hint: Copy and revise the visualization part of the code from above.
<img src="Quiz4-a_files/figure-html/unnamed-chunk-8-1.png" width="672" />
## Q5 List all bullish or bearish crossovers with dates and closing prices.
* A ***bullish*** crossover if the 100-day moving average cross ***above*** the 200-day moving average.
* A ***bearish*** crossover if the 100-day moving average cross ***below*** the 200-day moving average.
## 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?
```
Hint: Use message, echo and results in the chunk options. Refer to the RMarkdown Reference Guide.