Creating Calendars for Future’s Expiration

[This article was first published on R – Quintuitive, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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:

goldo-exp

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:

require(RQuantLib)

# 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.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)