Conduct the trend analysis for the Tesla stock.

For Q1 - Q3

library(tidyquant)

# Import data
from = today() - years(4)
Stocks <- tq_get("TSLA", get = "stock.prices", from = from)
Stocks
## # A tibble: 1,006 x 7
##    date        open  high   low close  volume adjusted
##    <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>
##  1 2015-02-26  204   211.  202.  207. 6472900     207.
##  2 2015-02-27  207.  209.  203.  203. 3882100     203.
##  3 2015-03-02  203.  203.  196.  197. 7922100     197.
##  4 2015-03-03  197.  200.  195.  200. 4432300     200.
##  5 2015-03-04  199.  203.  197.  202. 4212000     202.
##  6 2015-03-05  203.  206.  200.  201. 4877000     201.
##  7 2015-03-06  199.  201.  192.  194. 6712400     194.
##  8 2015-03-09  194.  194.  188.  191. 6736700     191.
##  9 2015-03-10  188.  194.  188.  190. 5530900     190.
## 10 2015-03-11  191.  196.  191.  194. 4974900     194.
## # ... with 996 more rows
# See the list of different mutate_fun avaiable 
tq_transmute_fun_options()
## $zoo
##  [1] "rollapply"          "rollapplyr"         "rollmax"           
##  [4] "rollmax.default"    "rollmaxr"           "rollmean"          
##  [7] "rollmean.default"   "rollmeanr"          "rollmedian"        
## [10] "rollmedian.default" "rollmedianr"        "rollsum"           
## [13] "rollsum.default"    "rollsumr"          
## 
## $xts
##  [1] "apply.daily"     "apply.monthly"   "apply.quarterly"
##  [4] "apply.weekly"    "apply.yearly"    "diff.xts"       
##  [7] "lag.xts"         "period.apply"    "period.max"     
## [10] "period.min"      "period.prod"     "period.sum"     
## [13] "periodicity"     "to.daily"        "to.hourly"      
## [16] "to.minutes"      "to.minutes10"    "to.minutes15"   
## [19] "to.minutes3"     "to.minutes30"    "to.minutes5"    
## [22] "to.monthly"      "to.period"       "to.quarterly"   
## [25] "to.weekly"       "to.yearly"       "to_period"      
## 
## $quantmod
##  [1] "allReturns"      "annualReturn"    "ClCl"           
##  [4] "dailyReturn"     "Delt"            "HiCl"           
##  [7] "Lag"             "LoCl"            "LoHi"           
## [10] "monthlyReturn"   "Next"            "OpCl"           
## [13] "OpHi"            "OpLo"            "OpOp"           
## [16] "periodReturn"    "quarterlyReturn" "seriesAccel"    
## [19] "seriesDecel"     "seriesDecr"      "seriesHi"       
## [22] "seriesIncr"      "seriesLo"        "weeklyReturn"   
## [25] "yearlyReturn"   
## 
## $TTR
##  [1] "adjRatios"          "ADX"                "ALMA"              
##  [4] "aroon"              "ATR"                "BBands"            
##  [7] "CCI"                "chaikinAD"          "chaikinVolatility" 
## [10] "CLV"                "CMF"                "CMO"               
## [13] "DEMA"               "DonchianChannel"    "DPO"               
## [16] "DVI"                "EMA"                "EMV"               
## [19] "EVWMA"              "GMMA"               "growth"            
## [22] "HMA"                "KST"                "lags"              
## [25] "MACD"               "MFI"                "momentum"          
## [28] "OBV"                "PBands"             "ROC"               
## [31] "rollSFM"            "RSI"                "runCor"            
## [34] "runCov"             "runMAD"             "runMax"            
## [37] "runMean"            "runMedian"          "runMin"            
## [40] "runPercentRank"     "runSD"              "runSum"            
## [43] "runVar"             "SAR"                "SMA"               
## [46] "SMI"                "SNR"                "stoch"             
## [49] "TDI"                "TRIX"               "ultimateOscillator"
## [52] "VHF"                "VMA"                "volatility"        
## [55] "VWAP"               "VWMA"               "wilderSum"         
## [58] "williamsAD"         "WMA"                "WPR"               
## [61] "ZigZag"             "ZLEMA"             
## 
## $PerformanceAnalytics
## [1] "Return.annualized"        "Return.annualized.excess"
## [3] "Return.clean"             "Return.cumulative"       
## [5] "Return.excess"            "Return.Geltner"          
## [7] "zerofill"

# Add the 100-day and 200-day simple moving average by passing close prices 
Stocks <-
  Stocks %>%
    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)

Stocks
## # A tibble: 1,006 x 9
##    date        open  high   low close  volume adjusted SMA.100 SMA.200
##    <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>   <dbl>   <dbl>
##  1 2015-02-26  204   211.  202.  207. 6472900     207.      NA      NA
##  2 2015-02-27  207.  209.  203.  203. 3882100     203.      NA      NA
##  3 2015-03-02  203.  203.  196.  197. 7922100     197.      NA      NA
##  4 2015-03-03  197.  200.  195.  200. 4432300     200.      NA      NA
##  5 2015-03-04  199.  203.  197.  202. 4212000     202.      NA      NA
##  6 2015-03-05  203.  206.  200.  201. 4877000     201.      NA      NA
##  7 2015-03-06  199.  201.  192.  194. 6712400     194.      NA      NA
##  8 2015-03-09  194.  194.  188.  191. 6736700     191.      NA      NA
##  9 2015-03-10  188.  194.  188.  190. 5530900     190.      NA      NA
## 10 2015-03-11  191.  196.  191.  194. 4974900     194.      NA      NA
## # ... with 996 more rows

