Downloading Options Data in R: An Update
I recently read an article which showed how to download Option Chain data from Google Finance using R. Interestingly, that article appears to be a close adaption of another article which does the same thing using Python.
While playing around with the code from these articles I noticed a couple of things that might benefit from minor tweaks. Before I look at those though, it’s worthwhile pointing out that there already is a function in quantmod for retrieving Option Chain data from Yahoo! Finance. What I am doing here is thus more for my own personal edification (but hopefully you will find it interesting too!).
The full code for this project (along with a number of updates subsequent to the original post) is available on GitHub.
An Option Chain is just a list of all available options for a particular security spanning a range of expiration dates.
First we need to load a few packages which facilitate the downloading, parsing and manipulation of the data.
We’ll be retrieving the data in JSON format. Somewhat disturbingly the JSON data from Google Finance does not appear to be fully compliant with the JSON standards because the keys are not quoted. We’ll use a helper function which will run through the data and insert quotes around each of the keys. The original code for this function looped through a list of key names. This is a little inefficient and would also be problematic if additional keys were introduced. We’ll get around that by using a different approach which avoids stipulating key names.
To make the download function more concise we’ll also define two URL templates.
And finally the download function itself, which proceeds through the following steps for a specified ticker symbol:
- downloads summary data;
- extracts expiration dates from the summary data and downloads the options data for each of those dates;
- concatenates these data into a single structure, neatens up the column names and selects a subset.
Let’s give it a whirl. (The data below were retrieved on Saturday 10 January 2015).
This is what the resulting data look like, with all available expiration dates consolidated into a single table:
There is a load of data there. To get an idea of what it looks like we can generate a couple of plots. Below is the Open Interest as a function of Strike Price across all expiration dates. The underlying price is indicated by the vertical dashed line. As one might expect, the majority of interest is associated with the next expiration date on 17 January 2015.
It’s pretty clear that this is not the optimal way to look at these data and I would be extremely interested to hear from anybody with a suggestion for a better visualisation. Trying to look at all of the expiration dates together is probably the largest problem, so let’s focus our attention on those options which expire on 17 January 2015. Again the underlying price is indicated by a vertical dashed line.
This is the first time that I have seriously had a look at options data, but I will now readily confess to being intrigued. Having the data readily available, there is no reason not to explore further. Details to follow.