This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.

# Real world tidy interest rate swap pricing Contributor:
curiousfrm.com
Visit: curiousfrm.com By:

Author of the Blog curiousfrm.com

In this post I will show how easy it is to price a portfolio of swaps leveraging the purrr package and given the swap pricing functions that we introduced in a previous post. I will do this in a “real world” environment using real market data from April 14.

Import the discount factors from Bloomberg

Let’s start the pricing of the swap portfolio with purrr by loading from an external source the EUR discount factor curve. My source is Bloomberg, and in particular the SWPM page, which allows all Bloomberg users to price interest rate sensitive instruments. It also contains a tab with the curve information, which is the source of my curve. It is partly represented in the screenshot below, as well as in the following table:

``````today <- lubridate::ymd(20190414)
ir_curve %>%
knitr::kable(caption = "Input from Bloomberg", "html") %>%
kableExtra::kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive")) %>%
kableExtra::scroll_box(width = "750px", height = "200px") ``````

https://www.curiousfrm.com/2019/04/real-world-tidy-interest-rate-swap-pricing/

We now wrangle this data in order to get a two-column tibble containing the time to maturity and the discount factors for each pillar points on the curve. We us a 30/360-day count convention because it is the standard for the EUR

``````df.table <- ir_curve %>%

dplyr::mutate(`Maturity Date` = lubridate::mdy(`Maturity Date`)) %>%

dplyr::rowwise(.) %>%

dplyr::mutate(t2m = RQuantLib::yearFraction(today, `Maturity Date`, 6)) %>%

na.omit %>%

dplyr::select(t2m, Discount) %>%

dplyr::rename(df = Discount) %>%

dplyr::ungroup(.) %>%

dplyr::bind_rows(c(t2m = 0,df = 1)) %>%

dplyr::arrange(t2m)

ggplot2::ggplot(df.table, ggplot2::aes(x = t2m, y = df)) +

ggplot2::geom_point() + ggplot2::geom_line(colour = "blue") +

ggplot2::ggtitle("Discount Factor curve at 14th of April 2019") +

ggplot2::xlab("Time to maturity") +

ggplot2::ylab("Discount Factor")
``````

.

Interest Rate Swap pricing functions

I am now going to re-use the pricing functions that have been already described in a previous post. I have tidied them up a bit and given proper names, but the description still fully holds.

Let’s start from the one that calculates the swap cashflows.

``````SwapCashflowYFCalculation <- function(today, start.date, maturity.date,

time.unit, dcc, calendar) {
0:((lubridate::year(maturity.date) - lubridate::year(start.date)) *

(4 - time.unit)) %>%

dates = start.date,

n = .x, timeUnit = time.unit,

bdc = 1,

emr = TRUE)) %>%

lubridate::as_date() %>% {

if (start.date < today) append(today, .) else .} %>%

purrr::map_dbl(~RQuantLib::yearFraction(today, .x, dcc)) %>%

tibble::tibble(yf = .) }``````

You may have noticed that I added one row `{if (start.date < today) append(today, .) else .}`. This allows  proper management of the pricing of swaps with a starting date before today.

I now proceed with calculating the actual par swap rate, which is a key input to the pricing formula. Notice in the function below that I use a linear interpolation on the log of the discount factors. This is in line with one of the Bloomberg options. It is proven that it:

1. provides step constant forward rates
2. locally stabilizes the bucketed sensitivities

Also, the (old) swap rate pricing function is the same. We only filter for future cashflows, as we want to be able to price swaps with a starting date before today.

``````OLDParSwapRate <- function(swap.cf){
swap.cf %<>%
dplyr::filter(yf >= 0)

num <- (swap.cf\$df - swap.cf\$df[dim(swap.cf)])

annuity <- (sum(diff(swap.cf\$yf)*swap.cf\$df[2:dim(swap.cf)]))
return(list(swap.rate = num/annuity,
annuity = annuity))

}

ParSwapRateCalculation <- function(
swap.cf.yf, df.table) { swap.cf.yf %>%
dplyr::mutate(df = approx(df.table\$t2m, log(df.table\$df), .\$yf) %>%
purrr::pluck("y") %>%
exp) %>%
OLDParSwapRate
}``````

I now want to introduce two new functions which are needed for calculating the actual market values:

• the first one extracts the year fraction for the accrual calculation
• the second one calculates the main characteristics of a swap:
• the par swap rate
• the pv01 (or analytic delta)
• the clean market value
• the accrual for the fixed rate leg

I have defined a variable `direction` which represents the type of swap:

• if it is equal to `1` then it is a receiver swap
• if it is equal to `-1` then it is a payer swap
``````CalculateAccrual <- function(swap.cf){

swap.cf %>% dplyr::filter(yf < 0) %>%

dplyr::select(yf) %>%

dplyr::arrange(dplyr::desc(yf)) %>%

dplyr::top_n(1) %>%
as.double %>%

{if (is.na(.)) 0 else .}

}

SwapCalculations <- function(swap.cf.yf, notional, strike, direction, df.table) {

swap.par.pricing <- ParSwapRateCalculation(swap.cf.yf, df.table)

mv <- notional * swap.par.pricing\$annuity * (strike - swap.par.pricing\$swap.rate) * direction

accrual.fixed <- swap.cf.yf %>%

CalculateAccrual %>%

`*`(notional * strike * direction * -1)

pv01 <- notional/10000 * swap.par.pricing\$annuity * direction

list(clean.mv = mv, accrual.fixed = accrual.fixed, par = swap.par.pricing\$swap.rate,

pv01 = pv01)

}``````

We then put everything together with the following pricing pipe:

``````SwapPricing <- function(today, swap, df.table) {
SwapCashflowYFCalculation(today, swap\$start.date,
swap\$maturity.date, swap\$time.unit,
swap\$dcc, swap\$calendar) %>%
SwapCalculations(swap\$notional, swap\$strike, swap\$direction, df.table)
}`````` 