source("cds_functions_generic.R") buildSC <- function(quote, cs, cdsdates){ SC <- new("creditcurve", recovery=quote$recovery, startdate=tradedate, issuer=quote$ticker) quotes <- data.frame(maturity=cdsdates, upfront = quote$upfront, running=quote$running) SC@curve <- cdshazardrate(quotes, SC@recovery, tradedate, cs) return( SC ) } get.cdsSchedule <- function(tradedate, indexmaturity){ cdsdates <- as.Date(character(0)) for(tenor in paste0(c(1:5, 7,10), "y")){ newdate <- cdsMaturity(tenor, date=tradedate) cdsdates <- c(cdsdates, newdate) if(newdate>=indexmaturity){ break } } return( list(cs=couponSchedule(IMMDate(tradedate, noadj=TRUE), cdsdates[length(cdsdates)], "Q", "FIXED", 1, tradedate, IMMDate(tradedate, "prev")), cdsdates=cdsdates) ) } set.singlenamesdata <- function(index, tradedate){ cds.cs <- get.cdsSchedule(tradedate, index$maturity) quotes <- get.indexquotes(index$name, tradedate) tenor <- names(cds.cs$cdsdates) index$portfolio <- list() for(i in seq_along(quotes$tickers)){ sane.quotes <- which(yearFrac(tradedate+2,cds.cs$cdsdates)*quotes$spread_curve[i, tenor]*1e-4+ quotes$upfront_curve[i, tenor] * 0.01>0) quote <- list(ticker = quotes$ticker[i], running = quotes$spread_curve[i, tenor[sane.quotes]] * 1e-4, upfront = quotes$upfront_curve[i, tenor[sane.quotes]] * 0.01, recovery = as.double(quotes$recovery[i,tenor[sane.quotes]][1])) index$portfolio <- c(index$portfolio, buildSC(quote, cds.cs$cs, cds.cs$cdsdates[sane.quotes])) } index$issuerweights <- rep(1/length(index$portfolio), length(index$portfolio)) index$recov <- sapply(index$portfolio, attr, "recovery") return( index ) } set.tranchedata <- function(index, tradedate){ temp <- get.tranchequotes(index$name, index$tenor, tradedate) index$quotes <- data.frame(spread=temp$indexrefspread[1]*1e-4, maturity=index$maturity) if(index$name=="ig19" || index$name=="ig21"){ index$quotes$spread <- 0.01 } index$cs <- couponSchedule(IMMDate(tradedate, noadj=TRUE), index$maturity,"Q", "FIXED", 1, 0, tradedate, IMMDate(tradedate, "prev")) if(!is.na(temp$indexrefprice[1])&&temp$indexrefprice[1]!=0){ index$quotes$price <- temp$indexrefprice[1]/100 }else{ ##rewrite as a snac function sc <- new("flatcurve", h=temp$indexrefspread[1]*1e-4/(1-index$recovery)) startdate <- tradedate + 1 cds.pv <- couponleg(index$cs, sc, startdate)*index$quotes$spread - defaultleg(index$cs, sc, index$recovery, startdate) index$quotes$price <- 1 + cds.pv - cdsAccrued(tradedate, index$quotes$spread[1]) } index$portfolio <- tweakcurves(index, tradedate)$portfolio index$defaultprob <- 1 - SPmatrix(index$portfolio, length(index$cs$dates)) negprob <- which(index$defaultprob<0, arr.ind=T) if(nrow(negprob)>0){ stop(paste(index$portfolio[[negprob[1,1]]]@issuer, "has negative probability, check single names data")) } K <- c(0, temp$detach/100) index$K <- adjust.attachments(K, index$loss, index$factor) index$tranche.upf <- temp$trancheupfront index$tranche.running <- temp$trancherunning*1e-4 ## compute dirty protection price if(length(grep("hy", index$name, ignore.case=TRUE))>0){ index$tranche.quotes <- 1-index$tranche.upf/100-cdsAccrued(tradedate, index$tranche.running) }else{ index$tranche.quotes <- index$tranche.upf/100-cdsAccrued(tradedate, index$tranche.running) } return( index ) }