• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
Finance Train

Finance Train

High Quality tutorials for finance, risk, data science

  • Home
  • Data Science
  • CFA® Exam
  • PRM Exam
  • Tutorials
  • Careers
  • Products
  • Login

Creating Term Structure of Futures Contracts Using R

Data Science, Derivatives

This lesson is part 7 of 15 in the course Derivatives with 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.

futures-prices-intraday-07-25-2019Download
# 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 

Term Structure of Futures

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.

Previous Lesson

‹ Different Parties in the Futures Market

Next Lesson

Contango and Backwardation ›

Join Our Facebook Group - Finance, Risk and Data Science

Posts You May Like

How to Improve your Financial Health

CFA® Exam Overview and Guidelines (Updated for 2021)

Changing Themes (Look and Feel) in ggplot2 in R

Coordinates in ggplot2 in R

Facets for ggplot2 Charts in R (Faceting Layer)

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

In this Course

  • Overview of Derivatives with R Tutorial
  • How to Create Futures Continuous Series
  • Exploring Crude Oil (CL) Future Data from Quandl in R
  • R Visualization of Statistical Properties of Future Prices
  • Comparing Futures vs Spot Prices for WTI Crude Oil
  • Different Parties in the Futures Market
  • Creating Term Structure of Futures Contracts Using R
  • Contango and Backwardation
  • Exploring Open Interest for Futures Contracts with R
  • Review of Options Contracts
  • Black Scholes Options Pricing Model in R
  • Binomial Option Pricing Model in R
  • Understanding Options Greeks
  • Options Strategy: Create Bull Call Spread with R Language
  • Options Strategy: Create Long Straddle with R Language

Latest Tutorials

    • Data Visualization with R
    • Derivatives with R
    • Machine Learning in Finance Using Python
    • Credit Risk Modelling in R
    • Quantitative Trading Strategies in R
    • Financial Time Series Analysis in R
    • VaR Mapping
    • Option Valuation
    • Financial Reporting Standards
    • Fraud
Facebook Group

Membership

Unlock full access to Finance Train and see the entire library of member-only content and resources.

Subscribe

Footer

Recent Posts

  • How to Improve your Financial Health
  • CFA® Exam Overview and Guidelines (Updated for 2021)
  • Changing Themes (Look and Feel) in ggplot2 in R
  • Coordinates in ggplot2 in R
  • Facets for ggplot2 Charts in R (Faceting Layer)

Products

  • Level I Authority for CFA® Exam
  • CFA Level I Practice Questions
  • CFA Level I Mock Exam
  • Level II Question Bank for CFA® Exam
  • PRM Exam 1 Practice Question Bank
  • All Products

Quick Links

  • Privacy Policy
  • Contact Us

CFA Institute does not endorse, promote or warrant the accuracy or quality of Finance Train. CFA® and Chartered Financial Analyst® are registered trademarks owned by CFA Institute.

Copyright © 2021 Finance Train. All rights reserved.

  • About Us
  • Privacy Policy
  • Contact Us