aboutsummaryrefslogtreecommitdiffstats
path: root/R
diff options
context:
space:
mode:
Diffstat (limited to 'R')
-rw-r--r--R/cds_utils.R35
1 files changed, 11 insertions, 24 deletions
diff --git a/R/cds_utils.R b/R/cds_utils.R
index 51409b55..e7ffce3a 100644
--- a/R/cds_utils.R
+++ b/R/cds_utils.R
@@ -1,4 +1,5 @@
library("RQuantLib")
+library("stringr")
today <- function() {
Sys.Date()
@@ -11,36 +12,22 @@ addBusDay <- function(tradedate = Sys.Date(), n.days = 3, calendar = "UnitedStat
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 )
+ match <- str_match(tenor, "([0-9]+)(m|y)")
+ if ( match[3] == "m" ) {
+ return ( 30 * as.integer(match[2]) )
+ }else if ( match[3]=="y") {
+ return ( 365 * as.integer(match[2]) )
}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] )
+ match <- str_match(tenor, "([0-9]+)(m|y)")
+ if ( match[3]=="m") {
+ return ( seq(date, length=2, by=paste(match[2],"month"))[2])
+ }else if ( match[3]=="y") {
+ return ( seq(date, length=2, by=paste(match[2],"year"))[2] )
}else{
stop("format not recognized")
}