Provides a basic introduction to valuing interest rate swaps using QuantLib Python.

An Interest Rate Swap is a financial derivative instrument in which two parties agree to exchange interest rate cash flows based on a notional amount from a fixed rate to a floating rate or from one floating rate to another floating rate.

Here we will consider an example of a plain vanilla USD swap with 10 million notional and 10 year maturity. Let the fixed leg pay 2.5% coupon semiannually, and the floating leg pay Libor 3m quarterly.

In [1]:

Expand Code

In [2]:

```
# construct discount curve and libor curve
risk_free_rate = 0.01
libor_rate = 0.02
day_count = ql.Actual365Fixed()
discount_curve = ql.YieldTermStructureHandle(
ql.FlatForward(calculation_date, risk_free_rate, day_count)
)
libor_curve = ql.YieldTermStructureHandle(
ql.FlatForward(calculation_date, libor_rate, day_count)
)
#libor3M_index = ql.Euribor3M(libor_curve)
libor3M_index = ql.USDLibor(ql.Period(3, ql.Months), libor_curve)
```

In [3]:

```
calendar = ql.UnitedStates()
settle_date = calendar.advance(calculation_date, 5, ql.Days)
maturity_date = calendar.advance(settle_date, 10, ql.Years)
fixed_leg_tenor = ql.Period(6, ql.Months)
fixed_schedule = ql.Schedule(settle_date, maturity_date,
fixed_leg_tenor, calendar,
ql.ModifiedFollowing, ql.ModifiedFollowing,
ql.DateGeneration.Forward, False)
float_leg_tenor = ql.Period(3, ql.Months)
float_schedule = ql.Schedule (settle_date, maturity_date,
float_leg_tenor, calendar,
ql.ModifiedFollowing, ql.ModifiedFollowing,
ql.DateGeneration.Forward, False)
```

`VanillaSwap`

object by including the fixed and float leg schedules created above.

In [4]:

```
notional = 10000000
fixed_rate = 0.025
fixed_leg_daycount = ql.Actual360()
float_spread = 0.004
float_leg_daycount = ql.Actual360()
ir_swap = ql.VanillaSwap(ql.VanillaSwap.Payer, notional, fixed_schedule,
fixed_rate, fixed_leg_daycount, float_schedule,
libor3M_index, float_spread, float_leg_daycount )
```

We evaluate the swap using a discounting engine.

In [5]:

```
swap_engine = ql.DiscountingSwapEngine(discount_curve)
ir_swap.setPricingEngine(swap_engine)
```

`ir_swap`

object. The fixed leg cashflows are shown below:

In [6]:

Expand Code

The floating leg cashflows are shown below:

In [7]:

Expand Code

Some other analytics such as the fair value, fair spread etc can be extracted as shown below.

In [8]:

Expand Code

Here we saw a simple example on how to construct a swap and value them. We evaluated the fixed and floating legs and then valued the `VanillaSwap`

using the `DiscountingSwapEngine`

.

Click here to download the ipython notebook on interest rate swaps.

quantlib python finance

- Heston Model Calibration Using QuantLib Python and Scipy Optimize
- Valuing Interest Rate Caps and Floors Using QuantLib Python
- Modeling Volatility Smile and Heston Model Calibration Using QuantLib Python
- Modeling Fixed Rate Bonds in QuantLib Python
- Variance Reduction in Hull-White Monte Carlo Simulation Using Moment Matching

I am Goutham Balaraman, and I explore topics in quantitative finance, programming, and data science. You can follow me @gsbalaraman.

Updated posts from this blog and transcripts of Luigi's screencasts on YouTube is compiled into QuantLib Python Cookbook .