# Loads tidyquant, tidyverse, lubridate, xts, quantmod, TTR
library(tidyquant)
# Import stock prices
WMT <- tq_get("WMT", get = "stock.prices", from = "2018-01-01", to =today())
WMT
## # A tibble: 221 x 7
## date open high low close volume adjusted
## <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2018-01-02 99.3 99.8 98.5 98.6 10150500 96.8
## 2 2018-01-03 98.8 100 98.8 99.4 8903600 97.7
## 3 2018-01-04 99.5 99.8 98.9 99.5 6830400 97.8
## 4 2018-01-05 99.6 100. 99.6 100. 7284000 98.3
## 5 2018-01-08 100. 102. 100. 102. 8843900 99.8
## 6 2018-01-09 102. 102. 100. 100. 7312700 98.6
## 7 2018-01-10 99.8 99.9 98.5 99.7 7930400 97.9
## 8 2018-01-11 99.7 100. 98.8 100. 6537700 98.2
## 9 2018-01-12 100. 101. 100. 101. 6951200 99.1
## 10 2018-01-16 101. 102. 100. 101. 7170700 98.9
## # ... with 211 more rows
# Add moving averages
WMT_MA <-
WMT %>%
tq_mutate(select = close, mutate_fun = SMA, n = 50) %>%
rename(SMA.50 = SMA) %>%
tq_mutate(select = close, mutate_fun = SMA, n = 200) %>%
rename(SMA.200 = SMA)
WMT_MA %>% View
# Transform to long form to wide form for graphing
WMT_MA %>%
select(date, close, SMA.50, SMA.200) %>%
gather(key = type, value = price, close:SMA.200)
## # A tibble: 663 x 3
## date type price
## <date> <chr> <dbl>
## 1 2018-01-02 close 98.6
## 2 2018-01-03 close 99.4
## 3 2018-01-04 close 99.5
## 4 2018-01-05 close 100.
## 5 2018-01-08 close 102.
## 6 2018-01-09 close 100.
## 7 2018-01-10 close 99.7
## 8 2018-01-11 close 100.
## 9 2018-01-12 close 101.
## 10 2018-01-16 close 101.
## # ... with 653 more rows
# Visualize Moving Average Crossover
WMT_MA %>%
select(date, close, SMA.50, SMA.200) %>%
gather(key = type, value = price, close:SMA.200) %>%
filter(date >= "2018-01-01") %>%
ggplot(aes(x = date, y = price, col = type)) +
geom_line() +
theme(legend.position="bottom") +
labs(x = NULL,
y = "Stock Prices")
# add moving averages and standard deviation
WMT_BB <- WMT %>%
tq_mutate(select = close, mutate_fun = SMA, n = 20) %>%
tq_mutate(select = close, mutate_fun = runSD, n = 20) %>%
rename(SD = value)
WMT_BB
## # A tibble: 221 x 9
## date open high low close volume adjusted SMA SD
## <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2018-01-02 99.3 99.8 98.5 98.6 10150500 96.8 NA NA
## 2 2018-01-03 98.8 100 98.8 99.4 8903600 97.7 NA NA
## 3 2018-01-04 99.5 99.8 98.9 99.5 6830400 97.8 NA NA
## 4 2018-01-05 99.6 100. 99.6 100. 7284000 98.3 NA NA
## 5 2018-01-08 100. 102. 100. 102. 8843900 99.8 NA NA
## 6 2018-01-09 102. 102. 100. 100. 7312700 98.6 NA NA
## 7 2018-01-10 99.8 99.9 98.5 99.7 7930400 97.9 NA NA
## 8 2018-01-11 99.7 100. 98.8 100. 6537700 98.2 NA NA
## 9 2018-01-12 100. 101. 100. 101. 6951200 99.1 NA NA
## 10 2018-01-16 101. 102. 100. 101. 7170700 98.9 NA NA
## # ... with 211 more rows
# Calculate the Bollinger Bands
WMT_BB <-
WMT_BB %>%
mutate(sd3up = SMA + 3 * SD,
sd3down = SMA - 3 * SD)
WMT_BB %>% View
# Transform to long form from wide form for graphing
WMT_BB %>%
select(date, close, SMA, sd3up, sd3down) %>%
gather(key = type, value = price, close:sd3down)
## # A tibble: 884 x 3
## date type price
## <date> <chr> <dbl>
## 1 2018-01-02 close 98.6
## 2 2018-01-03 close 99.4
## 3 2018-01-04 close 99.5
## 4 2018-01-05 close 100.
## 5 2018-01-08 close 102.
## 6 2018-01-09 close 100.
## 7 2018-01-10 close 99.7
## 8 2018-01-11 close 100.
## 9 2018-01-12 close 101.
## 10 2018-01-16 close 101.
## # ... with 874 more rows
WMT_BB %>%
select(date, close, SMA, sd3up, sd3down) %>%
gather(key = type, value = price, close:sd3down) %>%
filter(date >= "2018-01-01") %>%
ggplot(aes(x = date, y = price, col = type)) +
geom_line() +
theme(legend.position="bottom") +
labs(x = NULL,
y = "Stock Prices")
How to interpret moving average crossover
How to interpret bollinger bands
Q1 Moving AVerage Crossover: When does the first crossover occur? Is the first crossover bearish or bullish?
Q2 Bollinger Bands: When does it give the first signal? Is it bearish or bullish?
WMT_BB %>%
filter(close > sd3up, date>="2018-01-01") %>%
select(date, close, sd3up)
## # A tibble: 1 x 3
## date close sd3up
## <date> <dbl> <dbl>
## 1 2018-08-16 98.6 96.5
Q3 How effective is the BB? Use a couple of signals as examples.
Q4 Increase the bands to 3 times standard deviation, instead of 2 times. Does it make the BB more effective in signaling buying or selling opportunities?
Q5 Do the two measures give us consistent signals?
Q6 Which of the two appears to be more appropriate for short-term?
Q7 Try Walmart instead of Apple. Does it change your answers above.
Q8 Try different dates: “2017-01-01” - “2018-01-01”. Does it change your answers above.