Continuous Futures Contracts for Backtesting Purposes

In a previous article on QuantStart we investigated how to download free futures data from Quandl. In this article we are going to discuss the characteristics of futures contracts that present a data challenge from a backtesting point of view. In particular, the notion of the "continuous contract" and "roll returns". We will outline the main difficulties of futures and provide an implementation in Python with pandas that can partially alleviate the problems.

Futures are a form of *contract* drawn up between two parties for the purchase or sale of a quantity of an underlying asset at a specified date in the future. This date is known as the *delivery* or *expiration*. When this date is reached the buyer must deliver the physical underlying (or cash equivalent) to the seller for the price agreed at the contract formation date.

In practice futures are traded on exchanges (as opposed to *Over The Counter* - OTC trading) for standardised quantities and qualities of the underlying. The prices are *marked to market* every day. Futures are incredibly liquid and are used heavily for speculative purposes. While futures were often utilised to hedge the prices of agricultural or industrial goods, a futures contract can be formed on any tangible or intangible underlying such as stock indices, interest rates of foreign exchange values.

A detailed list of all the symbol codes used for futures contracts across various exchanges can be found on the CSI Data site: Futures Factsheet.

The main difference between a futures contract and equity ownership is the fact that a futures contract has a limited window of availability by virtue of the expiration date. At any one instant there will be a variety of futures contracts on the same underlying all with varying dates of expiry. The contract with the nearest date of expiry is known as the *near contract*. The problem we face as quantitative traders is that at any point in time we have a choice of multiple contracts with which to trade. Thus we are dealing with an overlapping set of time series rather than a continuous stream as in the case of equities or foreign exchange.

The goal of this article is to outline various approaches to constructing a continuous stream of contracts from this set of multiple series and to highlight the tradeoffs associated with each technique.

The main difficulty with trying to generate a continuous contract from the underlying contracts with varying deliveries is that the contracts do not often trade at the same prices. Thus situations arise where they do not provide a smooth splice from one to the next. This is due to contango and backwardation effects. There are various approaches to tackling this problem, which we now discuss.

Unfortunately there is no single "standard" method for joining futures contracts together in the financial industry. Ultimately the method chosen will depend heavily upon the strategy employing the contracts and the method of execution. Despite the fact that no single method exists there are some common approaches:

This method alleviates the "gap" across multiple contracts by shifting each contract such that the individual deliveries join in a smooth manner to the adjacent contracts. Thus the open/close across the prior contracts at expiry matches up.

The key problem with the Panama method includes the introduction of a trend bias, which will introduce a large drift to the prices. This can lead to negative data for sufficiently historical contracts. In addition there is a loss of the *relative* price differences due to an absolute shift in values. This means that returns are complicated to calculate (or just plain incorrect).

The Proportionality Adjustment approach is similar to the adjustment methodology of handling stock splits in equities. Rather than taking an absolute shift in the successive contracts, the ratio of the older settle (close) price to the newer open price is used to proportionally adjust the prices of historical contracts. This allows a continous stream without an interruption of the calculation of percentage returns.

The main issue with proportional adjustment is that any trading strategies reliant on an absolute price level will also have to be similarly adjusted in order to execute the correct signal. This is a problematic and error-prone process. Thus this type of continuous stream is often only useful for summary statistical analysis, as opposed to direct backtesting research.

The essence of this approach is to create a continuous contract of successive contracts by taking a linearly weighted proportion of each contract over a number of days to ensure a smoother transition between each.

For example consider five smoothing days. The price on day 1, $P_1$, is equal to 80% of the far contract price ($F_1$) and 20% of the near contract price ($N_1$). Similarly, on day 2 the price is $P_2 = 0.6 \times F_2 + 0.4 \times N_2$. By day 5 we have $P_5 = 0.0 \times F_5 + 1.0 \times N_5 = N_5$ and the contract then just becomes a continuation of the near price. Thus after five days the contract is smoothly transitioned from the far to the near.

The problem with the rollover method is that it requires trading on all five days, which can increase transaction costs.

There are other less common approaches to the problem but we will avoid them here.

The remainder of the article will concentrate on implementing the perpetual series method as this is most appropriate for backtesting. It is a useful way to carry out *strategy pipeline research*.

We are going to stitch together the WTI Crude Oil "near" and "far" futures contract (symbol CL) in order to generate a continuous price series. At the time of writing (January 2014), the near contract is CLF2014 (January) and the far contract is CLG2014 (February).

In order to carry out the download of futures data I've made use of the Quandl plugin. Make sure to set the correct Python virtual environment on your system and install the Quandl package by typing the following into the terminal:

pip install Quandl

