### Introduction

The term structure of commodities usually refers to the difference between Futures prices of different maturities at a given time point. The shape of the Futures curve is essential to commodity hedgers and speculators as Futures price serves as a forecast of future spot price. The Futures price curve contains the information about Futures supply and demand conditions. This algorithm will examine the role of term structure signals for the design of profitable trading strategies in commodity Futures markets.

### Method

#### Concepts of Futures

Before starting the algorithm, we first introduce a few concepts related to commodity Futures. Backwardation is defined as conditions when the Futures price is below the current spot price and contango as conditions when the Futures price is above the current spot price. The Futures term structure curve is a representation of the backwardation/contango rate for the different maturities of Futures contracts. This price gap between different maturity contracts is quantified as the roll return. The contango/backwardation rates are calculated by taking the price difference between the spot market price and the Futures contract price. This difference can be expressed as a percentage over the time to expiration and then annualised to calculate an annual roll return.

Where $P_{t,n}$ is the price of the nearest-to-maturity contract at time $t$. $P_{t,d}$ is the price of the distant contract at time $t$. $N_{t,n}$ is the number of days between time $t$ and the maturity of the nearby contract and $N_{t,d}$ is the number of days between time $t$ and the maturity of the distant contract.

#### Calculation of Roll Return

To calculate the roll return, first we sort the FuturesChain by expiry and select the first two contracts as the nearest-to-maturity contract and the the distant contract.

for symbol, chain in self.chains.items():
contracts = sorted(chain, key = lambda x: x.Expiry)
# R = (log(Pn) - log(Pd)) * 365 / (Td - Tn)
# R - Roll returns
# Pn - Nearest contract price
# Pd - Distant contract price
# Tn - Nearest contract expire date
# Pd - Distant contract expire date
near_contract = contracts
distant_contract = contracts[-1]
price_near = near_contract.LastPrice if near_contract.LastPrice>0 else 0.5*float(near_contract.AskPrice+near_contract.BidPrice)
price_distant = distant_contract.LastPrice if distant_contract.LastPrice>0 else 0.5*float(distant_contract.AskPrice+distant_contract.BidPrice)
if distant_contract.Expiry == near_contract.Expiry:
self.Debug("ERROR: Near and distant contracts have the same expiry!" + str(near_contract))
return
expire_range = 365 / (distant_contract.Expiry - near_contract.Expiry).days
roll_returns[symbol] = (np.log(float(price_near)) - np.log(float(price_distant)))*expire_range


#### Backwardation and Contango

In the next step, we will split the Futures based on backwardation and contango. If the roll return is greater than 0, the term structure of commodity Futures prices is downward-sloping and so that the market is in backwardation. Conversely, a negative roll return signals an upward-sloping price curve and a contangoed market.

positive_roll_returns = { symbol: returns for symbol, returns in roll_returns.items() if returns > 0 }
negative_roll_returns = { symbol: returns for symbol, returns in roll_returns.items() if returns < 0 }
backwardation = sorted(positive_roll_returns , key = lambda x: positive_roll_returns[x], reverse = True)[:quintile]
contango = sorted(negative_roll_returns , key = lambda x: negative_roll_returns[x])[:quintile]


The algorithm buys 20% of commodities with the highest roll-returns and shorts the 20% of commodities with the lowest roll-returns and holds the long-short positions for one month.

for short_symbol in contango:
sort = sorted(self.chains[short_symbol], key = lambda x: x.Expiry)
self.SetHoldings(sort.Symbol, -0.5/count)

for long_symbol in backwardation:
sort = sorted(self.chains[long_symbol], key = lambda x: x.Expiry)
self.SetHoldings(sort.Symbol, 0.5/count)


Python

C#

### Reference

1. Fuertes, Ana-Maria and Miffre, Joëlle and Rallis, Georgios, Tactical Allocation in Commodity Futures Markets: Combining Momentum and Term Structure Signals (April 22, 2010). Journal of Banking and Finance 34, 2530-2548. Online Copy
2. Quantpedia - Term Structure Effect in Commodities 