Q1 Import Facebook for the last five years, and save the data under Stocks.

Hint: Insert the code below (only importing code).

from = today() - years(5)
Stocks <- tq_get("FB", get = "stock.prices", from = from)
Stocks
## # A tibble: 1,258 x 7
##    date        open  high   low close   volume adjusted
##    <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 2014-02-26  70.2  71.2  68.8  69.3 55322700     69.3
##  2 2014-02-27  69.3  70.0  68.9  68.9 41653700     68.9
##  3 2014-02-28  69.5  69.9  67.4  68.5 66783700     68.5
##  4 2014-03-03  67.0  68.1  66.5  67.4 56824100     67.4
##  5 2014-03-04  68.7  68.9  67.6  68.8 42013500     68.8
##  6 2014-03-05  69.7  72.0  69.6  71.6 74567700     71.6
##  7 2014-03-06  71.9  71.9  70.2  70.8 46026500     70.8
##  8 2014-03-07  71.1  71.2  69.5  69.8 38927000     69.8
##  9 2014-03-10  70.8  72.2  70.5  72.0 59871600     72.0
## 10 2014-03-11  72.5  72.6  70.0  70.1 59408300     70.1
## # ... with 1,248 more rows

Q2 Calculate daily returns. Do not save the result.

Hint: Take the adjusted variable from Stocks, and calculate daily returns using tidyquant::tq_mutate. Note that there are a variety of functions available with the mutate_fun argument: See above for the result of tq_transmute_fun_options(). Note that tq_mutate allows to calculate returns using quantmod::periodReturn. Google the quantmod package manual for more information on the periodReturn function.

Stocks %>% tq_mutate(select = adjusted, mutate_fun = periodReturn, period = "daily")
## # A tibble: 1,258 x 8
##    date        open  high   low close   volume adjusted daily.returns
##    <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>         <dbl>
##  1 2014-02-26  70.2  71.2  68.8  69.3 55322700     69.3       0      
##  2 2014-02-27  69.3  70.0  68.9  68.9 41653700     68.9      -0.00462
##  3 2014-02-28  69.5  69.9  67.4  68.5 66783700     68.5      -0.00696
##  4 2014-03-03  67.0  68.1  66.5  67.4 56824100     67.4      -0.0153 
##  5 2014-03-04  68.7  68.9  67.6  68.8 42013500     68.8       0.0206 
##  6 2014-03-05  69.7  72.0  69.6  71.6 74567700     71.6       0.0403 
##  7 2014-03-06  71.9  71.9  70.2  70.8 46026500     70.8      -0.0102 
##  8 2014-03-07  71.1  71.2  69.5  69.8 38927000     69.8      -0.0147 
##  9 2014-03-10  70.8  72.2  70.5  72.0 59871600     72.0       0.0319 
## 10 2014-03-11  72.5  72.6  70.0  70.1 59408300     70.1      -0.0268 
## # ... with 1,248 more rows

Q3 Calculate monthly returns. This time save the result under returns_monthly.

Hint: Note that tq_transmute(), instead of tq_mutate(), is used when periodicity changes. Another difference between the two is that tq_transmute() returns only newly-created columns while tq_mutate() adds new columns to existing variables.

returns_monthly <- Stocks %>%
  tq_transmute(select = adjusted, mutate_fun = periodReturn, period = "monthly", col_rename = "returns_monthly")
returns_monthly
## # A tibble: 61 x 2
##    date       returns_monthly
##    <date>               <dbl>
##  1 2014-02-28        -0.0116 
##  2 2014-03-31        -0.120  
##  3 2014-04-30        -0.00764
##  4 2014-05-30         0.0589 
##  5 2014-06-30         0.0630 
##  6 2014-07-31         0.0797 
##  7 2014-08-29         0.0299 
##  8 2014-09-30         0.0564 
##  9 2014-10-31        -0.0512 
## 10 2014-11-28         0.0361 
## # ... with 51 more rows

Q4 Create a histogram for monthly returns.

Hint: Refer to the ggplot2 cheatsheet. Google it. See the section for One Variable. Note that there are two different cases: 1) Continuous and 2) Discrete. The type of chart you can use depends on what type of data your variable is.

ggplot(returns_monthly, aes(x = returns_monthly)) +
        geom_histogram()

Q5 Create a line chart for monthly returns.

Hint: Refer to the ggplot2 cheatsheet. See the section for Two Variables.

ggplot(returns_monthly, aes(x = date, y = returns_monthly)) +
  geom_line()

Q6 Repeat Q3 - Q5 for yearly returns

returns_yearly <- Stocks %>%
  tq_transmute(select = adjusted, mutate_fun = periodReturn, period = "yearly", col_rename = "returns_yearly")
returns_yearly
## # A tibble: 6 x 2
##   date       returns_yearly
##   <date>              <dbl>
## 1 2014-12-31         0.126 
## 2 2015-12-31         0.341 
## 3 2016-12-30         0.0993
## 4 2017-12-29         0.534 
## 5 2018-12-31        -0.257 
## 6 2019-02-25         0.256

ggplot(returns_yearly, aes(x = returns_yearly)) +
        geom_histogram()


ggplot(returns_yearly, aes(x = date, y = returns_yearly)) +
  geom_line()