aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--R/calibrate_tranches_BC.R6
-rw-r--r--R/cds_functions_generic.R4
-rw-r--r--R/cds_utils.R20
-rw-r--r--R/intex_deal_functions.R14
-rw-r--r--R/load_cf.R20
-rw-r--r--R/tranches_RV_BC.R3
-rw-r--r--R/yieldcurve.R21
7 files changed, 47 insertions, 41 deletions
diff --git a/R/calibrate_tranches_BC.R b/R/calibrate_tranches_BC.R
index 7bc71471..7652a90f 100644
--- a/R/calibrate_tranches_BC.R
+++ b/R/calibrate_tranches_BC.R
@@ -87,12 +87,12 @@ for(run in config$runs){
alldates <- seq(begin.date, as.Date(as.character(args$until)), by="1 day")
if(tolower(substr(index.name,1,2)) %in% c("xo", "eu")){
curr <- "EUR"
- cal <- "TARGET"
+ cal <- Calendar$new("TARGET")
}else{
curr <- "USD"
- cal <- "UnitedStates/GovernmentBond"
+ cal <- Calendar$new("UnitedStates/GovernmentBond")
}
- bus.dates <- alldates[isBusinessDay(calendar=cal, alldates)]
+ bus.dates <- alldates[cal$isBusinessDay(alldates)]
for(j in seq_along(bus.dates)){
tradedate <- bus.dates[j]
diff --git a/R/cds_functions_generic.R b/R/cds_functions_generic.R
index 9bfccb86..1076ce7e 100644
--- a/R/cds_functions_generic.R
+++ b/R/cds_functions_generic.R
@@ -857,7 +857,7 @@ forward.prot <- function(index, exerciseDate){
defaultAdjustedForwardIndexPrice <- function(index, exerciseDate, fixedRate=0.05){
tes <- addBusDay(exerciseDate)
- df <- discountFactors(YC, tes)
+ df <- YC$discount(tes)
price <- 1 - FEP(index, exerciseDate) +
1/df * (forward.cl(index, exerciseDate) * fixedRate -
forward.prot(index, exerciseDate) - cdsAccrued(exerciseDate, fixedRate))
@@ -867,7 +867,7 @@ 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 <- discountFactors(YC, tes)
+ df <- YC$discount(tes)
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 4c352163..044c616a 100644
--- a/R/cds_utils.R
+++ b/R/cds_utils.R
@@ -6,7 +6,8 @@ today <- function() {
}
addBusDay <- function(tradedate = Sys.Date(), n.days = 3, calendar = "UnitedStates/GovernmentBond"){
- advance(calendar = calendar, dates=tradedate, n=n.days, timeUnit=0)
+ cal <- Calendar$new(calendar)
+ cal$advance(dates=tradedate, n=n.days, timeUnit=0)
}
convertTenor <- function(tenor) {
@@ -70,8 +71,9 @@ couponSchedule <- function(nextpaydate=NULL, maturity,
dates <- c(dates, maturity)
}
unadj.dates <- dates
- if(length(dates)>1){
- dates[-length(dates)] <- adjust(calendar="UnitedStates/GovernmentBond", dates[-length(dates)])
+ if(length(dates) > 1){
+ cal <- Calendar$new("UnitedStates/GovernmentBond")
+ dates[-length(dates)] <- cal$adjust(dates[-length(dates)])
}
names(dates) <- NULL
@@ -81,7 +83,7 @@ couponSchedule <- function(nextpaydate=NULL, maturity,
Monthly = "1m",
Bimonthly = "2m",
Annual = "1y")
- forwards <- forwardRates(YC, dates, period)
+ forwards <- YC$forwardRate(dates, period)
if(is.na(coupontype) || (toupper(coupontype)=="FLOAT" && !is.na(margin))){
## if is.na(margin) probably letter of credit
## we floor the coupon at the current gross rate
@@ -94,10 +96,10 @@ couponSchedule <- function(nextpaydate=NULL, maturity,
yf[length(yf)] <- yf[length(yf)]+1/360
coupons <- yf * coupons
- if(tradedate != referenceDate(YC)){
+ if(tradedate != YC$referenceDate){
df <- cumprod(exp(-forwards * diff(c(0, yearFrac(tradedate, dates)))))
}else{
- df <- discountFactors(YC, dates)
+ df <- YC$discount(dates)
}
return( data.frame(dates=dates, unadj.dates = unadj.dates, coupons=coupons, df = df) )
}
@@ -123,7 +125,8 @@ IMMDate <- function(tradedate, type=c("next", "prev"), noadj=FALSE) {
stop("incorrect type")
}
if(!noadj){
- val <- adjust(calendar = "UnitedStates/GovernmentBond", val )
+ cal <- Calendar$new("UnitedStates/GovernmentBond")
+ cal$adjust(val)
}
names(val) <- NULL
return( val )
@@ -144,7 +147,8 @@ IMMDate2 <- function(tradedate, tenor, noadj=FALSE) {
firstimmdate <- temp[length(temp)]
maturity <- addTenor(addTenor(firstimmdate, tenor), "3m")
if(!noadj){
- maturity <- adjust(calendar = "UnitedStates/GovernmentBond", maturity )
+ cal <- Calendar$new("UnitedStates/GovernmentBond")
+ maturity <- cal$adjust( maturity )
}
names(maturity) <- NULL
return( maturity )
diff --git a/R/intex_deal_functions.R b/R/intex_deal_functions.R
index 6020f5e7..38637dc5 100644
--- a/R/intex_deal_functions.R
+++ b/R/intex_deal_functions.R
@@ -365,13 +365,14 @@ getpayday <- function(dealdata, tradedate){
m <- m %%3+9
y <- as.numeric(format(tradedate, "%Y"))
y <- y - 1
- payday <- as.Date(sprintf("%s-%s-%s",y, m, dealdata$pay_day))
+ payday <- as.Date(sprintf("%s-%s-%s", y, m, dealdata$pay_day))
i <- 1
- nextdate <- advance(calendar = "UnitedStates", dates=payday, 3*i, 2, bdc=4)
+ cal <- Calendar$new("UnitedStates")
+ nextdate <- cal$advance(dates=payday, period = "3m", bdc = "Unadjusted")
while(nextdate < tradedate){
payday <- nextdate
i <- i+1
- nextdate <- advance(calendar = "UnitedStates", dates=payday, 3*i, 2, bdc=4)
+ nextdate <- cal$advance(payday, 3*i, 2, bdc = "Unadjusted")
}
return(payday)
}
@@ -381,12 +382,13 @@ getdealschedule <- function(dealdata, freq="1 month", tradedate=Sys.Date(), lag=
n <- if(freq == "1 month") 1 else 3
sched <- payday
i <- 1
- bdc <- if(adjust) 0 else 4
- nextdate <- advance(calendar = "UnitedStates", dates=payday, n*i, 2, bdc=bdc)
+ bdc <- if(adjust) "Following" else "Unadjusted"
+ cal <- Calendar$new("UnitedStates")
+ nextdate <- cal$advance(payday, n*i, 2, bdc = bdc)
while(nextdate<= dealdata$"maturity" + lag){
sched <- c(sched, nextdate)
i <- i+1
- nextdate <- advance(calendar = "UnitedStates", dates=payday, n*i, 2, bdc=bdc)
+ nextdate <- cal$advance(payday, n*i, 2, bdc = bdc)
}
##always add the maturity
if(sched[length(sched)] < dealdata$maturity){
diff --git a/R/load_cf.R b/R/load_cf.R
index 423a8b9e..df70e277 100644
--- a/R/load_cf.R
+++ b/R/load_cf.R
@@ -93,12 +93,11 @@ getdealcf <- function(dealnames, zipfiles, tradedate=Sys.Date()){
dealdata <- getdealdata(dealname, tradedate)
alldates <- getdealschedule(dealdata, "1 month")
config <- getconfig(dealname, tradedate)
-
- T <- ifelse(alldates>=YC$params$tradeDate, yearFrac(YC$params$tradeDate, alldates), 0)
- DC <- DiscountCurve(c(YC$params, list(dt=0.25)), YC$tsQuotes, T, YC$legparams)
- df <- data.table(Date=alldates,
- Discounts=DC$discounts,
- T=T, key="Date")
+ alldates <- ifelse(alldates >= YC$referenceDate, alldates, YC$referenceDate)
+ class(alldates) <- "Date"
+ df <- data.table(Date = alldates,
+ Discounts = YC$discount(alldates),
+ key="Date")
cfdata[[dealname]] <- list(mv = dealdata$mv,
currbal = dealdata$"Curr Collat Bal",
principalbal = dealdata$"Principal Bal")
@@ -148,7 +147,7 @@ getdealcf <- function(dealnames, zipfiles, tradedate=Sys.Date()){
NULL})
data <- df[data, roll=TRUE]
data$Discounts[is.na(data$Discounts)] <- 1
- data$T[is.na(data$T)] <- 0
+ #data$T[is.na(data$T)] <- 0
r[i,] <- as.numeric(data[,list(sum(Cashflow*Discounts),
sum(Principal*Discounts),
sum(Interest*Discounts))])
@@ -224,10 +223,11 @@ getcusipcf <- function(params, cfdata, dist, tradedate=Sys.Date()){
dealname <- dealnames[i]
dealdata <- getdealdata(dealname, tradedate)
alldates <- getdealschedule(dealdata, "1 month", adjust=TRUE)
- T <- ifelse(alldates>=YC$params$tradeDate, yearFrac(YC$params$tradeDate, alldates), 0)
- DC <- DiscountCurve(c(YC$params, list(dt=0.25)), YC$tsQuotes, T, YC$legparams)
+ alldates <- ifelse(alldates >= YC$referenceDate, alldates, YC$referenceDate)
+ class(alldates) <- "Date"
+ T <- yearFrac(YC$referenceDate, alldates)
df <- data.table(Date=alldates,
- Discounts=DC$discounts,
+ Discounts=YC$discount(alldates),
T=T, key="Date")
r <- matrix(0, n.scenarios, 5)
colnames(r) <- fields
diff --git a/R/tranches_RV_BC.R b/R/tranches_RV_BC.R
index 6df778e6..b0ecae9e 100644
--- a/R/tranches_RV_BC.R
+++ b/R/tranches_RV_BC.R
@@ -72,7 +72,8 @@ for(r in runs$mappings){
next
}
alldates <- seq(begin.date, as.Date(as.character(args$until)), by="1 day")
- bus.dates <- alldates[isBusinessDay(calendar="UnitedStates/GovernmentBond", alldates)]
+ cal <- Calendar$new("UnitedStates/GovernmentBond")
+ bus.dates <- alldates[cal$isBusinessDay(alldates)]
addheaders <- TRUE
for(j in seq_along(bus.dates)){
tradedate <- bus.dates[j]
diff --git a/R/yieldcurve.R b/R/yieldcurve.R
index c745fb3c..75a84b30 100644
--- a/R/yieldcurve.R
+++ b/R/yieldcurve.R
@@ -74,11 +74,10 @@ exportYC <- function(tradedate=Sys.Date(), currency=c("USD", "EUR"), useFutures
}
}
MarkitData <- getMarkitIRData(tradedate, currency)
- setEvaluationDate(tradedate)
- ## sDate <- advance(calendar="WeekendsOnly", tradedate, 2, 0)
- sDate <- tradedate
setCalendarContext(calendar="WeekendsOnly", fixingDays=2,
- settleDate=sDate)
+ settleDate=tradedate)
+ settings <- Settings$new()
+ settings$EvaluationDate <- tradedate
legparams <- switch(currency,
USD = list(fixFreq="Semiannual",
floatFreq="Quarterly",
@@ -86,14 +85,14 @@ exportYC <- function(tradedate=Sys.Date(), currency=c("USD", "EUR"), useFutures
EUR = list(fixFreq="Annual",
floatFreq="Semiannual",
dayCounter="Thirty360"))
- params <- list(tradeDate=tradedate,
- settleDate=sDate,
- interpWhat="discount",
- interpHow="loglinear")
+ cal <- Calendar$new("WeekendsOnly")
+ dc <- DayCounter$new("Actual365Fixed")
+
if(exists("futurequotes")){
- quotes <- buildMarkitYC(MarkitData, currency, futurequotes[,2])
+ tsQuotes <- buildMarkitYC(MarkitData, currency, futurequotes[,2])
}else{
- quotes <- buildMarkitYC(MarkitData, currency)
+ tsQuotes <- buildMarkitYC(MarkitData, currency)
}
- YC <<- DiscountCurve(YC$params, YC$tsQuotes, YC$legparams)
+ YC <<- YieldTermStructure$new("discount", "loglinear", 0L, cal,
+ dc, tsQuotes, legparams)
}