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