imanuelcostigan / fmdates

Financial Market Date Calculations
https://imanuelcostigan.github.io/fmdates/
10 stars 5 forks source link

Investigate qlcal-r #19

Open imanuelcostigan opened 2 years ago

imanuelcostigan commented 2 years ago

qlcal is a newish R package that interfaces with QuantLib's calendaring functions. This might massively reduce the coding footprint of fmdates (or remove its need for existence entirely).

imanuelcostigan commented 2 years ago

Investigation results:

Localising calendar in qlcal

qlcal::setCalendar("TARGET")
qlcal::getHolidays(as.Date("2022-01-20"), as.Date("2022-01-28"))
#> Date of length 0

# See if setCalendar is isolated within a function call
getHolsGlobal <- function(start, end, calendar) {
  qlcal::setCalendar(calendar)
  qlcal::getHolidays(start, end, FALSE)
}
getHolsGlobal(as.Date("2022-01-20"), as.Date("2022-01-28"), "Australia")
#> [1] "2022-01-26"
qlcal::getName() # > Nope
#> [1] "Australia"

qlcal::setCalendar("TARGET") 
# See if setCelendar can be isolated within local().
getHolsLocal <- function(start, end, calendar) {
  local({
    qlcal::setCalendar(calendar)
    qlcal::getHolidays(start, end, FALSE)
  })
}
getHolsLocal(as.Date("2022-01-20"), as.Date("2022-01-28"), "Australia")
#> [1] "2022-01-26"
qlcal::getName() # > Nope
#> [1] "Australia"

Performance benchmark for adjust

qlcal::setCalendar("Australia")
bench::mark(
  fmdates::adjust(as.Date("2012-01-26"), "f", fmdates::AUSYCalendar()),
  qlcal::adjust(as.Date("2012-01-26"), "Following")
)
#> # A tibble: 2 × 6
#>   expression                                                                min
#>   <bch:expr>                                                           <bch:tm>
#> 1 fmdates::adjust(as.Date("2012-01-26"), "f", fmdates::AUSYCalendar())   2.36ms
#> 2 qlcal::adjust(as.Date("2012-01-26"), "Following")                     20.66µs
#> # … with 4 more variables: median <bch:tm>, itr/sec <dbl>, mem_alloc <bch:byt>,
#> #   gc/sec <dbl>

Session

sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.1.1 (2021-08-10)
#>  os       macOS Monterey 12.1
#>  system   aarch64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  en_AU.UTF-8
#>  ctype    en_AU.UTF-8
#>  tz       Australia/Sydney
#>  date     2021-12-31
#>  pandoc   2.14.0.3 @ /Applications/RStudio.app/Contents/MacOS/pandoc/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  backports     1.4.1   2021-12-13 [1] RSPM (R 4.1.1)
#>  cli           3.1.0   2021-10-27 [1] CRAN (R 4.1.1)
#>  crayon        1.4.2   2021-10-29 [1] CRAN (R 4.1.1)
#>  digest        0.6.29  2021-12-01 [1] CRAN (R 4.1.1)
#>  ellipsis      0.3.2   2021-04-29 [1] CRAN (R 4.1.0)
#>  evaluate      0.14    2019-05-28 [1] CRAN (R 4.1.0)
#>  fansi         0.5.0   2021-05-25 [1] RSPM (R 4.1.0)
#>  fastmap       1.1.0   2021-01-25 [1] CRAN (R 4.1.0)
#>  fs            1.5.2   2021-12-08 [1] CRAN (R 4.1.1)
#>  glue          1.6.0   2021-12-17 [1] RSPM (R 4.1.1)
#>  highr         0.9     2021-04-16 [1] CRAN (R 4.1.0)
#>  htmltools     0.5.2   2021-08-25 [1] CRAN (R 4.1.1)
#>  knitr         1.37    2021-12-16 [1] RSPM (R 4.1.1)
#>  lifecycle     1.0.1   2021-09-24 [1] CRAN (R 4.1.1)
#>  magrittr      2.0.1   2020-11-17 [1] CRAN (R 4.1.0)
#>  pillar        1.6.4   2021-10-18 [1] CRAN (R 4.1.1)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.1.0)
#>  purrr         0.3.4   2020-04-17 [1] CRAN (R 4.1.0)
#>  R.cache       0.15.0  2021-04-30 [1] CRAN (R 4.1.0)
#>  R.methodsS3   1.8.1   2020-08-26 [1] CRAN (R 4.1.0)
#>  R.oo          1.24.0  2020-08-26 [1] CRAN (R 4.1.0)
#>  R.utils       2.11.0  2021-09-26 [1] CRAN (R 4.1.1)
#>  reprex        2.0.1   2021-08-05 [1] RSPM (R 4.1.1)
#>  rlang         0.4.12  2021-10-18 [1] CRAN (R 4.1.1)
#>  rmarkdown     2.11    2021-09-14 [1] CRAN (R 4.1.1)
#>  rstudioapi    0.13    2020-11-12 [1] CRAN (R 4.1.0)
#>  sessioninfo   1.2.2   2021-12-06 [1] RSPM (R 4.1.1)
#>  stringi       1.7.6   2021-11-29 [1] CRAN (R 4.1.1)
#>  stringr       1.4.0   2019-02-10 [1] CRAN (R 4.1.0)
#>  styler        1.6.2   2021-09-23 [1] CRAN (R 4.1.1)
#>  tibble        3.1.6   2021-11-07 [1] CRAN (R 4.1.1)
#>  utf8          1.2.2   2021-07-24 [1] CRAN (R 4.1.0)
#>  vctrs         0.3.8   2021-04-29 [1] CRAN (R 4.1.0)
#>  withr         2.4.3   2021-11-30 [1] CRAN (R 4.1.1)
#>  xfun          0.29    2021-12-14 [1] RSPM (R 4.1.1)
#>  yaml          2.2.1   2020-02-01 [1] CRAN (R 4.1.0)
#> 
#>  [1] /Users/imanuel/Library/R/arm64/4.1/library
#>  [2] /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Created on 2021-12-31 by the reprex package (v2.0.1)