# Constant Expiry VIX Futures (Using Public Data)

**R – QuantStrat TradeR**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This post will be about creating constant expiry (E.G. a rolling 30-day contract) using VIX settlement data from the CBOE and the spot VIX calculation (from Yahoo finance, or wherever else). Although these may be able to be traded under certain circumstances, this is not always the case (where the desired expiry is shorter than the front month’s time to expiry).

The last time I visited this topic, I created a term structure using publicly available data from the CBOE, along with an external expiry calendar.

The logical next step, of course, is to create constant-expiry contracts, which may or may not be tradable (if your contract expiry is less than 30 days, know that the front month has days in which the time to expiry is more than 30 days).

So here’s where we left off: a way to create a continuous term structure using CBOE settlement VIX data.

So from here, before anything, we need to get VIX data. And while the getSymbols command used to be easier to use, because Yahoo broke its API (what else do you expect from an otherwise-irrelevant, washed-up web 1.0 dinosaur?), it’s not possible to get free Yahoo data at this point in time (in the event that Josh Ulrich doesn’t fix this issue in the near future, I’m open to suggestions for other free sources of data which provide data of reputable quality), so we need to get VIX data from elsewhere (particularly, the CBOE itself, which is a one-stop shop for all VIX-related data…and most likely some other interesting futures as well.)

So here’s how to get VIX data from the CBOE (thanks, all you awesome CBOE people! And a shoutout to all my readers from the CBOE, I’m sure some of you are from there).

VIX <- fread("http://www.cboe.com/publish/scheduledtask/mktdata/datahouse/vixcurrent.csv", skip = 1) VIXdates <- VIX$Date VIX$Date <- NULL; VIX <- xts(VIX, order.by=as.Date(VIXdates, format = '%m/%d/%Y')) spotVix <- Cl(VIX)

Next, there’s a need for some utility functions to help out with identifying which futures contracts to use for constructing synthetics.

# find column with greatest days to expiry less than or equal to desired days to expiry shortDurMinMax <- function(row, daysToExpiry) { return(max(which(row <= daysToExpiry))) } # find column with least days to expiry greater desired days to expiry longDurMinMax <- function(row, daysToExpiry) { return(min(which(row > daysToExpiry))) } # gets the difference between the two latest contracts (either expiry days or price) getLastDiff <- function(row) { indices <- rev(which(!is.na(row))) out <- row[indices[1]] - row[indices[2]] return(out) } # gets the rightmost non-NA value of a row getLastValue <- function(row) { indices <- rev(which(!is.na(row))) out <- row[indices[1]] return(out) }

The first two functions are to determine short-duration and long-duration contracts. Simply, provided a row of data and the desired constant time to expiry, the first function finds the contract with a time closest to expiry less than or equal to the desired amount, while the second function does the inverse.

The next two functions are utilized in the scenario of a function whose time to expiry is greater than the expiry of the longest trading contract. Such a synthetic would obviously not be able to be traded, but can be created for the purposes of using as an indicator. The third function gets the last two non-NA values in a row (I.E. the two last prices, the two last times to expiry), and the fourth one simply gets the rightmost non-NA value in a row.

The algorithm to create a synthetic constant-expiry contract/indicator is divided into three scenarios:

One, in which the desired time to expiry of the contract is shorter than the front month, such as a constant 30-day expiry contract, when the front month has more than 30 days to maturity (such as on Nov 17, 2016), at which point, the weight will be the desired time to expiry over the remaining time to expiry in the front month, and the remainder in spot VIX (another asset that cannot be traded, at least conventionally).

The second scenario is one in which the desired time to expiry is longer than the last traded contract. For instance, if the desire was to create a contract

with a year to expiry when the furthest out is eight months, there obviously won’t be data for such a contract. In such a case, the algorithm is to compute the linear slope between the last two available contracts, and add the extrapolated product of the slope multiplied by the time remaining between the desired and the last contract to the price of the last contract.

Lastly, the third scenario (and the most common one under most use cases) is that of the synthetic for which there is both a trading contract that has less time to expiry than the desired constant rate, and one with more time to expiry. In this instance, a matter of linear algebra (included in the comments) denotes the weight of the short expiry contract, which is (desired – expiry_long)/(expiry_short – expiry_long).

The algorithm iterates through all three scenarios, and due to the mechanics of xts automatically sorting by timestamp, one obtains an xts object in order of dates of a synthetic, constant expiry futures contract.

Here is the code for the function.