Now that the Quandl package is intalled, we need to make use of NumPy and pandas in order to carry out the roll-returns construction. If you haven't got NumPy or pandas installed, please follow my tutorial here. Create a new file and enter the following import statements:

import datetime import numpy as np import pandas as pd import Quandl

The main work is carried out in the `futures_rollover_weights`

function. It requires a starting date (the first date of the near contract), a dictionary of contract settlement dates (`expiry_dates`

), the symbols of the contracts and the number of days to roll the contract over (defaulting to five). The comments below explain the code:

def futures_rollover_weights(start_date, expiry_dates, contracts, rollover_days=5): """This constructs a pandas DataFrame that contains weights (between 0.0 and 1.0) of contract positions to hold in order to carry out a rollover of rollover_days prior to the expiration of the earliest contract. The matrix can then be 'multiplied' with another DataFrame containing the settle prices of each contract in order to produce a continuous time series futures contract.""" # Construct a sequence of dates beginning from the earliest contract start # date to the end date of the final contract dates = pd.date_range(start_date, expiry_dates[-1], freq='B') # Create the 'roll weights' DataFrame that will store the multipliers for # each contract (between 0.0 and 1.0) roll_weights = pd.DataFrame(np.zeros((len(dates), len(contracts))), index=dates, columns=contracts) prev_date = roll_weights.index[0] # Loop through each contract and create the specific weightings for # each contract depending upon the settlement date and rollover_days for i, (item, ex_date) in enumerate(expiry_dates.iteritems()): if i < len(expiry_dates) - 1: roll_weights.ix[prev_date:ex_date - pd.offsets.BDay(), item] = 1 roll_rng = pd.date_range(end=ex_date - pd.offsets.BDay(), periods=rollover_days + 1, freq='B') # Create a sequence of roll weights (i.e. [0.0,0.2,...,0.8,1.0] # and use these to adjust the weightings of each future decay_weights = np.linspace(0, 1, rollover_days + 1) roll_weights.ix[roll_rng, item] = 1 - decay_weights roll_weights.ix[roll_rng, expiry_dates.index[i+1]] = decay_weights else: roll_weights.ix[prev_date:, item] = 1 prev_date = ex_date return roll_weights

Now that the weighting matrix has been produced, it is possible to apply this to the individual time series. The main function downloads the near and far contracts, creates a single DataFrame for both, constructs the rollover weighting matrix and then finally produces a continuous series of both prices, appropriately weighted:

if __name__ == "__main__": # Download the current Front and Back (near and far) futures contracts # for WTI Crude, traded on NYMEX, from Quandl.com. You will need to # adjust the contracts to reflect your current near/far contracts # depending upon the point at which you read this! wti_near = Quandl.get("OFDP/FUTURE_CLF2014") wti_far = Quandl.get("OFDP/FUTURE_CLG2014") wti = pd.DataFrame({'CLF2014': wti_near['Settle'], 'CLG2014': wti_far['Settle']}, index=wti_far.index) # Create the dictionary of expiry dates for each contract expiry_dates = pd.Series({'CLF2014': datetime.datetime(2013, 12, 19), 'CLG2014': datetime.datetime(2014, 2, 21)}).order() # Obtain the rollover weighting matrix/DataFrame weights = futures_rollover_weights(wti_near.index[0], expiry_dates, wti.columns) # Construct the continuous future of the WTI CL contracts wti_cts = (wti * weights).sum(1).dropna() # Output the merged series of contract settle prices wti_cts.tail(60)

The output is as follows:

2013-10-14 102.230 2013-10-15 101.240 2013-10-16 102.330 2013-10-17 100.620 2013-10-18 100.990 2013-10-21 99.760 2013-10-22 98.470 2013-10-23 97.000 2013-10-24 97.240 2013-10-25 97.950 .. .. 2013-12-24 99.220 2013-12-26 99.550 2013-12-27 100.320 2013-12-30 99.290 2013-12-31 98.420 2014-01-02 95.440 2014-01-03 93.960 2014-01-06 93.430 2014-01-07 93.670 2014-01-08 92.330 Length: 60, dtype: float64

It can be seen that the series is now continuous across the two contracts. The next step is to carry this out for multiple deliveries across a variety of years, depending upon your backtesting needs.

If you would like more detail in forming continuous series of futures prices then please have a look at the following links:

- http://www.seykota.com/tribe/TSP/Continuous/index.htm
- http://www.ipedr.com/vol29/48-CEBMM2012-R00003.pdf
- http://www.automated-trading-system.com/what-everybody-ought-to-know-about-continous-futures-contracts/

You'll get instant access to a free 10-part email course packed with hints and tips to help you get started in quantitative trading!

Every week I'll send you a wrap of all activity on QuantStart so you'll never miss a post again.

Real, actionable quant trading tips with no nonsense.