# 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 = close)) +
geom_line() + # Plot stock price
geom_bbands(aes(high = high, low = low, close = close), ma_fun = SMA, n = 50) +
coord_x_date(xlim = c(as_date("2016-12-31") - dyears(1), as_date("2016-12-31")),
ylim = c(75, 125))
# EMA
AAPL %>%
ggplot(aes(x = date, y = close)) +
geom_line() + # Plot stock price
geom_bbands(aes(high = high, low = low, close = close),
ma_fun = EMA, wilder = TRUE, ratio = NULL, n = 50) +
coord_x_date(xlim = c(as_date("2016-12-31") - dyears(1), as_date("2016-12-31")),
ylim = c(75, 125))
# VWMA
AAPL %>%
ggplot(aes(x = date, y = close)) +
geom_line() + # Plot stock price
geom_bbands(aes(high = high, low = low, close = close, volume = volume),
ma_fun = VWMA, n = 50) +
coord_x_date(xlim = c(as_date("2016-12-31") - dyears(1), as_date("2016-12-31")),
ylim = c(75, 125))
# }
Run the code above in your browser using DataLab