aboutsummaryrefslogtreecommitdiffstats
path: root/R
diff options
context:
space:
mode:
Diffstat (limited to 'R')
-rw-r--r--R/cds_utils.R20
1 files changed, 16 insertions, 4 deletions
diff --git a/R/cds_utils.R b/R/cds_utils.R
index 1186f953..dab6da07 100644
--- a/R/cds_utils.R
+++ b/R/cds_utils.R
@@ -4,6 +4,15 @@ today <- function() {
Sys.Date()
}
+prevBusDay <- function(workdate = Sys.Date()){
+ i <- 1
+ while(!isBusinessDay(calendar = "UnitedStates/GovernmentBond",
+ workdate - i)){
+ i <- i+1
+ }
+ return( workdate - i )
+}
+
convertTenor <- function(tenor) {
## convert tenors of the form '1y', '2y', etc...
## and '1m', '2m'... into yearfrac
@@ -36,7 +45,8 @@ addTenor <- function(date, tenor) {
a <- as.numeric(attr(year, "capture.start"))
b <- a + attr(year, "capture.length") - 1
l <- as.numeric(substr(tenor, a,b))
- return ( seq(date, length=2, by=paste(l,"year"))[2] )
+ return ( adjust(calendar = "UnitedStates/GovernmentBond",
+ seq(date, length=2, by=paste(l,"year"))[2]) )
}else{
stop("format not recognized")
}
@@ -62,7 +72,8 @@ creditSchedule <- function(startdate, enddate) {
startmonth <- which(month>=startmonth)[1]
}
newdate <- as.Date(paste(startyear, month[startmonth], day, sep="-"))
- seq(newdate, enddate, by="3 months")
+ return(adjust(calendar="UnitedStates/GovernmentBond",
+ seq(newdate, enddate, by="3 months")))
}
couponSchedule <- function(nextpaydate=NULL, maturity, frequency, coupontype, currentcoupon,
@@ -120,10 +131,11 @@ couponSchedule <- function(nextpaydate=NULL, maturity, frequency, coupontype, cu
nextIMMDate <- function(date) {
startyear <- as.numeric(format(date, "%Y"))
nextimmdates <- seq(as.Date(paste(startyear, 3, 20, sep="-")), length=5, by="3 months")
- return( nextimmdates[nextimmdates >= date][1] )
+ return( adjust(calendar = "UnitedStates/GovernmentBond",
+ nextimmdates[nextimmdates >= date][1] )
}
-cdsMaturity <- function(tenor, date=today()){
+cdsMaturity <- function(tenor, date=Sys.Date()){
enddate <- addTenor(date, tenor)
month <- c(3, 6, 9, 12)
day <- 20