Calculating a Forward Swap Rate with QuantLib
Summary
The document shows how to obtain the fair fixed rate for a forward-starting interest rate swap using QuantLib. It begins with a valuation date, a flat yield curve, and a six-month Euribor index. Rather than manually constructing fixed and floating schedules and calculating cash flows, the accepted response uses a helper to create a vanilla swap with a specified tenor and forward start, attaches a discounting swap engine, and reads the swap’s fair rate.
The example also prints the generated schedules and day-count conventions, making clear that the result depends on instrument conventions. The response notes that a forward swap rate is not fully defined until those conventions are chosen; many defaults come from the index. The code’s displayed rate is specific to its sample curve, dates, and conventions, so it should not be treated as a market quote or a universal rate. Reviewing the helper-generated schedule and accrual conventions is part of checking that the constructed swap matches the intended contract.
Key ideas
- QuantLib can build a forward-starting vanilla swap and return its fair fixed rate.
- A discounting swap engine and an attached yield curve are needed to value the example swap.
- Swap tenor, forward start, index, schedules, and day-count conventions affect the instrument definition.
- The helper-generated fixed and floating schedules can be inspected to confirm the conventions used.
- The sample rate is specific to the example inputs and is not a general market result.
Tags
Full text
# Forward Swap Rate calculation using Quantlib
# Forward Swap Rate calculation using Quantlib
Here, we have an example for the calculation of Forward Swap Rate - How to compute forward swap rates?
Below is my Forward Swap -
```
from QuantLib import *
import datetime
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
calc_date = Date(29, 3, 2019)
start = 10
length = 10
start_date = TARGET().advance(calc_date, start, Years)
maturity_date = start_date + Period(length, Years)
spot_curve = FlatForward(calc_date, QuoteHandle(SimpleQuote(0.01)), Actual365Fixed())
termStructure = YieldTermStructureHandle(spot_curve)
index = Euribor6M(termStructure)
fixedSchedule = Schedule(start_date, ## pd.DataFrame({'date': list(fixedSchedule)})
maturity_date,
Period(1, Years),
TARGET(),
Unadjusted,
Unadjusted,
DateGeneration.Forward,
False
)
floatingSchedule = Schedule(start_date, ## pd.DataFrame({'date': list(floatingSchedule)})
maturity_date,
Period(6, Months),
TARGET(),
ModifiedFollowing,
ModifiedFollowing,
DateGeneration.Forward,
True
)
swap = VanillaSwap(VanillaSwap.Receiver,
10000000,
fixedSchedule,
1.45 / 100,
Thirty360(Thirty360.BondBasis),
floatingSchedule,
index,
0.0,
index.dayCounter()
)
```
Is there any way to directly obtain the Forward Swap rate using `QuantLib`? I am trying to avoid explicit calculations using the given link.
Many thanks for your pointer.
## Answer by David Duarte (score 3, accepted)
https://quant.stackexchange.com/a/58055
You can't get the Forward Swap directly since you will have to give some conventions for what you want. However there is a less verbose way to construct a forward swap and get it's fairRate. Note that most conventions will come from the index you specified.
```
import QuantLib as ql
calc_date = ql.Date(29, 3, 2019)
spot_curve = ql.FlatForward(calc_date, ql.QuoteHandle(ql.SimpleQuote(0.01)), ql.Actual365Fixed())
termStructure = ql.YieldTermStructureHandle(spot_curve)
index = ql.Euribor6M(termStructure)
engine = ql.DiscountingSwapEngine(termStructure)
start = 10
length = 10
swapTenor = ql.Period(length, ql.Years)
forwardStart = ql.Period(start, ql.Years)
swap = ql.MakeVanillaSwap(swapTenor, index, 0.0, forwardStart, pricingEngine=engine)
print(f"Forward Rate Swap Rate: {swap.fairRate():.3%}")
```
Forward Rate Swap Rate: 1.006%
(Edit) To see the swap details:
```
print(swap.fixedDayCount().name())
print([dt.ISO() for dt in swap.fixedSchedule()])
print(swap.floatingDayCount().name())
print([dt.ISO() for dt in swap.floatingSchedule()])
```
30/360 (Bond Basis) ['2030-09-23', '2031-09-23', '2032-09-23', '2033-09-23', '2034-09-25', '2035-09-24', '2036-09-23', '2037-09-23', '2038-09-23', '2039-09-23', '2040-09-24'] Actual/360 ['2030-09-23', '2031-03-24', '2031-09-23', '2032-03-23', '2032-09-23', '2033-03-23', '2033-09-23', '2034-03-23', '2034-09-25', '2035-03-27', '2035-09-24', '2036-03-24', '2036-09-23', '2037-03-23', '2037-09-23', '2038-03-23', '2038-09-23', '2039-03-23', '2039-09-23', '2040-03-23', '2040-09-24']Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.