knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
The SwapPricer
package allows you to price a book of interest rate swaps (IRS).
IRS is the most traded over the counter financial derivative in the world and now
you can easily price it in R.
Please note that as at version 0.2.0 the toolbox is able to price using a one-curve framework
We are working to improve the toolbox in the next releases including OIS discounting
Two main data structures are needed in order to run the SwapPriceR toolbox:
`%>%` <- magrittr::`%>%` `%<>%` <- magrittr::`%<>%`
Swap Structure
A swap can be defined as a list that includes the following needed fields:
Only if the field standard is set to FALSE then we need four additional fields
The following one is an example of valid setup for both a standard swap and a non standard one.
swap_standard <- list(currency = "EUR", notional = 10000000, start.date = lubridate::ymd(20070119), maturity.date = lubridate::ymd(20320119), strike = 0.00059820, type = "receiver", standard = TRUE) swap_nonstandard <- list(currency = "EUR", notional = 10000000, start.date = lubridate::ymd(20070119), maturity.date = lubridate::ymd(20320119), strike = 0.00059820, type = "receiver", standard = FALSE, time.unit = list(pay = 6, receive = 12), dcc = list(pay = 1, receive = 6), calendar = "TARGET")
Alternatively (and possibly more conveniently), you can upload your swap portfolio via a csv or excel file organised as the following table:
SwapPricer::swap.basket %>% dplyr::mutate_at(dplyr::vars("notional"), ~formattable::comma(., digits = 0)) %>% dplyr::mutate_at(dplyr::vars("strike"), formattable::percent) %>% knitr::kable(caption = "Swap portfolio", "html", align = "c") %>% kableExtra::scroll_box(width = "690px")
Another important thing to note is that if you upload the csv file, the dates
must be in a dmy format. We are working on relaxing this constraint, but we
haven't found the most suitable toolbox to help us (yes, we also tried anytime
and we were not fully impressed)
This input will then be treated by a function in the toolbox to put in the list format we have specified above.
Discount Factor
This has to be sourced from a third party provider (...for now...). In this case we downloaded the following data from Bloomberg.
SwapPricer:::ir.curve %>% knitr::kable(caption = "Input from Bloomberg", "html") %>% kableExtra::kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive")) %>% kableExtra::scroll_box(height = "500px")
From this data we extracted the two most important columns, which will be used in the code of the toolbox:
This data has to be stored in a tibble called discount. This tibble has to be part of a list which will also contain the code of the curve currency.
Here's the structure of an example of interest rate list:
str(SwapPricer::EUR.curves)
Now you have the two most important inputs that are needed to run your pricer. It is important to note that the interest rate curves' lists have to cover all the currencies present in the swap portfolio.
today <- lubridate::ymd(20190415) result <- SwapPricer::SwapPortfolioPricing(SwapPricer::swap.basket, today, SwapPricer::EUR.curves, SwapPricer::GBP.curves, SwapPricer::USD.curves)
result %>% dplyr::mutate_at(dplyr::vars("clean.mv","dirty.mv","accrual.pay","accrual.receive","pv01"), formattable::comma) %>% dplyr::mutate_at(dplyr::vars("par"), formattable::percent) %>% knitr::kable(caption = "Pricing results", "html") %>% kableExtra::kable_styling(bootstrap_options = c("striped", "hover", "condensed","responsive"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.