- 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
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.
# 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
Related Downloads
Data Science in Finance: 9-Book Bundle
Master R and Python for financial data science with our comprehensive bundle of 9 ebooks.
What's Included:
- Getting Started with R
- R Programming for Data Science
- Data Visualization with R
- Financial Time Series Analysis with R
- Quantitative Trading Strategies with R
- Derivatives with R
- Credit Risk Modelling With R
- Python for Data Science
- Machine Learning in Finance using Python
Each book includes PDFs, explanations, instructions, data files, and R code for all examples.
Get the Bundle for $29 (Regular $57)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.