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

December 27, 2013

Quantitative Finance Applications in R

by Daniel Hanson, QA Data Scientist, Revolution Analytics

Extracting Financial Data from Internet Source Using R (first in a series)

Earlier this month, a colleague and I attended a presentation on Computational Finance in R, given by Guy Yollin of the University of Washington Applied Mathematics faculty, at a meeting of the Seattle useR Group. The first among several topics was extracting financial time series from internet sources using two R packages: quantmod, and Quandl. This will be the subject of today’s blog post.

For those of us who do not have access to a Bloomberg terminal but wish to have data to experiment with financial models, these two packages are a real boon. While quantmod also contains plotting and charting functionality, we will look at its capability of downloading stock and index prices from Yahoo Finance and Google Finance. The Quandl package is a bit different in that it is tied in with the Quandl website, a source of financial data itself, as well as a portal to economic and social science data. We will focus on accessing financial futures data on Quandl.

quantmod

Many readers are likely familiar with the finance sites on Yahoo and Google as sources for tracking stock, mutual fund, and exchange traded fund (ETF) prices and returns. Using quantmod, one can easily load this data into R by specifying the the same ticker symbol that is used in these two web sources.

As with any other R package, one must install the quantmod package in the usual way. As an aside, included with the installation is also the xts package for time series data, which we will discuss briefly later on.

After installing the package, using it is quite simple and straightforward. For example, to retrieve all available data for the PowerShares QQQ Nasdaq 100 Index ETF, one enters the following R command:

getSymbols("QQQ")

The data set is returned to the R session in the form of an xts object with the name QQQ. To check the contents, the head(.) and tail(.) R functions are useful:

Now, a couple of remarks about what we have done, and other options that are available. First, note that we are given daily prices. In order to retrieve, say, monthly values, one may use an xts command on the return object; xts also provides functionality to extract a subset of the data based on a range of dates. This is a discussion worth having, given that xts is widely used in financial modeling in R, but we will defer it to a later blog post. Secondly, note that we did not specify the data source; this is because the default is Yahoo.

So, as another example, and employing a little bit of irony, let’s download Yahoo’s stock price history, using Google Finance as the data source:

Quandl

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, while fund tracking and analysis will often require a time series of 20 years or more of historical returns. Furthermore, some of the index-tracking ETF’s didn’t come into existence until the early 2000’s, and new tracking funds for emerging markets and other indices have even shorter histories. To get return data going back farther in time, it is common to look to futures markets, and Quandl provides a rich set of historical futures prices.

As with the quantmod package, it is necessary to install the Quandl package in the same way. The Quandl R package help page gives step by step instructions, followed by a first example of loading futures price data into R, which we also show here:

This gives us a first example, but note that the data only goes back to September of 2009. As a more realistic example, let’s look at futures prices that have a much longer record, and let’s specify that we want monthly rather than daily (the default setting) data. A good example is the price of gold futures, but how would we find the lookup symbol? There is an R search function in the Quandl package, but at least at the time of this writing, it exhibits, shall we say, some need for improvement.

We have found a more reliable approach at this stage is to start with the Futures section of the Quandl website, and then select Metals from the list that appears at left. At the top of the Metals list that is now displayed, click on Gold (Symbol GC/Exchange COMEX). A new Gold Futures page will load, with two contracts (GC1 and GC2) shown under Latest Quotes. Choose Historical Data for GC2, and a page showing a plot of returns and price data will be displayed; however, we want to get the symbol to use in our R command, and to do this, scroll all the way down to the bottom of the page where a box entitled DATASET INFORMATION is displayed, as shown here:

We can now get the lookup symbol from the end of the Permalink line, namely

Although this example may seem a bit cumbersome, running through it a few times with different types of futures contracts and trying it out, the process of obtaining the lookup symbol eventually becomes somewhat routine.

Finally, suppose we don’t want to go back to 1974, but instead we want a 20 year period of monthly prices from June 1992 through May 2012. In this case, we simply put in start_date and end_date parameters as follows:

This will give us the end-of-month futures price data from June 1992 through May 2012.

To calculate the log returns of monthly settled prices, we again extract this data as a vector, but remembering that the prices are in reverse chronological order, we need to reverse the order of the vector and then do the same calculation as we did for the Yahoo stock returns in the previous section:

In future posts, we will expand into topics of interest in quantitative finance and risk management. In the meantime, for more examples of using Quandl data in R, take a look at Joseph Rickert’s mini-tutorial on Quandl.