Continuous Futures Prices in R

Introduction

Crude oil prices by delivery period define the term structure of the market. The term structure changes shape over time given shifts in price level and slope.  Term structure behavior becomes clear by combining discrete futures contracts with similar maturities into a continuous time series.  R code is supplied to create continuous prices by delivery period. The purpose is to show term structure behavior and to derive risk and profitability measures for oil production, marketing and trading strategies. The resulting data is tidy, well suited for model training and out-of-sample testing.

Data Source and Scope

Professional grade futures prices are sourced from Steven’s Analytics via Quandl. Data for crude oil futures has daily granularity and spans all contracts from June 1983 to December 2027.  Continuous settlement prices are created for 36 forward months.  Time series plots and an MP4 animation  illustrate observed term structure behavior.  Finally, data objects for the first 12 maturities are saved with OHLS data. 1

Project Template and Source Code

A project template provides directories and source code for work space configuration, data downloads and management. The template seeks repeatable results across machines and over time. The project template can be downloaded here:

Project Template for Continuous Futures Prices (52 downloads)

Dependencies

The project work space uses the following configuration:

Data Downloading via Quandl

Quandl online provides Steven’s Reference Futures (SRF) data. The generic function below will extract a list of futures contracts; manage and transform the data for tidy work flows; and load the resulting data into a project cache.

Function Wrapper

The following code is a wrapper around the generic download get_futures() function. The example code downloads NYMEX crude oil data. The script gets data for a target date range.  As applied here, the code gets data for over 500 futures contracts. The wrapper is lightning fast and relies on the purrr::map() function in R. The vectorized operation is well specified and will extract, transform and load all contracts in the absence of looping.

Term Structure Components

The download process has filled the project cache. The next step is to manage the term structure components through basic data transformations. This first step is to combine all contracts into the object price.dat, where rows are observation dates and columns are individual futures contract. The initial data object will have many N/A values since the date range for any one contract is short, while the range for all downloaded contracts is much larger (July 1983 to January 2019 in this example).

Next, inspect price.dat and identify bad data where term structure components are missing due to holidays or bad price settlements.

The following plot confirms the number of listed futures contracts as a function of date:

 

Meanwhile, its interesting to note that NYMEX ended the open open outcry process for trading crude oil on December 30, 2016 because of increased volumes traded online.

Continuous Futures Price Series

The following code creates continuous time series by maturity, where the maturity term is specified by the user with the nterm parameter. The example code here sets nterm=36 , consistent with the open interest profile of the market.

Graph Analysis

The plot profiles the term structure of crude oil on the last day of the data sample:

Next, the spot or first nearby contract price is shown:

The term structure defines market conditions.  The graph profiles the first NBY contract price less the sixth NBY contract. The plot defines backwardated markets with positive values and contango markets with negative values:
The code block below creates the graphs shown:

Term Structure Animation

Last, the following section presents an animation of the crude oil term structure for calendar year 2018:

The animation code creates an MP4 movie with a scroll bar to control animation speed and direction.  In practice, the code makes an external system call to the ImageMagick application, which should be installed first:

  1. OHLS data includes open-high-low-settle, volume and open interest
This entry was posted in Data, R Programming. Bookmark the permalink.