<using python>
To price a plain vanilla interest rate swap using QuantLib in Python, we need to set up the swap's structure, define the yield curve for discounting, and configure the relevant schedule and market conventions.
Below is a basic step-by-step guide broken into 7 steps:
Of course, we start with importing the library
import QuantLib as ql
Let's assume we are pricing a 5-year fixed-for-floating interest rate swap with a notional of $1,000,000, where the fixed leg pays annually and the floating leg pays semi-annually.
notional = 1000000 # Notional amount
fixed_rate = 0.02 # Fixed rate (2%)
floating_spread = 0.0 # Spread on floating leg (if any)
tenor_years = 5 # Swap tenor in years
fixed_leg_frequency = ql.Annual
floating_leg_frequency = ql.Semiannual
fixed_leg_day_count = ql.Thirty360()
floating_leg_day_count = ql.Actual360()
fixed_leg_convention = ql.ModifiedFollowing
floating_leg_convention = ql.ModifiedFollowing
Define a simple yield curve for discounting purposes. Here, we’ll use a flat yield curve with a given rate.
flat_rate = 0.03 # Flat discount rate (3%)
today = ql.Date.todaysDate()
ql.Settings.instance().evaluationDate = today
discount_curve = ql.YieldTermStructureHandle(ql.FlatForward(today, flat_rate, ql.Actual360()))
start_date = today
maturity_date = today + ql.Period(tenor_years, ql.Years)
fixed_schedule = ql.Schedule(start_date, maturity_date, ql.Period(fixed_leg_frequency),
ql.TARGET(), fixed_leg_convention, fixed_leg_convention,
ql.DateGeneration.Forward, False)
floating_schedule = ql.Schedule(start_date, maturity_date, ql.Period(floating_leg_frequency),
ql.TARGET(), floating_leg_convention, floating_leg_convention,
ql.DateGeneration.Forward, False)