There are a large number of packages that have been produced by other people. These packages hold additional functions that allow you to easily accomplish a range of tasks. You must install the package to your machine. You use the function install.packages to do that. This will save all the functions in a directory. You must then require the package to make the functions available for a particular session.
We will look at the quantmod package. You can read all about the package on Cran.
The first step is to automatically download data from Yahoo finance using the function getSymbols. This will send a request to the Yahoo web site and bring back the data. If it does not get a response within a specific time frame, an error is displayed. In that case, try again.
Once the data is downloaded, take a look at the data using the head and tail functions.
#install.packages('quantmod')
require(quantmod)
getSymbols("BAC")
## [1] "BAC"
head(BAC)
## BAC.Open BAC.High BAC.Low BAC.Close BAC.Volume BAC.Adjusted
## 2007-01-03 53.40 54.18 52.99 53.33 16028200 43.17884
## 2007-01-04 53.33 53.89 53.05 53.67 13175000 43.45411
## 2007-01-05 53.59 53.59 53.03 53.24 10205000 43.10596
## 2007-01-08 53.46 53.64 52.80 53.45 9685900 43.27598
## 2007-01-09 53.60 53.71 52.97 53.50 12546500 43.31646
## 2007-01-10 53.26 53.70 53.16 53.58 10083900 43.38124
tail(BAC)
## BAC.Open BAC.High BAC.Low BAC.Close BAC.Volume BAC.Adjusted
## 2018-11-12 28.38 28.52 27.64 27.75 50307500 27.75
## 2018-11-13 27.75 28.14 27.69 27.76 57423400 27.76
## 2018-11-14 27.95 28.10 26.78 27.21 69213300 27.21
## 2018-11-15 27.14 27.97 26.88 27.90 66174800 27.90
## 2018-11-16 27.69 27.89 27.42 27.75 52456100 27.75
## 2018-11-19 27.78 28.02 27.54 27.75 45965700 27.75
You can take a look at the other functions in the quantmod package by running ?quantmod at the consol or pressing F1 when the cursor is over the word quantmod in RStudio.
head(Ad(BAC))
## BAC.Adjusted
## 2007-01-03 43.17884
## 2007-01-04 43.45411
## 2007-01-05 43.10596
## 2007-01-08 43.27598
## 2007-01-09 43.31646
## 2007-01-10 43.38124
?quantmod
?OHLC
## Help on topic 'OHLC' was found in the following packages:
##
## Package Library
## quantmod /home/rob/R/x86_64-pc-linux-gnu-library/3.4
## xts /home/rob/R/x86_64-pc-linux-gnu-library/3.4
##
##
## Using the first match ...
There are a number of built in plotting functions.
require(quantmod)
plot(Cl(BAC), col = 'black')
barChart(BAC)
Practice
The ttr package is installed and loaded as part of quantmod
require(TTR)
require(quantmod)
getSymbols("GOOG")
## [1] "GOOG"
barChart(GOOG)
candleChart(GOOG,multi.col=TRUE,theme='white', subset = 'last 4 months')
chartSeries(GOOG)
addMACD()
addBBands()
Practice
TTR packageOne of the sources is the Fred database. Here we get the data and chart it in different ways.
getSymbols('CPIAUCNS',src='FRED')
## [1] "CPIAUCNS"
plot(CPIAUCNS)
plot(diff(log(CPIAUCNS), lag=12), main="CPI Inflation")
getSymbols('DEXJPUS',src="FRED") # FX rates from FRED
## [1] "DEXJPUS"
plot(DEXJPUS, col = 'black', main = "USD-JPY")
Practice