diff options
Diffstat (limited to 'R/cds_utils.R')
| -rw-r--r-- | R/cds_utils.R | 67 |
1 files changed, 32 insertions, 35 deletions
diff --git a/R/cds_utils.R b/R/cds_utils.R index a2f15b4e..5a3f83f8 100644 --- a/R/cds_utils.R +++ b/R/cds_utils.R @@ -47,32 +47,8 @@ addTenor <- function(date, tenor) { }
}
-creditSchedule <- function(startdate, enddate) {
- ## generate a credit coupon payment schedule from startdate
- ## to enddate based on the IMM dates
- month <- c(3, 6, 9, 12)
- day <- 20
- startmonth <- as.numeric(format(startdate, "%m"))
- startday <- as.numeric(format(startdate, "%d"))
- startyear <- as.numeric(format(startdate, "%Y"))
- if (startday > day){
- followingmonth <- which(month>startmonth)
- if (length(followingmonth)==0){
- startyear <- startyear+1
- startmonth <- 3
- }else{
- startmonth <- followingmonth[1]
- }
- }else{
- startmonth <- which(month>=startmonth)[1]
- }
- newdate <- as.Date(paste(startyear, month[startmonth], day, sep="-"))
- return(adjust(calendar="UnitedStates/GovernmentBond",
- seq(newdate, enddate, by="3 months")))
-}
-
couponSchedule <- function(nextpaydate=NULL, maturity, frequency, coupontype, currentcoupon,
- margin, startdate=Sys.Date()){
+ margin, tradedate=Sys.Date(), prevpaydate=tradedate){
## computes the coupon schedule
## inputs:
## nextpaydate: first payment date of the coupon schedule
@@ -89,7 +65,7 @@ couponSchedule <- function(nextpaydate=NULL, maturity, frequency, coupontype, cu stop("unknown frequency")
}
if(is.null(nextpaydate)){
- dates <- rev(seq(maturity, startdate, by =paste0("-", bystring)))
+ dates <- rev(seq(maturity, tradedate, by =paste0("-", bystring)))
}else{
if(nextpaydate>maturity){
dates <- maturity
@@ -105,7 +81,7 @@ couponSchedule <- function(nextpaydate=NULL, maturity, frequency, coupontype, cu dates <- c(dates, maturity)
}
}
- dates <- dates[ dates >= startdate]
+ dates <- dates[ dates >= tradedate]
dates <- adjust(calendar="UnitedStates/GovernmentBond", dates)
DC <- switch(frequency,
S = DiscountCurve(L6m$params, L6m$tsQuotes, yearFrac(L6m$params$tradeDate, dates)),
@@ -121,24 +97,45 @@ couponSchedule <- function(nextpaydate=NULL, maturity, frequency, coupontype, cu }else{
coupons <- rep(currentcoupon, length(dates))
}
- coupons <- diff(c(0, yearFrac(startdate, dates, "act/360"))) * coupons
- if(startdate!=DC$params$tradeDate){
- df <- cumprod(exp(-DC$forwards * diff(c(0, yearFrac(startdate, dates)))))
+ coupons <- diff(c(0, yearFrac(prevpaydate, dates, "act/360"))) * coupons
+ if(tradedate!=DC$params$tradeDate){
+ df <- cumprod(exp(-DC$forwards * diff(c(0, yearFrac(tradedate, dates)))))
}else{
df <- DC$discounts
}
return( data.frame(dates=dates, coupons=coupons, df = df) )
}
-nextIMMDate <- function(date) {
- startyear <- as.numeric(format(date, "%Y"))
- nextimmdates <- seq(as.Date(paste(startyear, 3, 20, sep="-")), length=5, by="3 months")
- val <- adjust(calendar = "UnitedStates/GovernmentBond",
- nextimmdates[nextimmdates >= date][1] )
+IMMDate <- function(tradedate, type="next") {
+ ## 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, "%Y"))
+ if(as.numeric(format(tradedate, "%m")) <= 3){
+ startyear <- startyear - 1
+ }
+ nextimmdates <- seq(as.Date(paste(startyear, 3, 20, sep="-")), length=7, by="3 months")
+ if(type == "next"){
+ val <- adjust(calendar = "UnitedStates/GovernmentBond",
+ nextimmdates[nextimmdates >= start.protection][1] )
+ }else if(type == "prev"){
+ temp <- nextimmdates[nextimmdates < start.protection]
+ val <- adjust(calendar = "UnitedStates/GovernmentBond", temp[length(temp)] )
+ }else{
+ stop("incorrect type")
+ }
names(val) <- NULL
return( val )
}
+cdsAccrued <- function(tradedate, coupon){
+ start.protection <- tradedate + 1
+ return (yearFrac(IMMDate(tradedate, "prev"), start.protection, "act/360") * coupon)
+}
+
cdsMaturity <- function(tenor, date=Sys.Date()){
enddate <- addTenor(date, tenor)
month <- c(3, 6, 9, 12)
|
