Over-the-counter (OTC) derivatives comprise a significant proportion of trading activity in global financial markets. Their general contractual conventions are specified in what are known as International Swap and Derivatives Association (ISDA) definitions. For example, FX and currency option transactions are governed by the 1998 FX and Currency Options Definitions and swap transactions are governed by the 2006 ISDA Definitions. They describe in meticulous detail, among other things, how the dates of certain financial events should be determined. This includes how dates are defined to be good or bad and how bad dates are to be adjusted to good dates. They also define how to determine the length of time between two dates.

## Calendars

The Calendar class is extended by city specific calendar classes (e.g. USNYCalendar). The functions that create such city calendars have a name prefixed by four upper case letters: the first two being a country code (e.g. US) and the latter two being a city code (e.g. NY). Supported calendars are documented in ?Calendar.

Calendars are associated with three key methods: is_good, adjust and shift. These are described below.

## Good day functions

The is_good() method determines whether a date or vector of dates is a business day in a given locale (usually a city). Some examples of its use are:

library("lubridate")
#>
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#>
#>     date
library("fmdates")
ausy <- AUSYCalendar()
aume <- AUMECalendar()
syme <- c(ausy, aume) # handy JointCalendar construction approach
is_good(ymd(20140404), ausy)
#> [1] TRUE
is_good(ymd(20141104), syme) # Melbourne Cup holiday
#> [1] FALSE
syme\$rule <- any
is_good(ymd(20141104), syme)
#> [1] TRUE

There are a number of different ways to adjust a bad day to a good day using adjust(). Some examples include:

adjust(ymd(20140404), 'mf', ausy)
#> [1] "2014-04-04"
adjust(ymd(20141130), 'mf', ausy)
#> [1] "2014-11-28"
adjust(ymd(20141101), 'mp', ausy)
#> [1] "2014-11-03"
adjust(ymd(20141115), 'ms', ausy)
#> [1] "2014-11-14"

## Shifters

Occasionally it is necessary to shift a date by a certain period or to a certain epoch. The can be done by using shift().

shift(ymd(20120229), days(2), 'u', ausy, FALSE) # two good days
#> [1] "2012-03-02"
shift(ymd(20120229), months(1), 'u', ausy, FALSE) # one month
#> [1] "2012-03-29"
shift(ymd(20120229), months(1), 'mf', ausy, TRUE)  # one month with EOM rule
#> [1] "2012-03-30"
shift(ymd(20120229), years(1) + months(3), 'mf', ausy, TRUE)  # 1y3m
#> [1] "2013-05-31"
shift(ymd(20120229), hours(1), 'mf', ausy, TRUE)  # periods < days ignored.
#> The following period values are not supported by shift and are ignored: hour
#> [1] "2012-02-29"

## Date schedules

With the aforementioned machinery, it is now possible to determine a schedule of dates that are required for a given financial contract. For example, counterparties to a vanilla interest rate swaps exchanged fixed rate payments in return for floating rate payments with regular frequency during the life of the contract. These form payment periods which can be determined using generate_schedule().

# No stub
generate_schedule(effective_date = ymd(20120103), termination_date = ymd(20130103),
tenor = months(3), calendar = AUSYCalendar(), bdc = "mf", stub = "short_front",
eom_rule = FALSE)
#> [1] 2012-01-03 UTC--2012-04-03 UTC 2012-04-03 UTC--2012-07-03 UTC
#> [3] 2012-07-03 UTC--2012-10-03 UTC 2012-10-03 UTC--2013-01-03 UTC
# Front stub
generate_schedule(effective_date = ymd(20120103), termination_date = ymd(20121203),
tenor = months(3), calendar = AUSYCalendar(), bdc = "mf", stub = "short_front",
eom_rule = FALSE)
#> [1] 2012-01-03 UTC--2012-03-05 UTC 2012-03-05 UTC--2012-06-04 UTC
#> [3] 2012-06-04 UTC--2012-09-03 UTC 2012-09-03 UTC--2012-12-03 UTC

## Day count fractions

It is possible to determine the length of a period. This known as the day count fraction, year fraction or day count. The package has implemented a number of common day counters (see ?year_frac).

year_frac(ymd("2010-03-31"), ymd("2012-03-31"), "30/360us")
#> [1] 2
year_frac(ymd("2010-02-28"), ymd("2012-03-31"), "act/365")
#> [1] 2.087671