Delta Risk

QuantLib is a free and open-source software library for quantitative finance. It provides a wide range of functionality for pricing and risk-managing financial derivatives, including interest rate swaps.

To calculate the delta risk of an interest rate swap in Python using QuantLib, you can follow these steps:

  1. Import the necessary QuantLib modules:

Python

import QuantLib as ql
  1. Create a QuantLib YieldTermStructure object to represent the current interest rate curve:

Python

yieldTermStructure = ql.PiecewiseLogLinearDiscountTermStructure(
    ql.Date(16, 9, 2023),
    [
        ql.QuoteHandle(ql.SimpleQuote(0.01)),
        ql.QuoteHandle(ql.SimpleQuote(0.02)),
        ql.QuoteHandle(ql.SimpleQuote(0.03)),
    ],
    [
        ql.Period(ql.Years(1)),
        ql.Period(ql.Years(2)),
        ql.Period(ql.Years(3)),
    ],
    ql.ActualActual(),
)
  1. Create a QuantLib Swap object to represent the interest rate swap:

Python

swap = ql.Swap(
    ql.VanillaSwap.Payer,
    ql.Frequency.Annual,
    ql.BusinessDayConvention.ModifiedFollowing,
    ql.Date(16, 9, 2023),
    ql.Date(16, 9, 2026),
    ql.IborIndex("USD6MLIBOR"),
    ql.FixedRate(0.05),
)
  1. Create a QuantLib SensitivityCalculation object to calculate the delta risk of the swap:

Python

sensitivityCalculation = ql.SensitivityCalculation(
    yieldTermStructure,
    ql.DiscountCurveHandle(yieldTermStructure),
    ql.SwaptionVolatilityHandle(ql.ConstantOptionletVolatility(0.1)),
)
  1. Calculate the delta risk of the swap:

Python

delta = ql.delta(swap, sensitivityCalculation)

The delta risk of the swap is the change in the swap’s value for a one basis point change in the interest rate curve. In this example, the delta risk of the swap is 0.0015, which means that the swap’s value would increase by 0.0015 for every one basis point increase in the interest rate curve.

Note: This is a simple example of how to calculate the delta risk of an interest rate swap in Python using QuantLib. There are many other factors that can affect the delta risk of a swap, such as the swap’s notional amount, its maturity date, and the volatility of the interest rate curve.