Import stock prices

# 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

Moving Average Crossover

# 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")

Bollinger Bands

# 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?

There is no crossover in the period display.

Q2 Bollinger Bands: When does it give the first signal? Is it bearish or bullish?

2011-01-05 is the day it aproches the upper band. The close for thet day was 47.71429 and the upper band was 47.76286. There is no cross over

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.

the daily close will go up hit the upperband then fall untill it hits the lower band.

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?

The bollinger bands at 3 sd. show an easyer buy sell point because of the extreams shown in the graph.

Q5 Do the two measures give us consistent signals?

The wor measures give us vey different signals for the stock in question. The average croos over is usless for this evaluation.

Q6 Which of the two appears to be more appropriate for short-term?

Bollinger bands are a better short term tool for evaluating.

Q7 Try Walmart instead of Apple. Does it change your answers above.

Yes durring there is a bullish cross over.

Q8 Try different dates: “2017-01-01” - “2018-01-01”. Does it change your answers above.

we can not use the cross over graph when we adjust the start date to the begining of 2018