Hi
One of the most important calculation in applied finance is the number of
days between dates.
That kind of calculus become annoying when a specific calendar must be used.
That is the case for the business days calculus.
The package timeDate has a function isBizday to perform that kind of thing.
The problem is that using this function to calculate the number of business
days between dates has taken a very long time. An example of such an
implementation is the function below.
Has anyone an optimized function to to this counts?
Clearly I have in mind to perform at least the basic analytics for a bond:
Duration, internal rate of return , net present value, etc... That is not
time feasible with a portfolio of bond using the function below.
tks
library(timeDate)
BuziInDates <- function(start, end, holidays){
#calculates the number of business days
#between the dates start and end.
if(class(start) != "Date" | class(end) != "Date"){
stop("Arguments must be of class Date.")
}
if(class(holidays) != "function"){
stop("holidays must be a function")
}
if(start > end){
stop("start date must not be greater than end date.")
}
y1 <- as.POSIXlt(start)$year + 1900
y2 <- as.POSIXlt(end)$year + 1900
seqs <- timeSequence(from = start, to = end, by = "day")
ndays <- length(seqs)
dts <- NULL
temp <- NULL
if( y1 == y2){
temp <- window(holidays(y1), start = start, end = end)
dts <- seqs[isBizday(seqs,temp)]
}else{
temp <- holidays(y1)
for(k in seq(from = y1+1, to = y2)){
temp <- c(temp,holidays(k))
}
temp <- window(temp, start = start, end = end)
dts <- seqs[isBizday(seqs,temp)]
}
bdays <- length(dts)
return(bdays)
}
### using the function...
BuziInDates(as.Date('2009-09-11'), as.Date('2030-09-11') ,
holidayNYSE)
[[alternative HTML version deleted]]