Much of my time lately has gone into analyzing and trading products in the volatility complex. As a result, I regularly watch the VIX term structure for continuations or deviations from trend. To make analysis simpler, I’ve written some R code that rips the term structure off the CBOE VIX term structure page and parses it into a table with proper typing. You can view this code in the embedded gist below:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#@author Bommarito Consulting, LLC; http://bommaritollc.com/ | |
#@date 20121105 | |
# Imports | |
library(RCurl) | |
library(XML) | |
# Set RCurl options | |
options(RCurlOptions = list(timeout = 10, useragent = "Mozilla/6.0 (Windows NT 6.2; WOW64; rv:16.0.1) Gecko/20121011 Firefox/16.0.1")) | |
# Retrieve CBOE VIX term structure page and parse into HTML. | |
buffer <- getURL("http://www.cboe.com/data/volatilityindexes/volatilityindexes.aspx") | |
doc <- htmlParse(buffer, asText=T) | |
# Extract the term structure table. | |
vixTerm <- readHTMLTable(getNodeSet(doc, "//table[starts-with(@id, 'ctl00_')]")[[1]], header=T, colClasses=c('character', 'character', 'numeric', 'numeric'), as.factor=F) | |
# Clean up and process dates. | |
names(vixTerm) <- c("tradeDate", "expirationDate", "value", "month") | |
vixTerm$tradeDate <- as.POSIXct(vixTerm$tradeDate, format="%m/%d/%Y %I:%M:%S %p") | |
vixTerm$expirationDate <- as.Date(vixTerm$expirationDate, "%d-%b-%y") |
You’ll likely be interested in the getSymbols.cfe method in my qmao package (https://r-forge.r-project.org/R/?group_id=1113)
The following comes from the examples section of ?getSymbols.cfe
library(qmao)
getSymbols(c(“VX_U11”, “VX_V11”),src=’cfe’)
#all contracts expiring in 2010 and 2011.
getSymbols(“VX”,Months=1:12,Years=2010:2011,src=’cfe’)
#getSymbols(“VX”,Months=1:12,Years=10:11,src=’cfe’) #same
#The contracts expiring this month:
getSymbols(c(“VM”,”GV”),src=’cfe’)
If you are not familiar with quantmod/getSymbols, by default, it assigns the data to an environment (.GlobalEnv by default) instead of returning it. So, e.g. after running getSymbols(“VX_H12”) the data will be in a variable named VX_H12.