<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/creating-a-vix-futures-term-structure-in-r-from-official-cboe-settlement-data/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://quantstrattrader.wordpress.com/2017/04/27/creating-a-vix-futures-term-structure-in-r-from-official-cboe-settlement-data/"> R – QuantStrat TradeR</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>This post will be detailing a process to create a VIX term structure from freely available CBOE VIX settlement data and a calendar of freely obtainable VIX expiry dates. This has applications for volatility trading strategies.</p>
<p>So this post, as has been the usual for quite some time, will not be about a strategy, but rather, a tool that can be used for exploring future strategies. Particularly, volatility strategies–which seems to have been a hot topic on this blog some time ago (and might very well be still, especially since the <a target="_blank" rel="nofollow" href="http://volatilitymadesimple.com/blog/">Volatility Made Simple blog</a> has just stopped tracking open-sourced strategies for the past year).</p>
<p>This post’s topic is the VIX term structure–that is, creating a set of continuous contracts–properly rolled according to <a target="_blank" rel="nofollow" href="http://cfe.cboe.com/products/vx-cboe-volatility-index-vix-futures/contract-specifications">VIX contract specifications</a>, rather than a hodgepodge of generic algorithms as found on some other websites. The idea is, as of the settlement of a previous day (or whenever the CBOE actually releases their data), you can construct a curve of contracts, and see if it’s in contango (front month cheaper than next month and so on) or backwardation (front month more expensive than next month, etc.).</p>
<p>The first (and most code-intensive) part of the procedure is fairly simple–map the contracts to an expiration date, then put their settlement dates and times to expiry into two separate xts objects, with one column for each contract. </p>
<p>The expiries text file is simply a collection of copied and pasted expiry dates from <a target="_blank" rel="nofollow" href="http://www.macroption.com/vix-expiration-calendar/">this site.</a> It includes the January 2018 expiration date. Here is what it looks like:</p>
<pre>
> head(expiries)
  V1       V2   V3
1 18  January 2006
2 15 February 2006
3 22    March 2006
4 19    April 2006
5 17      May 2006
6 21     June 2006
</pre>
<pre>
require(xts)
require(data.table)

# 06 through 17
years &lt;- c(paste0(&quot;0&quot;, c(6:9)), as.character(c(10:17)))

# futures months
futMonths &lt;- c(&quot;F&quot;, &quot;G&quot;, &quot;H&quot;, &quot;J&quot;, &quot;K&quot;, &quot;M&quot;,
            &quot;N&quot;, &quot;Q&quot;, &quot;U&quot;, &quot;V&quot;, &quot;X&quot;, &quot;Z&quot;)

# expiries come from http://www.macroption.com/vix-expiration-calendar/
expiries &lt;- read.table(&quot;expiries.txt&quot;, header = FALSE, sep = &quot; &quot;)

# convert expiries into dates in R
dateString &lt;- paste(expiries$V3, expiries$V2, expiries$V1, sep = &quot;-&quot;)
dates &lt;- as.Date(dateString, format = &quot;%Y-%B-%d&quot;)

# map futures months to numbers for dates
monthMaps &lt;- cbind(futMonths, c(&quot;01&quot;, &quot;02&quot;, &quot;03&quot;, &quot;04&quot;, &quot;05&quot;, &quot;06&quot;,
                                   &quot;07&quot;, &quot;08&quot;, &quot;09&quot;, &quot;10&quot;, &quot;11&quot;, &quot;12&quot;))
monthMaps &lt;- data.frame(monthMaps)
colnames(monthMaps) &lt;- c(&quot;futureStem&quot;, &quot;monthNum&quot;)

dates &lt;- data.frame(dates)
dates$dateMon &lt;- substr(dates$dates, 1, 7)

contracts &lt;- expand.grid(futMonths, years)
contracts &lt;- paste0(contracts[,1], contracts[,2])
contracts &lt;- c(contracts, &quot;F18&quot;)
stem &lt;- &quot;https://cfe.cboe.com/Publish/ScheduledTask/MktData/datahouse/CFE_&quot;
#contracts &lt;- paste0(stem, contracts, &quot;_VX.csv&quot;)

masterlist &lt;- list()
timesToExpiry &lt;- list()
for(i in 1:length(contracts)) {
  
  # obtain data
  contract &lt;- contracts[i]
  dataFile &lt;- paste0(stem, contract, &quot;_VX.csv&quot;)
  expiryYear &lt;- paste0(&quot;20&quot;,substr(contract, 2, 3))
  expiryMonth &lt;- monthMaps$monthNum[monthMaps$futureStem == substr(contract,1,1)]
  expiryDate &lt;- dates$dates[dates$dateMon == paste(expiryYear, expiryMonth, sep=&quot;-&quot;)]
  data &lt;- suppressWarnings(fread(dataFile))
  
  # create dates
  dataDates &lt;- as.Date(data$`Trade Date`, format = '%m/%d/%Y')
  
  # create time to expiration xts
  toExpiry &lt;- xts(expiryDate - dataDates, order.by=dataDates)
  colnames(toExpiry) &lt;- contract
  timesToExpiry[[i]] &lt;- toExpiry
  
  # get settlements
  settlement &lt;- xts(data$Settle, order.by=dataDates)
  colnames(settlement) &lt;- contract
  masterlist[[i]] &lt;- settlement
}

