我在计算特定时间范围内德国的工作日时遇到问题。意思是,我想排除公共假期、周六和周日。 我无法使用 RQuantLib 或 bizdays 软件包,因为它们无法安装在我运行代码的实例上。有没有替代方案:
#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 包似乎只涵盖了几个德国假期,甚至其中一些是错误的,例如12月31日不是公共假期,1.1。不见了。
对我来说最重要的是假期期间有一个合适的图书馆。
要在没有套餐的情况下获得假期,我们可以使用免费的 API https://date.nager.at/Api(和
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
然后,您可以使用这些日期来排除计数中的天数。
您可以使用 qlcal 包,它提供了 QuantLib 日历逻辑并依赖于 QuantLib 库。
> 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"
>