diff options
| -rw-r--r-- | R/cds_utils.R | 124 |
1 files changed, 55 insertions, 69 deletions
diff --git a/R/cds_utils.R b/R/cds_utils.R index 5a3f83f8..fb1fb296 100644 --- a/R/cds_utils.R +++ b/R/cds_utils.R @@ -49,61 +49,64 @@ addTenor <- function(date, tenor) { couponSchedule <- function(nextpaydate=NULL, maturity, frequency, coupontype, currentcoupon,
margin, tradedate=Sys.Date(), prevpaydate=tradedate){
- ## computes the coupon schedule
- ## inputs:
- ## nextpaydate: first payment date of the coupon schedule
- ## maturity: last payment date of the schedule
- ## frequency: letter specifying the frequency between "Q", "M", "B", "S" or "A"
- ## if startdate is provided, we generate the forward coupon schedule starting from that date.
- bystring <- switch(frequency,
- Q = "3 months",
- M = "1 month",
- B = "2 months",
- S = "6 months",
- A = "12 months")
- if(is.null(bystring)){
- stop("unknown frequency")
- }
- if(is.null(nextpaydate)){
- dates <- rev(seq(maturity, tradedate, by =paste0("-", bystring)))
- }else{
- if(nextpaydate>maturity){
- dates <- maturity
+ ## computes the coupon schedule
+ ## inputs:
+ ## nextpaydate: first payment date of the coupon schedule
+ ## maturity: last payment date of the schedule
+ ## frequency: letter specifying the frequency between "Q", "M", "B", "S" or "A"
+ ## if startdate is provided, we generate the forward coupon schedule starting from that date.
+ bystring <- switch(frequency,
+ Q = "3 months",
+ M = "1 month",
+ B = "2 months",
+ S = "6 months",
+ A = "12 months")
+
+ if(is.null(bystring)){
+ stop("unknown frequency")
+ }
+ if(is.null(nextpaydate)){
+ dates <- rev(seq(maturity, tradedate, by =paste0("-", bystring)))
}else{
- ## weird bug with non integer dates, hence the as.Date(as.POSIXlt(.)) trick
- dates <- seq(nextpaydate, as.Date(as.POSIXlt(maturity)), by = bystring)
+ if(nextpaydate>maturity){
+ dates <- maturity
+ }else{
+ ## weird bug with non integer dates, hence the as.Date(as.POSIXlt(.)) trick
+ dates <- seq(nextpaydate, as.Date(as.POSIXlt(maturity)), by = bystring)
+ }
}
- }
- if(length(dates)==0){
- dates <- maturity
- }else{
- if(dates[length(dates)]<maturity){
- dates <- c(dates, maturity)
+ if(length(dates)==0){
+ dates <- maturity
+ }else{
+ if(dates[length(dates)]<maturity){
+ dates <- c(dates, maturity)
+ }
}
- }
- dates <- dates[ dates >= tradedate]
- dates <- adjust(calendar="UnitedStates/GovernmentBond", dates)
- DC <- switch(frequency,
- S = DiscountCurve(L6m$params, L6m$tsQuotes, yearFrac(L6m$params$tradeDate, dates)),
- Q = DiscountCurve(L3m$params, L3m$tsQuotes, yearFrac(L3m$params$tradeDate, dates)),
- M = DiscountCurve(L1m$params, L1m$tsQuotes, yearFrac(L1m$params$tradeDate, dates)),
- B = DiscountCurve(L2m$params, L2m$tsQuotes, yearFrac(L2m$params$tradeDate, dates)),
- A = DiscountCurve(L12m$params, L12m$tsQuotes, yearFrac(L12m$params$tradeDate, dates)))
+ dates <- dates[ dates >= tradedate]
+ dates <- adjust(calendar="UnitedStates/GovernmentBond", dates)
+ names(dates) <- NULL
+ DC <- switch(frequency,
+ S = DiscountCurve(L6m$params, L6m$tsQuotes, yearFrac(L6m$params$tradeDate, dates)),
+ Q = DiscountCurve(L3m$params, L3m$tsQuotes, yearFrac(L3m$params$tradeDate, dates)),
+ M = DiscountCurve(L1m$params, L1m$tsQuotes, yearFrac(L1m$params$tradeDate, dates)),
+ B = DiscountCurve(L2m$params, L2m$tsQuotes, yearFrac(L2m$params$tradeDate, dates)),
+ A = DiscountCurve(L12m$params, L12m$tsQuotes, yearFrac(L12m$params$tradeDate, dates)))
- if(toupper(coupontype)=="FLOAT" && !is.na(margin)){
- ## if is.na(margin) probably letter of credit
- ## we floor the coupon at the current gross rate
- coupons <- pmax(currentcoupon, DC$forwards + margin)
- }else{
- coupons <- rep(currentcoupon, length(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) )
+ if(toupper(coupontype)=="FLOAT" && !is.na(margin)){
+ ## if is.na(margin) probably letter of credit
+ ## we floor the coupon at the current gross rate
+ coupons <- pmax(currentcoupon, DC$forwards + margin)
+ }else{
+ coupons <- rep(currentcoupon, length(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) )
}
IMMDate <- function(tradedate, type="next") {
@@ -137,24 +140,7 @@ cdsAccrued <- function(tradedate, coupon){ }
cdsMaturity <- function(tenor, date=Sys.Date()){
- enddate <- addTenor(date, tenor)
- month <- c(3, 6, 9, 12)
- day <- 20
- startmonth <- as.numeric(format(enddate, "%m"))
- startday <- as.numeric(format(enddate, "%d"))
- startyear <- as.numeric(format(enddate, "%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]
- }
- return( as.Date(paste(startyear, month[startmonth], day, sep="-")))
+ return ( IMMDate(addTenor(date, tenor)) )
}
yearFrac <- function(date1, date2, daycount="act/365") {
|
