library("RQuantLib") today <- function() { Sys.Date() } convertTenor <- function(tenor) { ## convert tenors of the form '1y', '2y', etc... ## and '1m', '2m'... into yearfrac month <- regexpr("([0-9]+)m", tenor, perl = T) year <- regexpr("([0-9]+)y", tenor, perl = T) if ( month != -1 ) { a <- attr(month, "capture.start") b <- a + attr(month, "capture.length") - 1 l <- as.numeric(substr(tenor, a, b)) return ( 30 * l ) }else if ( year != -1) { a <- as.numeric(attr(year, "capture.start")) b <- a + attr(year, "capture.length") - 1 l <- as.numeric(substr(tenor, a,b)) return ( 365 * l ) }else{ stop("format not recognized") } } addTenor <- function(date, tenor) { month <- regexpr("([0-9]+)m", tenor, perl = T) year <- regexpr("([0-9]+)y", tenor, perl = T) if ( month != -1 ) { a <- attr(month, "capture.start") b <- a + attr(month, "capture.length") - 1 l <- as.numeric(substr(tenor, a, b)) return ( seq(date, length=2, by=paste(l,"month"))[2]) }else if ( year != -1) { a <- as.numeric(attr(year, "capture.start")) b <- a + attr(year, "capture.length") - 1 l <- as.numeric(substr(tenor, a,b)) return ( seq(date, length=2, by=paste(l,"year"))[2] ) }else{ stop("format not recognized") } } 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="-")) seq(newdate, enddate, by="3 months") } couponSchedule <- function(nextpaydate=NULL, maturity, frequency, coupontype, currentcoupon, margin, startdate=today()){ ## 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 amon "Q", "M", "B", or "S" ## if startdate is provided, this generates 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, today(), by =paste0("-", bystring))) }else{ dates <- seq(nextpaydate, maturity, by = bystring) } if(dates[length(dates)]= today()] 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 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) { startyear <- as.numeric(format(date, "%Y")) nextimmdates <- seq(as.Date(paste(startyear, 3, 20, sep="-")), length=5, by="3 months") return( nextimmdates[nextimmdates >= date][1] ) } cdsMaturity <- function(tenor, date=today()){ 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="-"))) } yearFrac <- function(date1, date2, daycount="act/365") { switch(daycount, "act/365"=as.numeric( (as.Date(date2) - as.Date(date1)) / 365), "act/360"=as.numeric( (as.Date(date2) - as.Date(date1)) / 360) ) }