SetYearFraction <- function(dayCount) {
dayCount <- toupper(dayCount)
if (dayCount %in% c("ACT360", "ACT365F")) {
dominator <- switch(dayCount,
"ACT360" = 360,
"ACT365F" = 365)
function(startDate, endDate) as.numeric(endDate - startDate) / dominator
} else if (dayCount %in% c("ACTACT", "ACTACTISDA", "ACT365")) {
RemainderOfYear <- function(date) `if`(lubridate::leap_year(date), 366, 365) - DaysOfYear(date) + 1
Vectorize(
function(startDate, endDate) {
startYyyy <- lubridate::year(startDate)
endYyyy <- lubridate::year(endDate)
startLength <- `if`(lubridate::leap_year(startDate), 366, 365)
endLength <- `if`(lubridate::leap_year(endDate), 366, 365)
if (startYyyy == endYyyy) {
as.numeric(endDate - startDate) / startLength
} else {
endYyyy - startYyyy - 1 +
(startLength - RemainderOfYear(startDate)) / startLength +
(endLength - RemainderOfYear(endDate)) / endLength
}
}
)
} else if (dayCount %in% c("30360BOND", "30E360")) {
BaseYearFraction <- function(startDate, endDate) {
lubridate::year(endDate) - lubridate::year(startDate) +
(lubridate::month(endDate) - lubridate::month(startDate)) / 12 +
(lubridate::day(endDate) - lubridate::day(startDate)) / 360
}
switch(dayCount,
"30360BOND" = function(startDate, endDate) {
lubridate::day(startDate) <- pmin(lubridate::day(startDate), 30)
lubridate::day(endDate) <- ifelse(test = lubridate::day(startDate) == 30,
yes = pmin(lubridate::day(endDate), 30),
no = lubridate::day(endDate))
BaseYearFraction(startDate, endDate)
},
"30E360" = function(startDate, endDate) {
lubridate::day(startDate) <- pmin(lubridate::day(startDate), 30)
lubridate::day(endDate) <- pmin(lubridate::day(endDate), 30)
BaseYearFraction(startDate, endDate)
})
} else {
stop("unknown type of day count convention")
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.