Calendars#

The rateslib.calendars module generates holiday calendars so that business days are well defined. It is built upon the pandas holiday calendars methods, which are themselves extensions of numpy data structures.

Summary#

Methods#

rateslib.calendars.create_calendar(rules[, ...])

Create a calendar with specific business and holiday days defined.

rateslib.calendars.get_calendar(calendar[, kind])

Returns a calendar object either from an available set or a user defined input.

rateslib.calendars.add_tenor(start, tenor, ...)

Add a tenor to a given date under specific modification rules and holiday calendar.

rateslib.calendars.dcf(start, end, convention)

Calculate the day count fraction of a period.

Loading existing calendars#

Warning

Use preset calendars at your own risk. Generally the repeated yearly holidays are accurate but a full list of ad-hoc and specialised holidays has not been properly reviewed and is not necessarily upto date.

The get_calendar() method is used internally to parse the different options a user might provide, e.g. supplying NoInput and then generating a null calendar object with no holidays or passing through a user defined calendar. There are also some calendars pre-programmed, and those currently available calendars are below. More information is available in the get_calendar() method:

In [1]: from rateslib.calendars import CALENDARS

In [2]: print(CALENDARS.keys())
dict_keys(['bus', 'tgt', 'ldn', 'nyc', 'stk', 'osl', 'zur', 'tro'])
In [3]: ldn_cal = get_calendar("ldn")

In [4]: date_range(start=dt(2022, 12, 23), end=dt(2023, 1, 9), freq=ldn_cal)
Out[4]: DatetimeIndex(['2022-12-23', '2022-12-28', '2022-12-29', '2022-12-30', '2023-01-03', '2023-01-04', '2023-01-05', '2023-01-06', '2023-01-09'], dtype='datetime64[ns]', freq='C')

In [5]: stk_cal = get_calendar("stk")

In [6]: date_range(start=dt(2022, 12, 23), end=dt(2023, 1, 9), freq=stk_cal)
Out[6]: DatetimeIndex(['2022-12-23', '2022-12-27', '2022-12-28', '2022-12-29', '2022-12-30', '2023-01-02', '2023-01-03', '2023-01-04', '2023-01-05', '2023-01-09'], dtype='datetime64[ns]', freq='C')

Available calendars can also be combined if a comma separator is used in the argument, which acts as an AND operator for business days and an OR operator for holidays. This is useful for multi-currency derivatives.

In [7]: ldn_stk_cal = get_calendar("ldn,stk")

In [8]: date_range(start=dt(2022, 12, 23), end=dt(2023, 1, 9), freq=ldn_stk_cal)
Out[8]: DatetimeIndex(['2022-12-23', '2022-12-28', '2022-12-29', '2022-12-30', '2023-01-03', '2023-01-04', '2023-01-05', '2023-01-09'], dtype='datetime64[ns]', freq='C')

Creating a custom calendar#

The create_calendar() method is provided to allow a user to create their own custom calendar defined by a weekmask and specific holidays. For example, suppose one wanted to create a holiday calendar that included weekends and Christmas every year and New Year’s Day rolled forward to a monday if it happened to fall on a weekend. The approach is as follows,

In [9]: from pandas import date_range

In [10]: from pandas.tseries.holiday import Holiday, next_monday

In [11]: holidays = [
   ....:     Holiday("Christmas", month=12, day=25),
   ....:     Holiday("New Year's", month=1, day=1, observance=next_monday),
   ....: ]
   ....: 

In [12]: custom_cal = create_calendar(holidays, "Mon Tue Wed Thu Fri")

In [13]: date_range(start=dt(2022, 12, 23), end=dt(2023, 1, 5), freq=custom_cal)
Out[13]: DatetimeIndex(['2022-12-23', '2022-12-26', '2022-12-27', '2022-12-28', '2022-12-29', '2022-12-30', '2023-01-03', '2023-01-04', '2023-01-05'], dtype='datetime64[ns]', freq='C')

Day count fractions (DCFs)#

This module also contains a dcf() method for calculating day count fractions. Review the API documentation for specific calculation details. Current DCF conventions available are listed below:

In [14]: from rateslib.calendars import _DCF

In [15]: print(_DCF.keys())
dict_keys(['ACT365F', 'ACT365F+', 'ACT360', '30360', '360360', 'BONDBASIS', '30E360', 'EUROBONDBASIS', '30E360ISDA', 'ACTACT', 'ACTACTISDA', 'ACTACTICMA', 'ACTACTICMA_STUB365F', 'ACTACTISMA', 'ACTACTBOND', '1', '1+'])