aboutsummaryrefslogtreecommitdiffstats
path: root/R/cds_utils.R
blob: afb8f400de52391cff816e3cf01dda96a1795b12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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)]<maturity){
        dates <- c(dates, maturity)
    }
    dates <- dates[ 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) )
}