Calculate German business days in specific month w/o RQuantLib

74 Views Asked by At

I have an issue calculating business days in Germany in a specific time frame. Meaning, I want to exclude public holidays, Saturday and Sunday. I cannot use RQuantLib or bizdays packages as they cannot be installed on the instance where I run the code. Is there an alternative to this:

#Calculate workdays
load_quantlib_calendars("Germany", from="2022-01-01", to="2026-01-01")
final_df <- final_df %>%
  mutate(first_day = as.Date(paste0(as.numeric(format(Sys.Date(), "%Y")), "-", `Calendar month` ,"-01")),
         last_day =  ceiling_date(as.Date(paste0(as.numeric(format(Sys.Date(), "%Y")), "-", `Calendar month` ,"-01")), 
                                  unit = "month", 
                                  change_on_boundary = TRUE)-1,
         `Workdays month Act` = bizdays(first_day, last_day, "QuantLib/Germany"),
         `Workdays month PY` =  bizdays(first_day-years(1), last_day-years(1), "QuantLib/Germany"))%>%
  select(-first_day, -last_day)`

timeDate package seems to cover only a few German holidays and even some of them are wrong e.g. 31st December is not public holiday and 1.1. is missing.

So important for me is that there is a proper library with the holidays.

2

There are 2 best solutions below

0
On

To get holidays without a package, we can use a free API https://date.nager.at/Api (and httr).

res <- httr::GET("https://date.nager.at/api/v3/publicholidays/2024/DE") |> httr::content()
lapply(res, `[`, c("date", "localName", "name", "fixed", "global")) |>
  bind_rows() %>%
  mutate(date = as.Date(date))
# # A tibble: 19 × 5
#    date       localName                 name                      fixed global
#    <date>     <chr>                     <chr>                     <lgl> <lgl> 
#  1 2024-01-01 Neujahr                   New Year's Day            TRUE  TRUE  
#  2 2024-01-06 Heilige Drei Könige       Epiphany                  TRUE  FALSE 
#  3 2024-03-08 Internationaler Frauentag International Women's Day TRUE  FALSE 
#  4 2024-03-29 Karfreitag                Good Friday               FALSE TRUE  
#  5 2024-03-31 Ostersonntag              Easter Sunday             FALSE FALSE 
#  6 2024-04-01 Ostermontag               Easter Monday             FALSE TRUE  
#  7 2024-05-01 Tag der Arbeit            Labour Day                TRUE  TRUE  
#  8 2024-05-09 Christi Himmelfahrt       Ascension Day             FALSE TRUE  
#  9 2024-05-19 Pfingstsonntag            Pentecost                 FALSE FALSE 
# 10 2024-05-20 Pfingstmontag             Whit Monday               FALSE TRUE  
# 11 2024-05-30 Fronleichnam              Corpus Christi            FALSE FALSE 
# 12 2024-08-15 Mariä Himmelfahrt         Assumption Day            TRUE  FALSE 
# 13 2024-09-20 Weltkindertag             World Children's Day      TRUE  FALSE 
# 14 2024-10-03 Tag der Deutschen Einheit German Unity Day          TRUE  TRUE  
# 15 2024-10-31 Reformationstag           Reformation Day           TRUE  FALSE 
# 16 2024-11-01 Allerheiligen             All Saints' Day           TRUE  FALSE 
# 17 2024-11-20 Buß- und Bettag           Repentance and Prayer Day FALSE FALSE 
# 18 2024-12-25 Erster Weihnachtstag      Christmas Day             TRUE  TRUE  
# 19 2024-12-26 Zweiter Weihnachtstag     St. Stephen's Day         TRUE  TRUE  

You can then use these dates to exclude days in your counting.

0
On

You can use the qlcal package from CRAN which provides QuantLib calendaring logic without a dependency on the QuantLib library.

> library(qlcal)   # QuantLib logic without a QuantLib dependency
> setCalendar("Germany/FrankfurtStockExchange") #or Settlement,Xetra,Eurex,Euwax
> getHolidays(as.Date("2022-01-01"), as.Date("2026-01-01"))
 [1] "2022-04-15" "2022-04-18" "2022-12-26" "2023-04-07" "2023-04-10"
 [6] "2023-05-01" "2023-12-25" "2023-12-26" "2024-01-01" "2024-03-29"
[11] "2024-04-01" "2024-05-01" "2024-12-24" "2024-12-25" "2024-12-26"
[16] "2025-01-01" "2025-04-18" "2025-04-21" "2025-05-01" "2025-12-24"
[21] "2025-12-25" "2025-12-26" "2026-01-01"
>