Pricing Floating Legs of Interest Rate Swaps with R


Davide Magno


Author of the Blog


In this post we will close the trilogy on (old style) swap pricing. In particular, we will download the variable rate data, which is needed to calculate the variable leg accrual.

Download the Data

To learn how to download the code via Quandl, visit Davide’s website To recap:

  • Part 1 of this series gave the general idea behind tidy pricing interest rate swaps using a 7 lines pipe
  • Part 2 went much more into detail and priced some real world contract comparing the results obtained vs Bloomberg.
  • The only part missing was calculating the accrual for the floating leg. To do this we need the information about the historical level of the interest rate to which the leg is linked. For standard EUR contracts, this rate is the 6 months’ EURIBOR. For those of you who are interested in understanding more what this rate is, see this link.

This post will continue below with a focus on the R packages and sample code.

R Packages and Functions

Let’s look at the code now. The function that gets modified the most is the SwapCashflowYFCalculation which I have re-named as CashFlowPricing one which now looks as follows:

CashFlowPricing  <- function(today,,, type,
                                     time.unit, dcc, calendar) {
  # Part 1: Calculate the whole cashflow dates
  cashflows <- seq(from = 0,
                   to = (lubridate::year( -
                           lubridate::year( * 12,
                   by = time.unit) %>%
    purrr::map_dbl(~RQuantLib::advance(calendar = calendar,
                                       dates =,
                                       n = .x,
                                       timeUnit = 2,
                                       bdc = 1,
                                       emr = TRUE)) %>%
    lubridate::as_date() %>%
    {if ( < today) append(today, .) else .}

  # Part 2: calculate accrual and rate fixing days <- cashflows[today - cashflows > 0]

  if (!identical(as.double(, double(0))) {  %<>%  max()
    if (stringr::str_detect(type, "floating")) { <- %>%
        {RQuantLib::advance(calendar = calendar,
                            dates = .,
                            n = -2,
                            timeUnit = 0,
                            bdc = 1,
                            emr = TRUE)}
    } else { <- NULL
    accrual.yf <- %>%
      {RQuantLib::yearFraction(today, ., dcc)} %>%
  } else { <- NULL
    accrual.yf <- 0

  # Part 3: Tidy and return the list of relevant dates
  cashflows %<>%
    purrr::map_dbl(~RQuantLib::yearFraction(today, .x, dcc)) %>%
    tibble::tibble(yf = .) %>%
    dplyr::filter(yf >= 0)

  return(list(cashflows = cashflows, accrual.yf = accrual.yf,

Reviewing the Output

Let’s analyze the code:

  • Part 1 is actually the core of the previously described code.
  • Part 2 is the new code. is the date from which the accrual starts to be calculated. This gets converted into a year fraction and saved into accrual.yf. The if statement calculates the date at which the floating EURIBOR rate has to be snapped from Quandl only for the floating rate. This date is stored in the variable and it considers a 2 days lag which is standard for the European market.
  • Part 3 finally converts and returns all the future cash flows in terms of year fraction

You can note that we now calculate the cashflows for the floating leg even if it will note be used by the OLDParSwapRate function. This will be needed for future developments when we will introduce the OIS discounting…(stay tuned!!)

I can now calculate the accrual, and for this purpose I developed a brand new function called CalculateAccrual

CalculateAccrual <- function(swap.dates, leg.type, swap, direction) {
  # Part 1: calculate the accrual rate
  if (!is.null(swap.dates$ {
    rate <- Quandl::Quandl("BOF/QS_D_IEUTIO6M",
                           start_date = swap.dates$,
                           end_date = swap.dates$ %>%
      tibble::as_tibble(.) %>%
      dplyr::select(Value) %>%
      as.double %>%
  } else {
    rate <- swap$strike
  # Part 2: Calculate the value of the accrual
  swap.dates %>%
    purrr::pluck("accrual.yf") %>%
    `*`(swap$notional * rate * switch(leg.type, "pay" = -1, "receive" = 1))

This is smaller and easier function:

  • Part 1: for floating legs we use Quandl to download the needed data and extract the rate information. For the fixed one, we just use the strike of the swap.
  • Part 2: we calculate the actual accrual amount using the classical function

Final Results

Let’s see the final result on the 25 years’ swap we use as test:

## # A tibble: 1 x 7
## accrual.receive     par    pv01
##   <chr>       <dbl>    <dbl>       <dbl>           <dbl>   <dbl>   <dbl>
## 1 Swap 25y -881815. -874994.       5441.           1379. 0.00771 -12394.

Visit to read the rest of the article, and learn how Davide compares the results vs Bloomberg data, and how his algorithm identifies the floating & fixed legs depending on the type of the swap:

Davide Magno is a professional financial engineer with more than 10 years of experience of managing complex financial quantitative tasks for banks, insurances and funds. He is passionate about both quantitative finance and data science: he is the author of the blog that aims at solving financial problems using modern data science coding languages and techniques. He is currently Head of Financial Risk Management in Axa Life Europe dac. Opinions expressed are solely his and do not express the views or opinions of his current employer.

Disclosure: Interactive Brokers

Information posted on IBKR Traders’ Insight that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Traders’ Insight are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from and is being posted with permission from The views expressed in this material are solely those of the author and/or and IBKR is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.

Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.

Disclosure: Forex

There is a substantial risk of loss in foreign exchange trading. The settlement date of foreign exchange trades can vary due to time zone differences and bank holidays. When trading across foreign exchange markets, this may necessitate borrowing funds to settle foreign exchange trades. The interest rate on borrowed funds must be considered when computing the cost of trades across multiple markets.