Simple introduction to quantmod and tushare
When doing quantitative financial analysis, the first step is to obtain the related data.
In the world of R
, multiple packages are capable of doing this. In this post, we briefly
introduce two packages, quantmod
and tushare
.
quantmod
The quantmod
package is designed to assist the quantitative trader.
The information at its official website is somewhat outdated with the news section being staged at 2009. But this package is still under development at Github.
Fetch the data
We can fetch the data of Huatai 300 ETF
with
library(quantmod)
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
getSymbols("510300.SS", src = "yahoo") # Get the data: 300ETF of Huatai
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
##
## This message is shown once per session and may be disabled by setting
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
## [1] "510300.SS"
etf300 <- `510300.SS` # rename the variable, note the ` sign around the name
rm(`510300.SS`)
In the previous script, the data is fetched from source Yahoo Finance and "510300.SS"
is the code name for Huatai 300ETF at yahoo’s website.
This function also supports different data source such as "google"
for Google Finance, "FRED"
for Federal Reserve Economic Data and "oanda"
for Oanda.com. Also "csv"
and "rda"
for loading data from local source.
By default, getSymbols
will return the results in the name of the symbol, in this case, it’s "510300.SS"
, which is an unusual R
variable name since it starts with a number 5. So we rename it to etf300
.
Another way is to use setSymbolLookup
to tell quantmod
how we want our data to be fetched
setSymbolLookup(ETF300 = list(name = "510300.SS", # now ETF300 is `510300.SS`
src = "yahoo"))
and we can check the settings for this symbol with
getSymbolLookup("ETF300") # we can check our setting for this symbol
## $ETF300
## $ETF300$name
## [1] "510300.SS"
##
## $ETF300$src
## [1] "yahoo"
Then we can fetch the data as usual:
getSymbols("ETF300")
## [1] "ETF300"
Save and load the data from local file
One can save the symbol data to local file for easy future access and analysis. This can be done with saveSymbols
saveSymbols("ETF300", file.path = ".")
This command will save the Huatai 300ETF data to a local file called "ETF300.RData"
to the current working directory(file.path = "."
).
Then one can load it via getSymbols
by setting src = "RData"
:
getSymbols("ETF300", src = "RData", # we can load data from local file by src = "RData"
dir = "./", # directory and file extension of local file
extension = "RData") # see documentation of getSymbols.rda()
## [1] "ETF300"
By default, this function will look for a file with extension .rda
. We manually set it with extension = "RData"
.
Visualize the data
One advantage of quantmod
is that it is very easy to visualize the data.
chartSeries(ETF300)
Thanks to xts
package, it is easy to visualize a subset of the data
chartSeries(ETF300, subset = "2020-12-15::2021-01-18") # adjust timeline
One can also add technical indicators from the TTR package to existing figures with
addMACD() # add moving average plot
More examples can be found at quantmod
’s example website.