Creating Term Structure of Futures Contracts Using R

The terms structure of the futures contracts for a specific asset is generated by the contracts prices at different expiration times. The contracts have different prices on each expiration date and there is an underlying relationship between them that is reflected with different shapes. The most common shapes are called normal or inverted forms. These shapes reflect the expectation of market participants from the asset.

The normal form is the most common structure of the contracts future prices at different maturities.  This shape is characterized by a positive relationship between futures prices and expiration dates, that is, the price of the futures contracts increases with its expiration time. Also this curve reveals factors such as supply and demand of the underlying assets when this is a commodity. 

In the inverted curve shape, distant future prices are lower than nearby future prices. This type of curve takes place when there are imbalances between supply and demand on a commodity. In periods of shortage in the supply of a commodity, producers go to the futures market in order to enter in a long position to secure short term prices and nearby expiration contracts go up in prices. 

The degree of the slope in the term structure, both in a normal or inverted market, is a signal about the expected future volatility of the asset. An important concept that arises from the term structure of futures contracts is the spread. This is the price difference between futures contracts from different maturities.

It is common that traders form spread positions by holding two different contracts in which they expect that the spread would narrow or widen in the future and can get profits on this prediction.  

To observe these patterns with real word data, we would get Gold futures prices data from a CSV file and explore the term structure of Gold Futures contracts. We start by reading the CSV file containing the futures contract prices of gold for the next periods, make some cleaning operations and finally plot the term structure.

Downloads
# R by default assign a factor data type for dates, so we need to pass the parameter # stringsAsFactor = False, when read a csv in order to read dates as character type.

goldPrices <- read.csv("C:/Users/Nicolas/Downloads/futures-prices-intraday-07-25-2019.csv",stringsAsFactors = FALSE)

# Remove one row that have all Na values with complete.cases command

goldPrices <- goldPrices[complete.cases(goldPrices),]

# Observe the first rows of the goldPrices dataframe

head(goldPrices)

#The following step consist of creating two new columns with the values of the #”Contract” column. These columns would have separately the contract code, and the expiration date.
# Remove the character ' from Contract column with gsub() command that is useful 
# for string  substitution.

goldPrices$Contract <- gsub("'","",goldPrices$Contract)
 
# Split the Contract column into two columns that are called ContractCode and #ExpiryDate. This task is performed with the separate command from dplyr #package. 

goldPrices <- goldPrices %>% separate(Contract,c('ContractCode','ExpiryDate'),sep='\\(')

# Clean a bit more the ExpiryDate column by removing the ‘)’

goldPrices$ExpiryDate <- gsub(')','',goldPrices$ExpiryDate)

head(goldPrices)

# Plot the term structure of futures Gold contracts

plot(goldPrices$Last,xaxt='n',xlab='Expiration',main='Term Structure Gold Future Prices Jun 2019-Jun 2025',ylab='LastPrice',col='blue',pch = 19)
axis(1, at=seq_along(goldPrices$Last),tck=.01, cex.axis=0.9,srt=45,labels=as.character(goldPrices$ExpiryDate),las=2)

Gold Future Contracts Term Structure 

We observe a normal curve between the contract prices of Gold Futures and expiration times. This means that there is a positive correlation between future prices and expiration times. For longer expiration times such as contracts that expire after December 2021, the steepness of the curve is greater than for contracts that expire earlier.

Lesson Resources

All Users

Related Downloads

Membership
Learn the skills required to excel in data science and data analytics covering R, Python, machine learning, and AI.
I WANT TO JOIN
JOIN 30,000 DATA PROFESSIONALS

Free Guides - Getting Started with R and Python

Enter your name and email address below and we will email you the guides for R programming and Python.

Saylient AI Logo

Take the Next Step in Your Data Career

Join our membership for lifetime unlimited access to all our data analytics and data science learning content and resources.