Title: | Financial Market Date Calculations |
---|---|
Description: | Implements common date calculations relevant for specifying the economic nature of financial market contracts that are typically defined by International Swap Dealer Association (ISDA, <http://www2.isda.org>) legal documentation. This includes methods to check whether dates are business days in certain locales, functions to adjust and shift dates and time length (or day counter) calculations. |
Authors: | Imanuel Costigan [aut, cre] |
Maintainer: | Imanuel Costigan <[email protected]> |
License: | GPL-2 |
Version: | 0.1.4-99 |
Built: | 2024-07-06 03:13:54 UTC |
Source: | https://github.com/imanuelcostigan/fmdates |
One common financial markets date arithmetic requires a date needs to be
rolled to the closest business day following some convention (see
is_valid_bdc()
for further details). Such rolled dates can be determined by
calling adjust()
.
adjust(dates, bdc, calendar)
adjust(dates, bdc, calendar)
dates |
a vector of dates to adjust. |
bdc |
the business day convention used to roll the |
calendar |
an object that inherits from |
a vector of adjusted dates - good days are unadjusted
Other calendar methods: generate_schedule
,
is_good
, is_valid_bdc
,
is
, locale
,
shift
, tz
ausy <- AUSYCalendar() adjust(lubridate::ymd("20120102"), "u", ausy) adjust(lubridate::ymd("20120102"), "f", ausy) adjust(lubridate::ymd("20120102"), "mf", ausy) adjust(lubridate::ymd("20120102"), "p", ausy) adjust(lubridate::ymd("20120102"), "mp", ausy) adjust(lubridate::ymd("20120102"), "ms", ausy)
ausy <- AUSYCalendar() adjust(lubridate::ymd("20120102"), "u", ausy) adjust(lubridate::ymd("20120102"), "f", ausy) adjust(lubridate::ymd("20120102"), "mf", ausy) adjust(lubridate::ymd("20120102"), "p", ausy) adjust(lubridate::ymd("20120102"), "mp", ausy) adjust(lubridate::ymd("20120102"), "ms", ausy)
Calendars are necessary for two reasons: they define whether a calendar day
is a good business day in a given locale and they are used to store the time
zone for the locale. Calendars can correspond to a single locale (usually a
city). These inherit from the Calendar
class. The package implements a
number of calendars for key financial market locales such as
AUSYCalendar
, USNYCalendar
and EUTACalendar
(TARGET). You can
also define a joint locale using JointCalendar()
.
Calendar(locale, tz) EmptyCalendar() AUSYCalendar() AUMECalendar() CHZHCalendar() EUTACalendar() GBLOCalendar() HKHKCalendar() JPTOCalendar() NOOSCalendar() NZAUCalendar() NZWECalendar() USNYCalendar()
Calendar(locale, tz) EmptyCalendar() AUSYCalendar() AUMECalendar() CHZHCalendar() EUTACalendar() GBLOCalendar() HKHKCalendar() JPTOCalendar() NOOSCalendar() NZAUCalendar() NZWECalendar() USNYCalendar()
locale |
a four letter string representing an abbreviation of the
locale. The package uses locale representations loosely based on
UN/LOCODE (e.g.
Australia/Sydney is represented by |
tz |
the time zone associated with the given |
Calendar()
returns a function that constructs an object inheriting
from the Calendar
class. The calendar constructors provided by the
package returns an object that inherits from Calendar
.
Other calendar classes: JointCalendar
Calendar(NA, NA) # Defined: EmptyCalendar() Calendar("AUSY", "Australia/Sydney") # Defined: AUSYCalendar()
Calendar(NA, NA) # Defined: EmptyCalendar() Calendar("AUSY", "Australia/Sydney") # Defined: AUSYCalendar()
The dates
are rounded to the end of their respective months.
eom(dates)
eom(dates)
dates |
a vector of dates. |
a date vector with the same class as dates
library("lubridate") eom(ymd(20120203, 20140203))
library("lubridate") eom(ymd(20120203, 20140203))
Implements common date calculations relevant for specifying the economic nature of financial market contracts that are typically defined by International Swap Dealer Association (ISDA) legal documentation.
The key classes and methods introduced by this package are documented in
Calendar
, JointCalendar
, is_good()
, adjust()
, shift()
and
year_frac()
.
Generate a date schedule from effective_date
to
termination_date
. This code was derived from the Quantlib method
Schedule::Schedule. This can be used to generate the cash flow, fixing and
projection dates of an interest rate swap according to certain conventions.
generate_schedule(effective_date, termination_date, tenor, calendar = EmptyCalendar(), bdc = "u", stub = "short_front", eom_rule = FALSE, first_date = effective_date, last_date = termination_date)
generate_schedule(effective_date, termination_date, tenor, calendar = EmptyCalendar(), bdc = "u", stub = "short_front", eom_rule = FALSE, first_date = effective_date, last_date = termination_date)
effective_date |
the date at which the schedule begins. For example, the
effective date of a swap. This should be |
termination_date |
the date at which the schedule ends. For example, the
termination date of a swap. This should be |
tenor |
the frequency of the events for which dates are generated. For
example, |
calendar |
a |
bdc |
a string representing one of the following business day conventions: "u", "f", "mf", "p", "mp", "ms" (unadjusted, following, modified following, preceding, modified preceding and modified succeeding, resp.) |
stub |
a string representing one of the following stub types: "short_front", "short_back", "long_front", "long_back". |
eom_rule |
a logical value defining whether the end-to-end convention applies. |
first_date |
date of first payment for example. This defaults to
|
last_date |
date of last payment for example. This defaults to
|
an Interval
vector
Other calendar methods: adjust
,
is_good
, is_valid_bdc
,
is
, locale
,
shift
, tz
library (lubridate) effective_date <- ymd('20120103') termination_date <- ymd('20121203') tenor <- months(3) stub <- 'short_front' bdc <- 'mf' calendar <- AUSYCalendar() eom_rule <- FALSE generate_schedule(effective_date, termination_date, tenor, calendar, bdc, stub, eom_rule)
library (lubridate) effective_date <- ymd('20120103') termination_date <- ymd('20121203') tenor <- months(3) stub <- 'short_front' bdc <- 'mf' calendar <- AUSYCalendar() eom_rule <- FALSE generate_schedule(effective_date, termination_date, tenor, calendar, bdc, stub, eom_rule)
Calendar class checkers
is.Calendar(x) is.JointCalendar(x)
is.Calendar(x) is.JointCalendar(x)
x |
object to be tested |
TRUE
if x
inherits from Calendar
or JointCalendar
(is.Calendar
and is.JointCalendar
respectively) and FALSE
otherwise.
Other calendar methods: adjust
,
generate_schedule
, is_good
,
is_valid_bdc
, locale
,
shift
, tz
This checks whether the dates
provided are the last day of a
month.
is_eom(dates)
is_eom(dates)
dates |
a vector of dates. |
a logical vector
library("lubridate") is_eom(ymd(20110228)) # TRUE is_eom(ymd(20120229)) # TRUE
library("lubridate") is_eom(ymd(20110228)) # TRUE is_eom(ymd(20120229)) # TRUE
Checks whether dates are business days (good days) in a given locale
represented by a Calendar
.
is_good(dates, calendar)
is_good(dates, calendar)
dates |
a vector of dates |
calendar |
an object inheriting from either Calendar or JointCalendar. Dispatch to methods occurs on this argument. |
An is_good
method must be written for each calendar. The default method
returns TRUE
for all dates. Methods have been implemented for each of the
calendars inheriting from the Calendar
class - see the method's code for
more details. The method implemented for the JointCalendar
class checks
whether the supplied dates are good in each or any of the locales represented
by the joint calendar depending on the rule specified by the joint calendar.
a logical vector with TRUE
if the date is good and FALSE
if the
date is bad
Calendar
Other calendar methods: adjust
,
generate_schedule
,
is_valid_bdc
, is
,
locale
, shift
,
tz
is_good(lubridate::ymd(20160126, 20160411), AUSYCalendar()) is_good(lubridate::ymd(20160126), USNYCalendar())
is_good(lubridate::ymd(20160126, 20160411), AUSYCalendar()) is_good(lubridate::ymd(20160126), USNYCalendar())
Checks whether business day conventions are valid.
is_valid_bdc(bdc)
is_valid_bdc(bdc)
bdc |
a character vector |
The supported day conventions are:
u - unadjusted. No adjustments made to a date.
f - following. The date is adjusted to the following business day.
mf - modified following. As per following convention. However, if the following business day is in the month following the date, then the date is adjusted to the preceding business day.
p - preceding. The date is adjusted to the preceding business day.
mp - modified preceding. As per preceding convention. However, if the preceding business day is in the month prior to the date, then the date is adjusted to the following business day.
ms - modified succeeding. This convention applies to Australian bank bills. Australian bank bills' maturities defined as either early (prior to the 15th) or late month (after the 15th). If the maturity date calculated straight from a bill's term crosses either the end of the month or the 15th of the month, the bill's maturity is adjusted to the preceding business day.
a flag (TRUE
or FALSE
) if all the supplied business
day conventions are supported.
Other calendar methods: adjust
,
generate_schedule
, is_good
,
is
, locale
,
shift
, tz
Checks whether day basis conventions are valid. Supported day basis
conventions are documented in year_frac()
is_valid_day_basis(day_basis)
is_valid_day_basis(day_basis)
day_basis |
A character vector of day basis conventions. |
will return TRUE
for day_basis
elements that are any of the
following: 30/360
, 30/360us
, 30e/360
,
30e/360isda
, 30e+/360
, act/360
, act/365
and
act/actisda
. Otherwise will return FALSE
Other counter methods: actual_360
,
actual_365
,
actual_actual_isda
,
thirty_360_eu_isda
,
thirty_360_eu_plus
,
thirty_360_eu
, thirty_360_us
,
thirty_360
, year_frac
is_valid_day_basis(c("act/360", "act/365f"))
is_valid_day_basis(c("act/360", "act/365f"))
Sometimes the calendar governing a financial contract is defined by multiple
single locales. These joint calendars are represented by the JointCalendar
class.
JointCalendar(calendars, rule = all)
JointCalendar(calendars, rule = all)
calendars |
a list of at least one |
rule |
either |
an object of class JointCalendar
when using JointCalendar()
Other calendar classes: Calendar
JointCalendar(list(AUSYCalendar(), AUMECalendar()), all) JointCalendar(list(AUSYCalendar(), AUMECalendar()), any)
JointCalendar(list(AUSYCalendar(), AUMECalendar()), all) JointCalendar(list(AUSYCalendar(), AUMECalendar()), any)
Extract locale from calendars
locale(x)
locale(x)
x |
an instance of a |
a string representing the locale (e.g. "AUSY")
Other calendar methods: adjust
,
generate_schedule
, is_good
,
is_valid_bdc
, is
,
shift
, tz
locale(AUSYCalendar()) locale(c(AUSYCalendar(), AUMECalendar()))
locale(AUSYCalendar()) locale(c(AUSYCalendar(), AUMECalendar()))
The adjust()
function rolls dates to the closest good dates. This function
shifts dates by a given period and adjusting the
resulting dates to a closest good dates following the given business day
convention.
shift(dates, period, bdc = "u", calendar = EmptyCalendar(), eom_rule = TRUE)
shift(dates, period, bdc = "u", calendar = EmptyCalendar(), eom_rule = TRUE)
dates |
a vector of dates to shift and adjust |
period |
an atomic instance of the period class in the sense that only one of its slots should be non-zero. It must also only be a day, month or year period type. |
bdc |
the business day convention used to roll the |
calendar |
an object that inherits from |
eom_rule |
if one of the |
a vector of shifted dates
Other calendar methods: adjust
,
generate_schedule
, is_good
,
is_valid_bdc
, is
,
locale
, tz
library(lubridate) ausy <- AUSYCalendar() shift(ymd("20120229"), months(1), "u", ausy, FALSE) shift(ymd("20120229"), months(1), "u", ausy, TRUE)
library(lubridate) ausy <- AUSYCalendar() shift(ymd("20120229"), months(1), "u", ausy, FALSE) shift(ymd("20120229"), months(1), "u", ausy, TRUE)
Extract time zone from calendars
## S3 method for class 'Calendar' tz(x) ## S3 method for class 'JointCalendar' tz(x)
## S3 method for class 'Calendar' tz(x) ## S3 method for class 'JointCalendar' tz(x)
x |
an instance of a |
a string representing the time zone (e.g. "Australia/Sydney") or vector of time zones in the case of joint calendars
Other calendar methods: adjust
,
generate_schedule
, is_good
,
is_valid_bdc
, is
,
locale
, shift
lubridate::tz(AUSYCalendar()) lubridate::tz(c(AUSYCalendar(), AUMECalendar()))
lubridate::tz(AUSYCalendar()) lubridate::tz(c(AUSYCalendar(), AUMECalendar()))
This calculates the years between two dates using the given day basis convention.
year_frac(date1, date2, day_basis, maturity_date = NULL)
year_frac(date1, date2, day_basis, maturity_date = NULL)
date1 |
A vector of dates. This will be coerced to a |
date2 |
A vector of dates. This will be coerced to a |
day_basis |
The basis on which the year fraction is calculated. See
|
maturity_date |
a vector of dates representing the maturity date of the instrument. Only used for 30E/360 ISDA day basis. |
The order of date1
and date2
is not important. If date1
is less than date2
then the result will be non-negative. Otherwise,
the result will be negative. The parameters will be repeated with recycling
such that each parameter's length is equal to maximum length of
any of the parameters.
a numeric vector representing the number of years between
date1
and date2
.
http://en.wikipedia.org/wiki/Day_count_convention
Other counter methods: actual_360
,
actual_365
,
actual_actual_isda
,
is_valid_day_basis
,
thirty_360_eu_isda
,
thirty_360_eu_plus
,
thirty_360_eu
, thirty_360_us
,
thirty_360
require(lubridate) year_frac(ymd("2010-03-31"), ymd("2012-03-31"), "30/360us") # 2 year_frac(ymd("2010-02-28"), ymd("2012-03-31"), "act/360") # 2.116667 year_frac(ymd("2010-02-28"), ymd("2012-03-31"), "act/365") # 2.087671 year_frac(ymd("2010-02-28"), ymd("2012-03-31"), "act/actisda") # 2.086998
require(lubridate) year_frac(ymd("2010-03-31"), ymd("2012-03-31"), "30/360us") # 2 year_frac(ymd("2010-02-28"), ymd("2012-03-31"), "act/360") # 2.116667 year_frac(ymd("2010-02-28"), ymd("2012-03-31"), "act/365") # 2.087671 year_frac(ymd("2010-02-28"), ymd("2012-03-31"), "act/actisda") # 2.086998