Creating Calendars for Future’s Expiration

January 17, 2016

(This article was first published on R – Quintuitive, and kindly contributed to R-bloggers)

Lately I have been doing calendar analysis of various markets (future contracts). Not an overly complicated task, but has a few interesting angles and since I haven’t seen anything similar on the Net – here we go.

The world of futures is not friendly – pretty much every contract has its own definition for expiration. Likewise with the options. Let’s take the rules for gold as an example:

Gold Future Expiration

That’s pretty straightforward. Let’s look at the options:


You get the idea – things start getting complicated quickly.

The good part is that this is a one time work. We generate a calendar once, until 2050 (choose your end date if you are planning on trading longer), and we are done.

Now, let’s talk R. :) It will be nice to get some calendar support. Done. Previously I have used the timeSeries package, which provides some functions, like isBizday. It has been a couple of years since the last time I looked into similar tasks, so I decided to check what else is around, and I had something in mind. That’s how I came to RQuantLib, which is a wrapper around the QuantLib c++ library.

Let’s deal with the futures:


# Generate all interesting dates
dates = as.Date(as.numeric(as.Date("2000-01-01")):as.numeric(as.Date("2050-01-01")))

# Get the end of month for each date
eom = getEndOfMonth("UnitedStates", dates)

# Get the last trading day, removing the last NA in the process
expi = head(eom[eom != lag.xts(eom, -1)], -1)

Rawesome! The options are a bit trickier, but nothing special – similar vector operations involving weekdays, RQuantLib’s isHoliday, and likely a loop to implement the Friday/Prior to holiday condition.

The post Creating Calendars for Future’s Expiration appeared first on Quintuitive.

To leave a comment for the author, please follow the link and comment on their blog: R – Quintuitive. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)