Learn more about using open source R for big data analysis, predictive modeling, data science and more from the staff of Revolution Analytics.

January 09, 2014

Quantitative Finance Applications in R - 2

by Daniel Hanson QA Data Scientist, Revolution Analytics

Some Applications of the xts Time Series Package

In our previous discussion, we looked at accessing financial data using the quantmod and Quandl R packages. As noted there, the data series returned by quantmod comes in the form of an xts time series object, and Quandl provides a parameter that sets the return object of type xts. As the xts R package comes included with the quantmod package, it is not necessary to reload it as long as quantmod has been loaded.

In this article, we will look at some of the useful features in xts, by way of data retrieved from quantmod. We will also need to load one more package: moments. The moments package computes skewness and kurtosis of data, as these calculations are somewhat surprisingly not included in base R. With that said, we run the following library commands so that we’ll be ready to go:

We will, in fact, see that xts objects can typically be treated just like dataframes in a number of other cases. To wit, if we just wanted the closing prices for the series, we can extract them in the usual way:

SPY.Close <- SPY[, "SPY.Close"]

Then, note that this subset is also an xts object:

is.xts(SPY.Close) # returns TRUE

We will use this series of closing prices later when we look at plotting.

By now, you may be asking: if xts objects can be treated like dataframes, what’s the big deal? Well, the main difference is that, being indexed by date, we have a convenient tool at our disposal. For example, suppose we wanted to take the subset of prices from January 2006 through December 2007. This is easily done by entering the command

x1 <- SPY['2006-01/2007-12'] # store the output in x1, etc

Note that the index setting is of the form ‘from /to’, with date format YYYY-MM. The output is stored in a new xts object called, say, x1. In a similar fashion, if we wanted all the prices from the beginning of the set through, say, the end of July 2005, we would enter:

x2 <- SPY['/2005-07']

We can also store all the prices from a particular year, say 2010, or a particular month of the year, say December 2010, as follows (respectively):

x3 <- SPY['2010']x4 <- SPY['2010-12']

Next, suppose we wish to extract monthly or quarterly data for 2010. There are a couple of ways to do this. First, one can use the commands:

as we did for the full SPY set. What we apparently cannot do, however, is extract monthly or quarterly data from SPY.Close using the to.period(.), to.monthly(.) or to.quarterly(.) command. One gets unexpected behavior where all columns of the original SPY set are returned, rather than just the desired subsets of the SPY.Close prices. The reason for this does not seem to be provided in the xts documentation or vignette.

Getting back to the task at hand, we can plot the SPY.Close series in a fashion similar to plotting a vector of data using the plot(.) command. We can use the same parameters to indicate the x and y axis labels, a title, and the color of the graph. In addition, we can make our plot look nicer by using parameters specific to xts objects. For example, let’s look at the following plot(.) command:

The parameters main, col, xlab, and ylab are the same as those used in base R plot(.). The parameters major.ticks and minor.ticks are specific to xts; the former will display years along the x-axis, while setting the latter to FALSE, suffice it to say for now, avoids a gray bar along the x-axis.

The above hopefully provides a useful introduction to the xts package for use with financial market data. The plot examples given are admittedly rudimentary, and other packages, including quantmod, provide more sophisticated features that result in a more palatable presentation, as well as more useful information, such as plots of overlayed time series. We will look at more advanced graphics in an upcoming article.

Comments

You can follow this conversation by subscribing to the comment feed for this post.

This is a very nice post to highlight the great xts package by Jeff Ryan and Josh Ulrich. Above, Joseph mentioned that xts objects behave much like data.frames in terms of subsetting etc. It is important to know that the data in an xts object is actually stored in a matrix, not a data.frame. The xts object does not support data in a data.frame and any attempt to do so will result in the data.frame being converted into a matrix. So if you have mixed data types in a data.frame x.df (e.g. character data and numeric data) and you create an xts object using the xts() constructor function with x.df as the data then the resulting xts object will have its data as a character matrix (which you can confirm using class(coredata(my.xts))).

If I make a script out of the code above, to get data for QQQ instead of SPY, I need to replace "SPY" with "QQQ" in many places, instead of one, which would be better. This occurs because, as the post states, "the return object from getSymbols is assigned the name of the ticker symbol, in this case SPY." How can the code be made more generic so that it works for any ticker symbol?

In last post You wrote "One of the limitations of data available from Yahoo and Google, as may be noticed above, is that it only dates back to January of 2007,"

and based on that instructed users to use futures data instead which is "common in these situations", which they can get at Quandl

yet in this tutorial You claim "getSymbols("SPY", src="google", from = "2004-01-01")"

Lately there is so much hustle-peddling of Quandl in data community that besides its goodness is really nothing special, and You lower Yourself in eyes of people who are actually very familiar with these matters.