diff options
Diffstat (limited to 'R/cds_utils.R')
| -rw-r--r-- | R/cds_utils.R | 31 |
1 files changed, 18 insertions, 13 deletions
diff --git a/R/cds_utils.R b/R/cds_utils.R index 9e5a44ae..ccf7ef52 100644 --- a/R/cds_utils.R +++ b/R/cds_utils.R @@ -33,7 +33,9 @@ addTenor <- function(date, tenor) { }
}
-couponSchedule <- function(nextpaydate=NULL, maturity, frequency, coupontype, currentcoupon,
+couponSchedule <- function(nextpaydate=NULL, maturity,
+ frequency=c("Quarterly", "Monthly", "Bimonthly", "Semiannual", "Annual"),
+ coupontype, currentcoupon,
margin, tradedate=Sys.Date(), prevpaydate=tradedate){
## computes the coupon schedule
## inputs:
@@ -41,12 +43,13 @@ couponSchedule <- function(nextpaydate=NULL, maturity, frequency, coupontype, cu ## 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.
+ frequency <- match.arg("frequency")
bystring <- switch(frequency,
- Q = "3 months",
- M = "1 month",
- B = "2 months",
- S = "6 months",
- A = "12 months")
+ Quarterly = "3 months",
+ Monthly = "1 month",
+ Bimonthly = "2 months",
+ Semiannual = "6 months",
+ Annual = "12 months")
if(is.null(bystring)){
stop("unknown frequency")
@@ -73,11 +76,11 @@ couponSchedule <- function(nextpaydate=NULL, maturity, frequency, coupontype, cu names(dates) <- NULL
dt <- switch(frequency,
- S = 1/2,
- Q = 1/4,
- M = 1/12,
- B = 1/6,
- A = 1)
+ Semiannual = 1/2,
+ Quarterly = 1/4,
+ Monthly = 1/12,
+ Bimonthly = 1/6,
+ Annual = 1)
DC <- DiscountCurve(c(YC$params, list(dt=dt)),
YC$tsQuotes, yearFrac(YC$params$tradeDate, dates), YC$legparams)
@@ -101,12 +104,13 @@ couponSchedule <- function(nextpaydate=NULL, maturity, frequency, coupontype, cu return( data.frame(dates=dates, unadj.dates = unadj.dates, coupons=coupons, df = df) )
}
-IMMDate <- function(tradedate, type="next", noadj=FALSE) {
+IMMDate <- function(tradedate, type=c("next", "prev"), 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
+ type <- match.arg(type)
start.protection <- tradedate + 1
startyear <- as.numeric(format(start.protection, format="%Y"))
startyear <- startyear - 1
@@ -165,7 +169,8 @@ cdsMaturity <- function(tenor, date=Sys.Date()){ return ( r )
}
-yearFrac <- function(date1, date2, daycount="act/365") {
+yearFrac <- function(date1, date2, daycount=c("act/365", "act/360")) {
+ daycount <- match.arch(daycount)
switch(daycount,
"act/365"=as.numeric( (as.Date(date2) - as.Date(date1)) / 365),
"act/360"=as.numeric( (as.Date(date2) - as.Date(date1)) / 360) )
|
