library("RQuantLib") today <- function() { Sys.Date() } addBusDay <- function(tradedate = Sys.Date(), n.days = 3, calendar = "UnitedStates/GovernmentBond"){ advance(calendar = calendar, dates=tradedate, n=n.days, timeUnit=0) } 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") } } 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 }else{ ## weird bug with non integer dates, hence the as.Date(as.POSIXlt(.)) trick dates <- seq(nextpaydate, as.Date(as.POSIXlt(maturity)), by = bystring) } } dates <- dates[ dates >= tradedate] if(length(dates)>0){ dates <- adjust(calendar="UnitedStates/GovernmentBond", dates) } if(length(dates)==0){ dates <- maturity }else{ if(dates[length(dates)]= start.protection][1] }else if(type == "prev"){ temp <- nextimmdates[nextimmdates < start.protection] val <- temp[length(temp)] }else{ stop("incorrect type") } if(!noadj){ val <- adjust(calendar = "UnitedStates/GovernmentBond", val ) } 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()){ r <- IMMDate(addTenor(date, tenor)) names(r) <- tenor return ( r ) } 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) ) }