Quantmod Vignette
Quantmod Vignette
This vignette gives a brief introduction to obtaining data from the web by using the R package quantmod.
As example data, the time series of the S&P 500 index is used. This data is also used in Carmona, page
5 ff.
First, we load the package quantmod:
R> require("quantmod")
quantmod provides a very suitable function for downloading financial date from the web. This function
is called getSymbols. The first argument of this function is a character vector specifying the names of
the symbols to be downloaded and the second one specifies the environment where the object is created.
The help page of this function (?getSymbols) provides more information. By default, objects are created
in the workspace. Here, we use a separate environment which we call sp500 to store the downloaded
data. We first create the environment:
R> sp500 <- new.env()
We can then download the S&P 500 time series (symbol: ^GSPC) from 1960-01-04 to 2009-01-01 from
yahoo finance via:
R> getSymbols("^GSPC", env = sp500, src = "yahoo",
+
from = as.Date("1960-01-04"), to = as.Date("2009-01-01"))
[1] "GSPC"
The package quantmod works with a variety of sources. Current src methods available are: yahoo,
google, MySQL, FRED, csv, RData, and oanda. For example, FRED (Federal Reserve Economic Data),
is a database of 20,070 U.S. economic time series (see http://research.stlouisfed.org/fred2/).
There are several possibilities, to load the variable GSPC from the environment sp500 to a variable in
the global environment (also known as the workspace), e.g., via
R> GSPC <- sp500$GSPC
R> GSPC1 <- get("GSPC", envir = sp500)
R> GSPC2 <- with(sp500, GSPC)
The object GSPC1 and GSPC2 are identical to GSPC so we can remove them from the workspace with:
R> rm(GSPC1)
R> rm(GSPC2)
The function head shows the first six rows of the data.
1
R> head(GSPC)
1960-01-04
1960-01-05
1960-01-06
1960-01-07
1960-01-08
1960-01-11
This is on OHLC time series with at least the (daily) Open, Hi, Lo and Close prices for the symbol;
here, it also contains the traded volume and the closing price adjusted for splits and dividends.
The data object is an extensible time series (xts) object:
R> class(GSPC)
[1] "xts" "zoo"
Here, it is a multivariate (irregular) time series with 12334 daily observations on 6 variables:
R> dim(GSPC)
[1] 12334
Such xts objects allow for conveniently selecting single time series using $
R> head(GSPC$GSPC.Volume)
1960-01-04
1960-01-05
1960-01-06
1960-01-07
1960-01-08
1960-01-11
GSPC.Volume
3990000
3710000
3730000
3310000
3290000
3470000
as well as very conviently selecting observations according to their time stamp by using a character row
index in the ISO 8601 date/time format CCYY-MM-DD HH:MM:SS, where more granular elements
may be left out in which case all observations with time stamp matching the given one will be used.
E.g., to get all observations in March 1970:
R> GSPC["1970-03"]
1970-03-02
1970-03-03
1970-03-04
1970-03-05
1970-03-06
1970-03-09
1970-03-10
1970-03-11
1970-03-12
1970-03-13
1970-03-16
1970-03-17
1970-03-18
1970-03-19
1970-03-20
1970-03-23
1970-03-24
1970-03-25
1970-03-26
1970-03-30
1970-03-31
88.33
87.86
86.91
87.29
87.54
87.42
87.06
86.99
88.11
89.77
89.92
89.63
89.43
87.97
87.86
88.28
88.20
87.77
87.64
88.43
91.07
90.65
90.41
90.17
87.29
86.39
86.36
86.93
86.88
86.43
86.19
86.90
88.11
89.18
88.91
88.85
87.86
86.91
87.29
87.54
87.42
87.06
86.99
87.98
89.77
89.92
89.63
89.63
9560000
8910000
9090000
9790000
8930000
7910000
7330000
8840000
17500000
11350000
9600000
8370000
87.86
86.91
87.29
87.54
87.42
87.06
86.99
87.98
89.77
89.92
89.63
89.63
It is also possible to specify a range of timestamps using / as the range separator, where both endpoints
are optional: e.g.,
R> GSPC["/1960-01-06"]
1960-01-04
1960-01-05
1960-01-06
2008-12-26
2008-12-29
2008-12-30
2008-12-31
1960-01-04
1960-01-05
1960-01-06
1960-01-07
1960-01-08
1960-01-11
GSPC.Close
59.91
60.39
60.13
59.69
59.50
58.77
R> head(OpCl(GSPC))
OpCl.GSPC
1960-01-04
0
3
1960-01-05
1960-01-06
1960-01-07
1960-01-08
1960-01-11
0
0
0
0
0
R> head(ClCl(GSPC))
1960-01-04
1960-01-05
1960-01-06
1960-01-07
1960-01-08
1960-01-11
ClCl.GSPC
NA
0.008012018
-0.004305349
-0.007317479
-0.003183113
-0.012268908
If we are intersted in the daily values of the weekly last-traded-day, we aggregate it by using an
appropriate function from the zoo Quick-Reference (Shah et al., 2005). The zoo Quick-Reference can
be found in the web, cran.r-project.org/web/packages/zoo/vignettes/zoo-quickref.pdf, and it
is strongly recommended to have a look at this vignette since it gives a very good overview of the zoo
package. Their convenience function nextfri computes for each Date the next Friday.
R> nextfri <- function(x) 7 * ceiling(as.numeric(x - 5 + 4)/7) + as.Date(5 - 4)
We get the aggregated data then via
R> SP.we <- aggregate(GSPC, nextfri, tail, 1)
The function aggregate splits the data into subsets here according to the function nextfri and
computes statistics for each, i.e., takes the last value, which is done by tail.
This works because the data object is also a Zs ordered observations (zoo) object which knows to
apply nextfri() to the index (timestamps). However, this loses the xts class: if this is not desired, one
can use
R> SP.we <- xts(aggregate(GSPC, nextfri, tail, 1))
instead.
(Alternatively, package quantmod provides apply.weekly(), which uses a slightly different endpoint
strategy.)
We can now extract the closing prices for the last trading day in every week:
R> SPC.we <- Cl(SP.we)
and create a plot of this time series via
R> plot(SPC.we)
(see Figure 1).
Finally, we can create log-returns by hand and visualize these as well
R> lr <- diff(log(SPC.we))
R> plot(lr)
(see Figure 2).
Alternatively, we could use periodReturn() (and relatives, specifically weeklyReturn()) from quantmod with type = "log". Again, this will give slightly different values.
4
500
1000
1500
SPC.we
Figure 1: Plot of the weekly S&P 500 index closing values from 1960-01-04 to 2009-01-01.
In this example we want analyze an American stock exchange, the National Association of Securities
Dealers Automated Quotations, better known as NASDAQ. It is the largest electronic screen-based equity
securities trading market in the United States.
From the web we obtained a txt-file containing the names and symbols of the NASDAQ100 (June
2009) which can be loaded via
R> sym <- readLines("nasdaqsymbols.txt")
R> head(sym)
[1] "CTIVISION INC \tATVI"
"ADOBE SYS INC \tADBE"
[3] "ALTERA CORP \tALTR"
"AMAZON \tAMZN"
[5] "AMERICAN POWER CONVER CORP \tAPCC" "AMGEN \tAMGN"
The elements are separated by a space and a tab (\t). We therefore use the command strsplit to
split the vector into names and symbols:
R> tmp <- strsplit(sym, split = " \t")
We can then use the function lapply to get only the symbol of each company, and unlist to create
a character vector with the symbols of the NASDAQ companies as entries.
R> symbols <- unlist(lapply(tmp, `[`, 2))
We assign the companies names to the symbols
R> names(symbols) <- unlist(lapply(tmp, `[`, 1))
5
0.20
0.15
0.10
0.05
0.00
0.05
0.10
lr
Figure 2: Plot of the weekly S&P 500 index log-returns values from 1960-01-04 to 2009-01-01.
and get
R> head(symbols, 10)
CTIVISION INC
"ATVI"
ALTERA CORP
"ALTR"
AMERICAN POWER CONVER CORP
"APCC"
APOLLO GROUP-A
"APOL"
APPLIED MATERIAL
"AMAT"
As before we create a new environment for our NASDAQ data and use the function getSymbols of
the quantmod package to download the NASDAQ time series from 2000-01-01 to today. By using the
command tryCatch we handle unusual conditions, including errors and warnings. In this case, if the data
from a company are not available from yahoo finance, the message "Symbol ... not downloadable!"
is provided.
R> nasdaq <- new.env()
R> for(i in head(symbols, 10)){
+
status <- tryCatch(getSymbols(i, env = nasdaq, src = "yahoo",
+
from = as.Date("2000-01-01")),
+
error = identity)
+
if(inherits(status, "error"))
+
cat("Symbol '", i, "' not downloadable!\n", sep = "")
+ }
6
Further, the command chartSeries of the package quantmod provides the full financial charting
abilities to R and allows for an interaction within the charts. E.g., using
R> chartSeries(nasdaq$AAPL)
gives a chart of the Apple values (see Figure 3) and e.g., with the command with(nasdaq,addOBV(AAPL))
the On-Balance volume can be visualized in the plot. See the manual of the quantmod package (Ryan,
2008) for the whole list of available plot and visualization functions.
$
nasdaq
AAPL
[20000103/20141126]
700
Last 119
600
500
400
300
200
100
0
1500
Volume (millions):
40,768,300
1000
500
0
Jan 03
2000
Jan 02
2002
Jan 02
2004
Jan 03
2006
Jan 02
2008
Jan 04
2010
Jan 03
2012
Jan 02
2014
Have a look at the quantmod homepage for further examples and try to reproduce them, http:
//www.quantmod.com/examples/intro/.