Introduction to bizdays

2024-02-12

Introduction

bizdays was developed to count business days between two dates. This is a big issue in brazilian financial market because many financial instruments consider the amount of business days in their accounting rules. So, a typical use of the package is:

library(bizdays)
bizdays("2022-02-01", "2022-02-28", "Brazil/ANBIMA")
## [1] 18

The bizdays function returns the amount of business days between these dates according to the calendar Brazil/ANBIMA.

The calendar Brazil/ANBIMA is already loaded and all loaded calendars can be seen with calendars()

calendars()
## Calendars: 
## Actual, Brazil/ANBIMA, Brazil/B3, Brazil/BMF, EveryMonday, MyCalendar, WeekendsOnly, actual, example1, example2, example3, weekends

That lists calendars registered in the calendar register. Once you have a calendar registered you can simply use its name in the functions.

bizdays("2022-02-01", "2022-02-28", "actual")
## [1] 27

You can look specificaly at one calendar by doing

calendars()[["Brazil/B3"]]
## Brazil/B3 financial calendar 
##   560 holidays 
##   2 weekdays (saturday, sunday) 
##   range from 1990-01-01 to 2024-12-31 
## bizdays arguments adjust
##   from: following 
##   to:   preceding

Load calendars from other packages

Calendars can be loaded from packages RQuantlib and timeDate (Rmetrics).

load_rmetrics_calendars(2000:2030)
## Calendar Rmetrics/LONDON loaded
## Calendar Rmetrics/NERC loaded
## Calendar Rmetrics/NYSE loaded
## Calendar Rmetrics/TSX loaded
## Calendar Rmetrics/ZURICH loaded
calendars()
## Calendars: 
## Actual, Brazil/ANBIMA, Brazil/B3, Brazil/BMF, EveryMonday, MyCalendar, Rmetrics/LONDON, Rmetrics/NERC, Rmetrics/NYSE, Rmetrics/TSX, Rmetrics/ZURICH, WeekendsOnly, actual, example1, example2, example3, weekends

Once you have calendars loaded they can be directly used by its name.

bizdays("2022-02-01", "2022-02-28", "Rmetrics/NYSE")
## [1] 18

So, unless you really need a new calendar, you don’t have to create them.

Usage

Count bizdays

bizdays("2022-02-01", "2022-02-28", "Brazil/ANBIMA")
## [1] 18
getbizdays("2022-01", "Brazil/ANBIMA")
## [1] 21

Check bizdays

is.bizday(c("2022-02-01", "2022-02-05"), "Brazil/ANBIMA")
## [1]  TRUE FALSE

Fix dates

following(c("2022-02-01", "2022-02-05"), "Brazil/ANBIMA")
## [1] "2022-02-01" "2022-02-07"
preceding(c("2022-02-01", "2022-02-05"), "Brazil/ANBIMA")
## [1] "2022-02-01" "2022-02-04"

Sequence of business days

bizseq("2022-02-01", "2022-02-05", "Brazil/ANBIMA")
## [1] "2022-02-01" "2022-02-02" "2022-02-03" "2022-02-04"

Add business days to a date

add.bizdays("2022-02-01", 0:5, "Brazil/ANBIMA")
## [1] "2022-02-01" "2022-02-02" "2022-02-03" "2022-02-04" "2022-02-07"
## [6] "2022-02-08"

getdate

getdate("first bizday", "2022-01", "Brazil/ANBIMA")
## [1] "2022-01-03"
getdate("180th day", "2022", "Brazil/ANBIMA")
## [1] "2022-06-29"