# NOT RUN {
# Load libraries
library(tidyquant)
library(dplyr)
library(ggplot2)
AAPL <- tq_get("AAPL", from = "2013-01-01", to = "2016-12-31")
# SMA
AAPL %>%
ggplot(aes(x = date, y = adjusted)) +
geom_line() + # Plot stock price
geom_ma(ma_fun = SMA, n = 50) + # Plot 50-day SMA
geom_ma(ma_fun = SMA, n = 200, color = "red") + # Plot 200-day SMA
coord_x_date(xlim = c("2016-01-01", "2016-12-31"),
ylim = c(75, 125)) # Zoom in
# EVWMA
AAPL %>%
ggplot(aes(x = date, y = adjusted)) +
geom_line() + # Plot stock price
geom_ma(aes(volume = volume), ma_fun = EVWMA, n = 50) + # Plot 50-day EVWMA
coord_x_date(xlim = c("2016-01-01", "2016-12-31"),
ylim = c(75, 125)) # Zoom in
# }
Run the code above in your browser using DataLab