Conduct the trend analysis for the Netflix stock.

Q1 Import the data for the last five years.

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-25  47.3  48.2  46.6  46.9 24062500     46.9
##  2 2013-10-28  46.4  47.3  44.5  44.9 34260800     44.9
##  3 2013-10-29  45.5  46.8  44.2  46.8 30936500     46.8
##  4 2013-10-30  47.0  47.0  45.1  45.4 21640500     45.4
##  5 2013-10-31  45.4  46.7  45.1  46.1 18489800     46.1
##  6 2013-11-01  47.0  47.6  46.6  47.0 22020600     47.0
##  7 2013-11-04  47.3  48.3  46.5  48.2 21919100     48.2
##  8 2013-11-05  47.9  48.9  47.6  48.8 17289300     48.8
##  9 2013-11-06  48.5  49.2  47.5  47.9 20733300     47.9
## 10 2013-11-07  48.4  48.5  46.5  46.7 20007400     46.7
## # ... with 1,249 more rows

Q2 How many new columns does the R code chunk below add?

# 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-25  47.3  48.2  46.6  46.9 24062500     46.9      NA      NA
##  2 2013-10-28  46.4  47.3  44.5  44.9 34260800     44.9      NA      NA
##  3 2013-10-29  45.5  46.8  44.2  46.8 30936500     46.8      NA      NA
##  4 2013-10-30  47.0  47.0  45.1  45.4 21640500     45.4      NA      NA
##  5 2013-10-31  45.4  46.7  45.1  46.1 18489800     46.1      NA      NA
##  6 2013-11-01  47.0  47.6  46.6  47.0 22020600     47.0      NA      NA
##  7 2013-11-04  47.3  48.3  46.5  48.2 21919100     48.2      NA      NA
##  8 2013-11-05  47.9  48.9  47.6  48.8 17289300     48.8      NA      NA
##  9 2013-11-06  48.5  49.2  47.5  47.9 20733300     47.9      NA      NA
## 10 2013-11-07  48.4  48.5  46.5  46.7 20007400     46.7      NA      NA
## # ... with 1,249 more rows

The new code R code chunk has an addittional two columns for a total of nine.

Q3 What are the values of type variable?

# 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-25  46.9      NA      NA
##  2 2013-10-28  44.9      NA      NA
##  3 2013-10-29  46.8      NA      NA
##  4 2013-10-30  45.4      NA      NA
##  5 2013-10-31  46.1      NA      NA
##  6 2013-11-01  47.0      NA      NA
##  7 2013-11-04  48.2      NA      NA
##  8 2013-11-05  48.8      NA      NA
##  9 2013-11-06  47.9      NA      NA
## 10 2013-11-07  46.7      NA      NA
## # ... with 1,249 more rows

NFLX %>%
  select(date, close, SMA.100, SMA.200) %>%
  gather(key = type, value = price, close:SMA.200) %>%
  View()

Using the gather function, the values of SMA 100. and SMA 200. are moved to the type variable.

Q4 Filter the date for 2016-06-01 to the latest.

No need to type answer. Just revise the code.

Q5 Does the chart identify a bullish or bearish crossover?

This chart would indicate a bullish crossover, because the 100 day moving averages crosses above the 200 day average.

Q6 What day is the crossover?

It’s the first day after the crossover when the shorter-term moving average crosses over the longer-term moving average.

The crossover occurs on October 24th, 2016 when the SMA. 100 of 97.26$ crosses above the SMA. 200 of 97.14$.

Q7 How much 100 shares of the stock would have cost on the day of the crossover in Q6?

According to the closing price, 100 shares at 127.33 per stock would cost 12,733$.

Q8 How much profit would you have made, if you had bought 100 shares on the day of crossover and sold them today?

If the 100 shares were sold yesterday, (There is no current closing price for today, becasue the day is not over), the investor would make a profit of 17,450 dollars. The closing price of yesterday’s stock was 301.83$.

NFLX %>%
  select(date, close, SMA.100, SMA.200) %>%
  gather(key = type, value = price, close:SMA.200) %>%
  filter(date >= "2016-06-01") %>%
  ggplot(aes(x = date, y = price, col = type)) +
  geom_line() + 
  theme(legend.position="bottom") +
  labs(title = "Simple Moving Averages are a Breeze with tidyquant",
       x = NULL,
       y = "Stock Prices")

Q9 Extend the period by filtering the date for 2015-01-01 to the latest. How much would you have made if you followed the above moving average strategy?

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.

The first bearish crossover occurs on April 6th of 2016 where the investor would have made 10,483 dolars if he sold the 100 shares at 104.83. Then the bullish crossover mentioned earlier would occur on october 24th where the investor could use the 10,483 dollars to buy 82 shares. If the investor were then to sell those shares today at 331.38, he would make a profit of 16,690 dollars.



NFLX %>%
  select(date, close, SMA.100, SMA.200) %>%
  gather(key = type, value = price, close:SMA.200) %>%
  filter(date >= "2015-01-01") %>%
  ggplot(aes(x = date, y = price, col = type)) +
  geom_line() + 
  theme(legend.position="bottom") +
  labs(title = "Simple Moving Averages are a Breeze with tidyquant",
       x = NULL,
       y = "Stock Prices")



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-25  46.9      NA      NA
##  2 2013-10-28  44.9      NA      NA
##  3 2013-10-29  46.8      NA      NA
##  4 2013-10-30  45.4      NA      NA
##  5 2013-10-31  46.1      NA      NA
##  6 2013-11-01  47.0      NA      NA
##  7 2013-11-04  48.2      NA      NA
##  8 2013-11-05  48.8      NA      NA
##  9 2013-11-06  47.9      NA      NA
## 10 2013-11-07  46.7      NA      NA
## # ... with 1,249 more rows

NFLX %>%
  select(date, close, SMA.100, SMA.200) %>%
  gather(key = type, value = price, close:SMA.200) %>%
  filter(date >= "2015-01-01") %>%
  View()