quantdates

Julian Chitiva

2024-07-03

This package is a R library that provides the user to work with dates and days count for financial analysis. It works with business days from New York (US), London (UK) and Bogota (CO). It also implements day counting using common financial conventions such as ACT/360, ACT/365, ACT/ACT and 30/360, among others.

Dates Manipulation

This part implement functions for dates manipulation such as add days (business days) to a date, transform dates between various formats and obtain business days for a given city calendar.

Add Date

AddDate(date = Sys.Date(),addDays=14,addMonths=2,addYears=3)
#> [1] "2027-09-17"
AddDate(date = '2019-10-04',addDays=14,addMonths=2,addYears=3)
#> [1] "2022-12-18"

Add Business Days

AddBusinessDays(date = Sys.Date(),numDate = 15,loc = 'NY')
#> [1] "2024-07-25"
AddBusinessDays(date = '2020-10-03',numDate = 15,loc = 'NY')
#> [1] "2020-10-23"
AddBusinessDays(date = Sys.Date(),numDate = 15,loc = 'BOG')
#> [1] "2024-07-24"
AddBusinessDays(date = Sys.Date(),numDate = 15,loc = 'LDN')
#> [1] "2024-07-24"

Last Day of Month

LastDayOfMonth(year = 2020, month = 2)
#> [1] "2020-02-29"
LastDayOfMonth(year = 2024, month = 5)
#> [1] "2024-05-31"
LastDayOfMonth(date = '2020-02-03')
#> [1] "2020-02-29"

Business Days

BusinessDays(loc='BOG')[2500:2520]
#>  [1] "2000-03-15" "2000-03-16" "2000-03-17" "2000-03-21" "2000-03-22"
#>  [6] "2000-03-23" "2000-03-24" "2000-03-27" "2000-03-28" "2000-03-29"
#> [11] "2000-03-30" "2000-03-31" "2000-04-03" "2000-04-04" "2000-04-05"
#> [16] "2000-04-06" "2000-04-07" "2000-04-10" "2000-04-11" "2000-04-12"
#> [21] "2000-04-13"
BusinessDays(loc='BOG', from='2020-10-10', to='2020-11-10')
#>  [1] "2020-10-13" "2020-10-14" "2020-10-15" "2020-10-16" "2020-10-19"
#>  [6] "2020-10-20" "2020-10-21" "2020-10-22" "2020-10-23" "2020-10-26"
#> [11] "2020-10-27" "2020-10-28" "2020-10-29" "2020-10-30" "2020-11-03"
#> [16] "2020-11-04" "2020-11-05" "2020-11-06" "2020-11-09" "2020-11-10"

Num to Date

NumR2DateR(as.numeric(Sys.Date()))
#> [1] "2024-07-03"
NumExcel2DateR(as.numeric(Sys.Date()))
#> [1] "1954-07-02"

Counting Dates

This section shows the usage of day count functions.

Difftime

difftime_leap_year(tfinal='2023-03-05',tinitial='2019-02-28',leapDatesIn=TRUE)
#> [1] 1466
difftime_leap_year(tfinal=as.Date('2023-03-05'),tinitial=as.Date('2019-02-28'),leapDatesIn=TRUE)
#> [1] 1466
difftime_leap_year(tfinal='2023-03-05',tinitial='2019-02-28',leapDatesIn=FALSE)
#> [1] 1465
difftime_leap_year(tfinal='2023-03-05',tinitial=as.Date('2019-02-28'),leapDatesIn=FALSE)
#> [1] 1465

Difftime in Business Days

difftime_business(tfinal='2023-03-08',tinitial='2019-02-28',wd=wdBOG)
#> [1] 988
difftime_business(tfinal='2023-03-08',tinitial='2019-02-28',wd=wdLDN)
#> [1] 1015
difftime_business(tfinal='2023-03-08',tinitial='2019-02-28',wd=wdNY)
#> [1] 1013

Day Count

day_count(tfinal='2023-03-08',tinitial='2019-02-28',convention='ACT/365')
#> [1] 4.024658
day_count(tfinal='2023-03-08',tinitial='2019-02-28',convention='ACT/360')
#> [1] 4.080556
day_count(tfinal='2023-03-08',tinitial='2019-02-28',convention='30/360')
#> [1] 4.027778
day_count(tfinal='2023-03-08',tinitial='2019-02-28',convention='NL/365')
#> [1] 4.021918
day_count(tfinal='2023-03-08',tinitial='2019-02-28',convention='ACT/ACT-ISDA')
#> [1] 4.024658
day_count(tfinal='2023-03-08',tinitial='2019-02-28',convention='ACT/ACT-AFB')
#> [1] 4.019178