constantExpiry <- function(spotVix, termStructure, expiryStructure, daysToExpiry) { # Compute synthetics that are too long (more time to expiry than furthest contract) # can be Inf if no column contains values greater than daysToExpiry (I.E. expiry is 3000 days) suppressWarnings(longCol <- xts(apply(expiryStructure, 1, longDurMinMax, daysToExpiry), order.by=index(termStructure))) longCol[longCol == Inf] <- 10 # xts for too long to expiry -- need a NULL for rbinding if empty tooLong <- NULL # Extend the last term structure slope an arbitrarily long amount of time for those with too long expiry tooLongIdx <- index(longCol[longCol==10]) if(length(tooLongIdx) > 0) { tooLongTermStructure <- termStructure[tooLongIdx] tooLongExpiryStructure <- expiryStructure[tooLongIdx] # difference in price/expiry for longest two contracts, use it to compute a slope priceDiff <- xts(apply(tooLongTermStructure, 1, getLastDiff), order.by = tooLongIdx) expiryDiff <- xts(apply(tooLongExpiryStructure, 1, getLastDiff), order.by = tooLongIdx) slope <- priceDiff/expiryDiff # get longest contract price and compute additional days to expiry from its time to expiry # I.E. if daysToExpiry is 180 and longest is 120, additionalDaysToExpiry is 60 maxDaysToExpiry <- xts(apply(tooLongExpiryStructure, 1, max, na.rm = TRUE), order.by = tooLongIdx) longestContractPrice <- xts(apply(tooLongTermStructure, 1, getLastValue), order.by = tooLongIdx) additionalDaysToExpiry <- daysToExpiry - maxDaysToExpiry # add slope multiplied by additional days to expiry to longest contract price tooLong <- longestContractPrice + additionalDaysToExpiry * slope } # compute synthetics that are too short (less time to expiry than shortest contract) # can be -Inf if no column contains values less than daysToExpiry (I.E. expiry is 5 days) suppressWarnings(shortCol <- xts(apply(expiryStructure, 1, shortDurMinMax, daysToExpiry), order.by=index(termStructure))) shortCol[shortCol == -Inf] <- 0 # xts for too short to expiry -- need a NULL for rbinding if empty tooShort <- NULL tooShortIdx <- index(shortCol[shortCol==0]) if(length(tooShortIdx) > 0) { tooShort <- termStructure[,1] * daysToExpiry/expiryStructure[,1] + spotVix * (1 - daysToExpiry/expiryStructure[,1]) tooShort <- tooShort[tooShortIdx] } # compute everything in between (when time to expiry is between longest and shortest) # get unique permutations for contracts that term structure can create colPermutes <- cbind(shortCol, longCol) colnames(colPermutes) <- c("short", "long") colPermutes <- colPermutes[colPermutes$short > 0,] colPermutes <- colPermutes[colPermutes$long < 10,] regularSynthetics <- NULL # if we can construct synthetics from regular futures -- someone might enter an extremely long expiry # so this may not always be the case if(nrow(colPermutes) > 0) { # pasting long and short expiries into a single string for easier subsetting shortLongPaste <- paste(colPermutes$short, colPermutes$long, sep="_") uniqueShortLongPaste <- unique(shortLongPaste) regularSynthetics <- list() for(i in 1:length(uniqueShortLongPaste)) { # get unique permutation of short-expiry and long-expiry contracts permuteSlice <- colPermutes[which(shortLongPaste==uniqueShortLongPaste[i]),] expirySlice <- expiryStructure[index(permuteSlice)] termStructureSlice <- termStructure[index(permuteSlice)] # what are the parameters? shortCol <- unique(permuteSlice$short); longCol <- unique(permuteSlice$long) # computations -- some linear algebra # S/L are weights, ex_S/ex_L are time to expiry # D is desired constant time to expiry # S + L = 1 # L = 1 - S # S + (1-S) = 1 # # ex_S * S + ex_L * (1-S) = D # ex_S * S + ex_L - ex_L * S = D # ex_S * S - ex_L * S = D - ex_L # S(ex_S - ex_L) = D - ex_L # S = (D - ex_L)/(ex_S - ex_L) weightShort <- (daysToExpiry - expirySlice[, longCol])/(expirySlice[, shortCol] - expirySlice[, longCol]) weightLong <- 1 - weightShort syntheticValue <- termStructureSlice[, shortCol] * weightShort + termStructureSlice[, longCol] * weightLong regularSynthetics[[i]] <- syntheticValue } regularSynthetics <- do.call(rbind, regularSynthetics) } out <- rbind(tooShort, regularSynthetics, tooLong) colnames(out) <- paste0("Constant_", daysToExpiry) return(out) }

And here’s how to use it:

constant30 <- constantExpiry(spotVix = vixSpot, termStructure = termStructure, expiryStructure = expiryStructure, daysToExpiry = 30) constant180 <- constantExpiry(spotVix = vixSpot, termStructure = termStructure, expiryStructure = expiryStructure, daysToExpiry = 180) constantTermStructure <- cbind(constant30, constant180) chart.TimeSeries(constantTermStructure, legend.loc = 'topright', main = "Constant Term Structure")

With the result:

Which means that between the CBOE data itself, and this function that creates constant expiry futures from CBOE spot and futures prices, one can obtain any futures contract, whether real or synthetic, to use as an indicator for volatility trading strategies. This allows for exploration of a wide variety of volatility trading strategies.

Thanks for reading.

NOTE: I am always interested in networking and hearing about full-time opportunities related to my skill set. My linkedin can be found here.

Furthermore, if you are a volatility ETF/futures trading professional, I am interested in possible project-based collaboration. If you are interested, please contact me.

**leave a comment**for the author, please follow the link and comment on their blog:

**R – QuantStrat TradeR**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.