aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--R/cds_utils.R29
1 files changed, 28 insertions, 1 deletions
diff --git a/R/cds_utils.R b/R/cds_utils.R
index e7ffce3a..90c71d28 100644
--- a/R/cds_utils.R
+++ b/R/cds_utils.R
@@ -125,13 +125,40 @@ IMMDate <- function(tradedate, type="next", noadj=FALSE) {
return( val )
}
+IMMDate2 <- function(tradedate, tenor, noadj=FALSE) {
+ ## returns the next IMM date for a CDS, adjusted for settlement
+ ## or previous one if type="prev"
+ ## protection seems to be assumed at close of business day
+ ## so if we trade on Friday, we're protected during the week-end
+ ## matches with Bloomberg calculator
+ start.protection <- tradedate + 1
+ startyear <- as.numeric(format(start.protection, format="%Y"))
+ startyear <- startyear - 1
+ previmmdates <- seq(as.Date(paste(startyear, 3, 20, sep="-")), length=5, by="6 months")
+ ## firstimmdate
+ temp <- previmmdates[previmmdates < start.protection]
+ firstimmdate <- temp[length(temp)]
+ maturity <- addTenor(addTenor(firstimmdate, tenor), "3m")
+ if(!noadj){
+ maturity <- adjust(calendar = "UnitedStates/GovernmentBond", maturity )
+ }
+ names(maturity) <- NULL
+ return( maturity )
+}
+
cdsAccrued <- function(tradedate, coupon){
start.protection <- tradedate + 1
return (yearFrac(IMMDate(tradedate, "prev"), start.protection, "act/360") * coupon)
}
cdsMaturity <- function(tenor, date=Sys.Date()){
- r <- IMMDate(addTenor(date, tenor))
+ ## before December 20, 2015, single name cds rolled quarterly
+ if(date < as.Date("2015-12-20")){
+ r <- IMMDate(addTenor(date, tenor))
+ }else{
+ r <- IMMDate2(date, tenor)
+
+ }
names(r) <- tenor
return ( r )
}