Conduct the trend analysis for the Netflix stock.
Revise the from code below. No need to type answer. Just revise the code.
# Load tidyquant, tidyverse, lubridate, xts, quantmod, TTR
library(tidyquant)
from = today() - years(5)
NFLX <- tq_get("NFLX", get = "stock.prices", from = from)
NFLX
## # A tibble: 1,259 x 7
## date open high low close volume adjusted
## <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2013-10-30 47.0 47.0 45.1 45.4 21640500 45.4
## 2 2013-10-31 45.4 46.7 45.1 46.1 18489800 46.1
## 3 2013-11-01 47.0 47.6 46.6 47.0 22020600 47.0
## 4 2013-11-04 47.3 48.3 46.5 48.2 21919100 48.2
## 5 2013-11-05 47.9 48.9 47.6 48.8 17289300 48.8
## 6 2013-11-06 48.5 49.2 47.5 47.9 20733300 47.9
## 7 2013-11-07 48.4 48.5 46.5 46.7 20007400 46.7
## 8 2013-11-08 46.7 47.9 46.5 47.8 19448800 47.8
## 9 2013-11-11 47.5 48.5 46.9 48.3 16815400 48.3
## 10 2013-11-12 48.0 48.5 47.3 47.7 12231800 47.7
## # ... with 1,249 more rows
# Add the 100-day and 200-day simple moving average by passing close prices
NFLX <-
NFLX %>%
tq_mutate(select = close, mutate_fun = SMA, n = 100) %>%
rename(SMA.100 = SMA) %>%
tq_mutate(select = close, mutate_fun = SMA, n = 200) %>%
rename(SMA.200 = SMA)
NFLX
## # A tibble: 1,259 x 9
## date open high low close volume adjusted SMA.100 SMA.200
## <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2013-10-30 47.0 47.0 45.1 45.4 21640500 45.4 NA NA
## 2 2013-10-31 45.4 46.7 45.1 46.1 18489800 46.1 NA NA
## 3 2013-11-01 47.0 47.6 46.6 47.0 22020600 47.0 NA NA
## 4 2013-11-04 47.3 48.3 46.5 48.2 21919100 48.2 NA NA
## 5 2013-11-05 47.9 48.9 47.6 48.8 17289300 48.8 NA NA
## 6 2013-11-06 48.5 49.2 47.5 47.9 20733300 47.9 NA NA
## 7 2013-11-07 48.4 48.5 46.5 46.7 20007400 46.7 NA NA
## 8 2013-11-08 46.7 47.9 46.5 47.8 19448800 47.8 NA NA
## 9 2013-11-11 47.5 48.5 46.9 48.3 16815400 48.3 NA NA
## 10 2013-11-12 48.0 48.5 47.3 47.7 12231800 47.7 NA NA
## # ... with 1,249 more rows
# Transform to long form to wide form for graphing
NFLX %>%
select(date, close, SMA.100, SMA.200)
## # A tibble: 1,259 x 4
## date close SMA.100 SMA.200
## <date> <dbl> <dbl> <dbl>
## 1 2013-10-30 45.4 NA NA
## 2 2013-10-31 46.1 NA NA
## 3 2013-11-01 47.0 NA NA
## 4 2013-11-04 48.2 NA NA
## 5 2013-11-05 48.8 NA NA
## 6 2013-11-06 47.9 NA NA
## 7 2013-11-07 46.7 NA NA
## 8 2013-11-08 47.8 NA NA
## 9 2013-11-11 48.3 NA NA
## 10 2013-11-12 47.7 NA NA
## # ... with 1,249 more rows
NFLX %>%
select(date, close, SMA.100, SMA.200) %>%
gather(key = type, value = price, close:SMA.200)
## # A tibble: 3,777 x 3
## date type price
## <date> <chr> <dbl>
## 1 2013-10-30 close 45.4
## 2 2013-10-31 close 46.1
## 3 2013-11-01 close 47.0
## 4 2013-11-04 close 48.2
## 5 2013-11-05 close 48.8
## 6 2013-11-06 close 47.9
## 7 2013-11-07 close 46.7
## 8 2013-11-08 close 47.8
## 9 2013-11-11 close 48.3
## 10 2013-11-12 close 47.7
## # ... with 3,767 more rows
No need to type answer. Just revise the code.
It’s the first day after the crossover when the shorter-term moving average crosses over the longer-term moving average.
Buy 100 shares at the first bullish crossover; sell all 100 shares at the bearish crossover; buy at the second bullish crossover with all the sales proceeds from the previous transaction; and sell all shares today.