aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--R/cds_functions_generic.R6
-rw-r--r--R/cds_utils.R31
-rw-r--r--R/tranche_functions.R4
-rw-r--r--R/yieldcurve.R10
4 files changed, 29 insertions, 22 deletions
diff --git a/R/cds_functions_generic.R b/R/cds_functions_generic.R
index 96463a43..167b3d91 100644
--- a/R/cds_functions_generic.R
+++ b/R/cds_functions_generic.R
@@ -854,7 +854,8 @@ forward.prot <- function(index, exerciseDate){
defaultAdjustedForwardIndexPrice <- function(index, exerciseDate, fixedRate=0.05){
tes <- addBusDay(exerciseDate)
- df <- DiscountCurve(L3m$params, L3m$tsQuotes, yearFrac(index$tradedate, tes))$discounts
+ df <- DiscountCurve(c(YC$params, list(dt=0.25)), YC$tsQuotes, yearFrac(index$tradedate, tes),
+ YC$legparams)$discounts
price <- 1 - FEP(index, exerciseDate) +
1/df*(forward.cl(index, exerciseDate)*fixedRate -
forward.prot(index, exerciseDate)-cdsAccrued(exerciseDate, fixedRate))
@@ -864,7 +865,8 @@ defaultAdjustedForwardIndexPrice <- function(index, exerciseDate, fixedRate=0.05
forwardflatcds <- function(h, cs, tradeDate, exerciseDate, fixedRate=0.05, R=0.4){
tes <- addBusDay(exerciseDate)
fep <- (1-R)*(1-exp(-h*yearFrac(tradeDate, exerciseDate)))
- df <- DiscountCurve(L3m$params, L3m$tsQuotes, yearFrac(tradeDate, tes))$discounts
+ df <- DiscountCurve(c(YC$params, list(dt=0.25)), YC$tsQuotes, yearFrac(tradeDate, tes),
+ YC$legparams)$discounts
sc <- new("flatcurve", h=h)
cl <- couponleg(cs, sc, startdate=exerciseDate)
pl <- defaultleg(cs, sc, recovery=R, startdate=exerciseDate)
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) )
diff --git a/R/tranche_functions.R b/R/tranche_functions.R
index 3a7a643c..4cc9ba73 100644
--- a/R/tranche_functions.R
+++ b/R/tranche_functions.R
@@ -181,13 +181,13 @@ MFtranche.pv <- function(index, dist, protection=FALSE){
return(list(pl=plvec, cl=clvec, bp=bp))
}
-adjust.skew <- function(index1, index2, method="ATM"){
+adjust.skew <- function(index1, index2, method=c("ATM", "TLP", "PM")){
#index1 is the index for which we already have computed the skew
#index2 is the index we're mapping to
# if method="ATM", do simple at the money mapping
# method="TLP", do tranche loss proportion mapping
# method="PM", do probability matching
-
+ method <- match.arg(method)
K1 <- index1$K[-c(1,length(index1$K))]
K2 <- index2$K[-c(1,length(index2$K))]
diff --git a/R/yieldcurve.R b/R/yieldcurve.R
index 94762c25..b0703096 100644
--- a/R/yieldcurve.R
+++ b/R/yieldcurve.R
@@ -6,7 +6,7 @@ if(.Platform$OS.type == "unix"){
data.dir <- "//WDSENTINEL/share/CorpCDOs/data"
}
-getMarkitIRData <- function(date=Sys.Date(), currency="USD") {
+getMarkitIRData <- function(date=Sys.Date(), currency=c("USD", "EUR")) {
## downloads the latest available interest rates data from Markit
## before date and returns the parsed file into a list
require(xml2)
@@ -19,7 +19,7 @@ getMarkitIRData <- function(date=Sys.Date(), currency="USD") {
return( as_list(read_xml(file.path(data.dir, "Yield Curves", filename.ext))) )
}else{
temp <- tempfile(tmpdir = file.path(data.dir, "Yield Curves"))
- download.file(paste("http://www.markit.com/news/", filename, ".zip", sep=""), temp, quiet=T)
+ download.file(paste0("http://www.markit.com/news/", filename, ".zip"), temp, quiet=T)
con <- file(temp, "r")
firstline <- readLines(con, 1, warn=FALSE)
## Markit returns a plain text file if there is no data.
@@ -51,7 +51,7 @@ nextthirdwed <- function(x) {
thirdwed(y + 30 * (y < x))
}
-buildMarkitYC <- function(MarkitData, currency="USD", futurequotes){
+buildMarkitYC <- function(MarkitData, currency=c("USD", "EUR"), futurequotes){
deposits <- list()
futures <- list()
swaps <- list()
@@ -90,7 +90,7 @@ buildMarkitYC <- function(MarkitData, currency="USD", futurequotes){
return( tsQuotes )
}
-exportYC <- function(tradedate=Sys.Date(), currency="USD", useFutures=FALSE){
+exportYC <- function(tradedate=Sys.Date(), currency=c("USD", "EUR"), useFutures=FALSE){
## export the Yield Curve into the environment
require(RQuantLib)
if(useFutures){
@@ -113,7 +113,7 @@ exportYC <- function(tradedate=Sys.Date(), currency="USD", useFutures=FALSE){
USD = list(fixFreq="Semiannual",
floatFreq="Quarterly",
dayCounter="Thirty360"),
- EUR = list(fixfreq="Annual",
+ EUR = list(fixFreq="Annual",
floatFreq="Semiannual",
dayCounter="Thirty360"))
params <- list(tradeDate=tradedate,