diff options
Diffstat (limited to 'R')
| -rw-r--r-- | R/cds_utils.R | 97 | ||||
| -rw-r--r-- | R/load_cf.R | 3 |
2 files changed, 50 insertions, 50 deletions
diff --git a/R/cds_utils.R b/R/cds_utils.R index fe7bb4ec..a5e7648d 100644 --- a/R/cds_utils.R +++ b/R/cds_utils.R @@ -78,58 +78,59 @@ creditSchedule <- function(startdate, enddate) { couponSchedule <- function(nextpaydate=NULL, maturity, frequency, coupontype, currentcoupon,
margin, startdate=Sys.Date()){
- ## 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, startdate, by =paste0("-", bystring)))
+ ## 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, startdate, by =paste0("-", bystring)))
+ }else{
+ if(nextpaydate>maturity){
+ dates <- maturity
}else{
- if(nextpaydate>maturity){
- dates <- maturity
- }else{
- dates <- seq(nextpaydate, maturity, by = bystring)
- }
+ ## 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 >= startdate]
- 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 >= startdate]
+ 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(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(startdate, dates, "act/360"))) * coupons
- if(startdate!=DC$params$tradeDate){
- df <- cumprod(exp(-DC$forwards * diff(c(0, yearFrac(startdate, dates)))))
- }else{
- df <- DC$discounts
- }
- return( data.frame(dates=dates, coupons=coupons, df = df) )
+ if(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(startdate, dates, "act/360"))) * coupons
+ if(startdate!=DC$params$tradeDate){
+ df <- cumprod(exp(-DC$forwards * diff(c(0, yearFrac(startdate, dates)))))
+ }else{
+ df <- DC$discounts
+ }
+ return( data.frame(dates=dates, coupons=coupons, df = df) )
}
nextIMMDate <- function(date) {
diff --git a/R/load_cf.R b/R/load_cf.R index c443152b..8ab40654 100644 --- a/R/load_cf.R +++ b/R/load_cf.R @@ -232,7 +232,6 @@ getcusipcf <- function(cusips, cfdata, dist, workdate=Sys.Date()){ cusipdata[[cusip]] <- NULL
flag <- FALSE
}else{
- cat("done", cusip, "\n")
cusipdata[[cusip]]$fields <- r
cusipdata[[cusip]]$price <- 100 * crossprod(cfdata[[dealnames[i]]]$weight,
cusipdata[[cusip]]$fields[,"Cashflow"])/cusipdata[[cusip]]$currbal
@@ -244,7 +243,7 @@ getcusipcf <- function(cusips, cfdata, dist, workdate=Sys.Date()){ compute.delta(dist, cfdata[[dealnames[i]]]$weight,
cusipdata[[cusip]]$fields[,"Cashflow"]/cusipdata[[cusip]]$currbal,
workdate)
-
+ cat("done", cusip, "\n")
}
}
return(cusipdata)
|