# cbind outputs
masterlist &lt;- do.call(cbind, masterlist)
timesToExpiry &lt;- do.call(cbind, timesToExpiry)

# NA out zeroes in settlements
masterlist[masterlist==0] &lt;- NA
</pre>
<p>From there, we need to visualize how many contracts are being traded at once on any given day (I.E. what’s a good steady state number for the term structure)?</p>
<pre>
sumNonNA &lt;- function(row) {
  return(sum(!is.na(row)))
}

simultaneousContracts &lt;- xts(apply(masterlist, 1, sumNonNA), order.by=index(masterlist))
chart.TimeSeries(simultaneousContracts)
</pre>
<p>The result looks like this:</p>
<p><img src="https://i0.wp.com/i.imgur.com/sBh7vIw.png"></p>
<p>So, 8 contracts (give or take) at any given point in time. This is confirmed by the end of the master list of settlements.</p>
<pre>
dim(masterlist)
tail(masterlist[,135:145])
</pre>
<pre>
> dim(masterlist)
[1] 3002  145
> tail(masterlist[,135:145])
           H17    J17    K17    M17    N17    Q17    U17    V17    X17    Z17   F18
2017-04-18  NA 14.725 14.325 14.525 15.175 15.475 16.225 16.575 16.875 16.925    NA
2017-04-19  NA 14.370 14.575 14.525 15.125 15.425 16.175 16.575 16.875 16.925    NA
2017-04-20  NA     NA 14.325 14.325 14.975 15.375 16.175 16.575 16.875 16.900    NA
2017-04-21  NA     NA 14.325 14.225 14.825 15.175 15.925 16.350 16.725 16.750    NA
2017-04-24  NA     NA 12.675 13.325 14.175 14.725 15.575 16.025 16.375 16.475 17.00
2017-04-25  NA     NA 12.475 13.125 13.975 14.425 15.225 15.675 16.025 16.150 16.75
</pre>
<p>Using this information, an algorithm can create eight continuous contracts, ranging from front month to eight months out. The algorithm starts at the first day of the master list to the first expiry, then moves between expiry windows, and just appends the front month contract, and the next seven contracts to a list, before rbinding them together, and does the same with the expiry structure.</p>
<pre>
termStructure &lt;- list()
expiryStructure &lt;- list()
masterDates &lt;- unique(c(first(index(masterlist)), dates$dates[dates$dates %in% index(masterlist)], Sys.Date()-1))
for(i in 1:(length(masterDates)-1)) {
  subsetDates &lt;- masterDates[c(i, i+1)]
  dateRange &lt;- paste(subsetDates[1], subsetDates[2], sep=&quot;::&quot;)
  subset &lt;- masterlist[dateRange,c(i:(i+7))]
  subset &lt;- subset[-1,]
  expirySubset &lt;- timesToExpiry[index(subset), c(i:(i+7))]
  colnames(subset) &lt;- colnames(expirySubset) &lt;- paste0(&quot;C&quot;, c(1:8))
  termStructure[[i]] &lt;- subset
  expiryStructure[[i]] &lt;- expirySubset
}

termStructure &lt;- do.call(rbind, termStructure)
expiryStructure &lt;- do.call(rbind, expiryStructure)
</pre>
<p>Again, one more visualization of when we have a suitable number of contracts:</p>
<pre>
simultaneousContracts &lt;- xts(apply(termStructure, 1, sumNonNA), order.by=index(termStructure))
chart.TimeSeries(simultaneousContracts)
</pre>
<p><img src="https://i1.wp.com/i.imgur.com/mi9BaUU.png"></p>
<p>And in order to preserve the most data, we’ll cut the burn-in period off when we first have 7 contracts trading at once.</p>
<pre>
first(index(simultaneousContracts)[simultaneousContracts >= 7])
termStructure &lt;- termStructure[&quot;2006-10-23::&quot;]
expiryStructure &lt;- expiryStructure[index(termStructure)]
</pre>
<p>So there you have it–your continuous VIX futures contract term structure, as given by the official CBOE settlements. While some may try and simulate a trading strategy based on these contracts, I myself prefer to use them as indicators or features to a model that would rather buy XIV or VXX.</p>
<p>One last trick, for those that want to visualize things, a way to actually visualize the term structure on any given day, in particular, the most recent one in the term structure.</p>
<pre>
plot(t(coredata(last(termStructure))), type = 'b')
</pre>
<p><img src="https://i0.wp.com/i.imgur.com/JWt1swG.png"></p>
<p>A clear display of contango.</p>
<p>A post on how to compute synthetic constant-expiry contracts (EG constant 30 day expiry contracts) will be forthcoming in the near future.</p>
<p>Thanks for reading.</p>
<p>NOTE: I am currently interested in networking and full-time positions which may benefit from my skills. I may be contacted at my LinkedIn profile found <a target="_blank" rel="nofollow" href="https://www.linkedin.com/in/ilyakipnis/">here</a>.</p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/quantstrattrader.wordpress.com/630/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/quantstrattrader.wordpress.com/630/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://quantstrattrader.wordpress.com/2017/04/27/creating-a-vix-futures-term-structure-in-r-from-official-cboe-settlement-data/"> R – QuantStrat TradeR</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/fWclkr0erqc"></a></div>