Movatterモバイル変換


[0]ホーム

URL:


Dirk Eddelbuettel

qlcal-r: QuantLibCalendaring for R

CILicenseLast CommitCRANDependenciesDownloads

qlcal brings the calendaring functionality from theQuantLib project to R.

Motivation

qlcal lets us access various global (exchange orsettlement) calendars. Here is a quick example for the NYSE in 2022where Juneteenth is making a first appearance as the most recently addedfederal holiday:

>library(qlcal)>setCalendar("UnitedStates/NYSE")>getHolidays(as.Date("2022-01-01"),as.Date("2022-12-31"))[1]"2022-01-17""2022-02-21""2022-04-15""2022-05-30""2022-06-20"[6]"2022-07-04""2022-09-05""2022-11-24""2022-12-26">

Brief History

This package started as an integration of the (somewhat experimental)Quantuccia package(see next section) to R by means ofRcpp in packageRcppQuantuccia.ButQuantuccia didnot continue beyond its initial proof of concept. So as ofRcppQuantuccia release 0.0.5, we have refocused it on anevensmaller subset ofQuantLib: just thecalendaring. So code for pricers, math, models, schedules, … that was inQuantuccia has beenremoved. The calendaring, along with all its support code, is nowcurrent with the currentQuantLib release which,as of this writing, is 1.24.

Going forward, the idea is to regroup the QuantLib calendaringfunctionality in a small and self-contained libraryqlcal,and provide frontends such as this R package.

Longer Example

Here we examine holiday lists for given calendars, specified bycountry and possibly exchange:

R>library(qlcal)R> fromD<-as.Date("2017-01-01")R> toD<-as.Date("2017-12-31")R>getHolidays(fromD, toD)# default calender ie TARGET[1]"2017-04-14""2017-04-17""2017-05-01""2017-12-25""2017-12-26"R>setCalendar("UnitedStates")R>getHolidays(fromD, toD)# US aka US::Settlement[1]"2017-01-02""2017-01-16""2017-02-20""2017-05-29""2017-07-04""2017-09-04"[7]"2017-10-09""2017-11-10""2017-11-23""2017-12-25"R>setCalendar("UnitedStates::NYSE")R>getHolidays(fromD, toD)# US New York Stock Exchange[1]"2017-01-02""2017-01-16""2017-02-20""2017-04-14""2017-05-29""2017-07-04"[7]"2017-09-04""2017-11-23""2017-12-25"R>

This shows the difference between the default US settlement calendarand the NYSE calendar which we selected explicitly.

As all calendars are now supported (and are listed in a conveniencevectorcalendars):

>library(qlcal)> calendars [1]"TARGET""UnitedStates" [3]"UnitedStates/LiborImpact""UnitedStates/NYSE" [5]"UnitedStates/GovernmentBond""UnitedStates/NERC" [7]"UnitedStates/FederalReserve""Argentina" [9]"Australia""Austria"[11]"Austria/Exchange""Bespoke"[13]"Botswana""Brazil"[15]"Brazil/Exchange""Canada"[17]"Canada/TSX""Chile"[19]"China""China/IB"[21]"CzechRepublic""Denmark"[23]"Finland""France"[25]"France/Exchange""Germany"[27]"Germany/FrankfurtStockExchange""Germany/Xetra"[29]"Germany/Eurex""Germany/Euwax"[31]"HongKong""Hungary"[33]"Iceland""India"[35]"Indonesia""Israel"[37]"Italy""Italy/Exchange"[39]"Japan""Mexico"[41]"NewZealand""Norway"[43]"Null""Poland"[45]"Romania""Russia"[47]"SaudiArabia""Singapore"[49]"Slovakia""SouthAfrica"[51]"SouthKorea""SouthKorea/KRX"[53]"Sweden""Switzerland"[55]"Taiwan""Thailand"[57]"Turkey""Ukraine"[59]"UnitedKingdom""UnitedKingdom/Exchange"[61]"UnitedKingdom/Metals""WeekendsOnly">

We can then for example quickly count number of holiday per calendar(by computing the length of the returned vector of holidays) and show ashortened print, all in a handful of lines continuing from above

> getHols<-function(cal) {# simple helper function+setCalendar(cal)+getHolidays(as.Date("2022-01-01"),as.Date("2022-12-31"))+ }> D<-data.table(calendar=calendars)> D[ ,`:=`(n =length(getHols(calendar)),+holidays =paste(format(getHols(calendar),"%d %b"),collapse=",")),+    by= calendar ]> D> D                       calendar  n                                                                     holidays1:                      TARGET315 Apr,18 Apr,26 Dec2:                UnitedStates1017 Jan,21 Feb,30 May,20 Jun,04 Jul,05 Sep,10 Oct,11 Nov,24 Nov,26 Dec3:    UnitedStates/LiborImpact1017 Jan,21 Feb,30 May,20 Jun,04 Jul,05 Sep,10 Oct,11 Nov,24 Nov,26 Dec4:           UnitedStates/NYSE917 Jan,21 Feb,15 Apr,30 May,20 Jun,04 Jul,05 Sep,24 Nov,26 Dec5: UnitedStates/GovernmentBond1117 Jan,21 Feb,15 Apr,30 May,20 Jun,04 Jul,05 Sep,10 Oct,11 Nov,24 Nov,26 Dec---58:                     Ukraine1003 Jan,07 Jan,08 Mar,25 Apr,02 May,09 May,13 Jun,28 Jun,24 Aug,14 Oct59:               UnitedKingdom903 Jan,15 Apr,18 Apr,02 May,02 Jun,03 Jun,29 Aug,26 Dec,27 Dec60:      UnitedKingdom/Exchange903 Jan,15 Apr,18 Apr,02 May,02 Jun,03 Jun,29 Aug,26 Dec,27 Dec61:        UnitedKingdom/Metals903 Jan,15 Apr,18 Apr,02 May,02 Jun,03 Jun,29 Aug,26 Dec,27 Dec62:                WeekendsOnly0>

Here we set the year to 2022 as it includes the added US holiday ofJuneteenth.

We can also access the calendar ‘name’ from the underlying (QuantLibCalendar) object:

> D[, name:= {setCalendar(calendar);getName() }, by=calendar][, .(calendar,name)]                       calendar                      name1:                      TARGET                    TARGET2:                UnitedStates             US settlement3:    UnitedStates/LiborImpact      US with Libor impact4:           UnitedStates/NYSE   New York stock exchange5: UnitedStates/GovernmentBond US government bond market---58:                     Ukraine  Ukrainian stock exchange59:               UnitedKingdom             UK settlement60:      UnitedKingdom/Exchange     London stock exchange61:        UnitedKingdom/Metals    London metals exchange62:                WeekendsOnly             weekends only>

Installation

The package can be installed fromCRAN via

install.packages("qlcal")

or if you prefer non-release development version these can beinstalled from GitHub via e.g.

remotes::install_github("qlcal/qlcal-r")

It only requiresRcpp andBH both of whichare available whereeverR itself runs.

Authors

Dirk Eddelbuettel for the package and integration.

The authors and contributors of QuantLib for the underlyingcalendaring code.

License

GPL (>= 2)

Initially created: Wed Dec 08 19:57:59 CDT 2021
Last modified: Sun May 26 10:02:51 CDT 2024


[8]
ページ先頭

©2009-2025 Movatter.jp