rm(list=ls())
library(ggplot2)
library(data.table)
library(quantmod)
## Warning: package 'quantmod' was built under R version 4.2.3
## Loading required package: xts
## Warning: package 'xts' was built under R version 4.2.3
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## ################################### WARNING ###################################
## # We noticed you have dplyr installed. The dplyr lag() function breaks how    #
## # base R's lag() function is supposed to work, which breaks lag(my_xts).      #
## #                                                                             #
## # If you call library(dplyr) later in this session, then calls to lag(my_xts) #
## # that you enter or source() into this session won't work correctly.          #
## #                                                                             #
## # All package code is unaffected because it is protected by the R namespace   #
## # mechanism.                                                                  #
## #                                                                             #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning.  #
## #                                                                             #
## # You can use stats::lag() to make sure you're not using dplyr::lag(), or you #
## # can add conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop   #
## # dplyr from breaking base R's lag() function.                                #
## ################################### WARNING ###################################
## 
## Attaching package: 'xts'
## The following objects are masked from 'package:data.table':
## 
##     first, last
## Loading required package: TTR
## Warning: package 'TTR' was built under R version 4.2.3
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(xts)

Q1: Use getSymbols function in quantmod package to extract daily closing prices of Tesla (ticker: TSLA) from 2015 to 2021. Print the header of the data.

##                Open     High      Low    Close   Volume Adjusted
## 2015-01-02 14.85800 14.88333 14.21733 14.62067 71466000 14.62067
## 2015-01-05 14.30333 14.43333 13.81067 14.00600 80527500 14.00600
## 2015-01-06 14.00400 14.28000 13.61400 14.08533 93928500 14.08533
## 2015-01-07 14.22333 14.31867 13.98533 14.06333 44526000 14.06333
## 2015-01-08 14.18733 14.25333 14.00067 14.04133 51637500 14.04133
## 2015-01-09 13.92800 13.99867 13.66400 13.77733 70024500 13.77733

Q2: Calculate daily returns using the data extracted in Q1. Print the header.

##            daily.returns
## 2015-01-02  -0.015973415
## 2015-01-05  -0.042040968
## 2015-01-06   0.005664215
## 2015-01-07  -0.001561908
## 2015-01-08  -0.001564352
## 2015-01-09  -0.018801634

Q3: Use the function created in the lecture video to extract monthly prices for your favorite five stocks for the last five years. Print the header.

I have selected AAPL, BAC, and T for this illustration.

##          date    AAPL   BAC        T
## 1: 2017-01-01 30.3375 22.64 31.84290
## 2: 2017-02-01 34.2475 24.68 31.56344
## 3: 2017-03-01 35.9150 23.59 31.38218
## 4: 2017-04-01 35.9125 23.34 29.93202
## 5: 2017-05-01 38.1900 22.41 29.10121
## 6: 2017-06-01 36.0050 24.26 28.49698

Q4: Convert the data extracted in Q3 to ‘long format’. Print the header.

##          date variable   value
## 1: 2017-01-01     AAPL 30.3375
## 2: 2017-02-01     AAPL 34.2475
## 3: 2017-03-01     AAPL 35.9150
## 4: 2017-04-01     AAPL 35.9125
## 5: 2017-05-01     AAPL 38.1900
## 6: 2017-06-01     AAPL 36.0050

Q5: Plot the stock prices as a line graph.

Q6: Use the functions in the quantmod package to conduct your own analysis for one or more stocks. To get the full credit, your idea should not be a variation of the previous questions.

IMPORTANT: RUN THE FOLLOWING CODE CHUNK

savehistory()