I built a zero-coupon curve out of a generic par swap rate curve (Step 1) and I am trying to recover the swap curve back from the zero-coupon curve (Step 2).

Step 1 works but not Step 2. I get close quotes but they do not exactly match. Anyone has any idea what's wrong in my Step 2?

My guess is that it does not come from calendar issues, as I am using theoretical calendar, daycounter, and index, with no adjustment.

Here's my code:


# define constants
face_amount = 100
settlementDays = 0
calendar = ql.NullCalendar()
fixedLegAdjustment = ql.Unadjusted
floatingLegAdjustment = ql.Unadjusted
fixedLegDayCounter = ql.SimpleDayCounter()
floatingLegDayCounter = ql.SimpleDayCounter()
fixedLegFrequency = ql.Semiannual
end_of_month = False
floating_rate = ql.IborIndex("MyIndex", ql.Period(3, ql.Months), settlementDays, ql.USDCurrency(), calendar, floatingLegAdjustment, end_of_month, floatingLegDayCounter)

# irs is a DataFrame with one line and the column as maturities (from 3M to 120M)
deposits = [irs.columns[0]]
swaps = irs.columns[1:]

# curve dates
zero_rates = {}
curve_date = ql.DateParser.parseFormatted(str("2017-01-01"), "%Y-%m-%d")
ql.Settings.instance().evaluationDate = curve_date
spot_date = calendar.advance(curve_date, settlementDays, ql.Days)

# deposit helper
deposit_helpers_mat = []
for tenor in deposits:
    deposit_helpers_mat.append([ql.Period(int(tenor), ql.Months), ql.QuoteHandle(ql.SimpleQuote(irs[int(tenor)] / 100))])

deposit_helper = [ql.DepositRateHelper(tenors_deposit, settlementDays, calendar, fixedLegAdjustment, end_of_month, fixedLegDayCounter) for tenors_deposit, deposit_rates in deposit_helpers_mat]

# swap helper
swap_helpers_mat = []
for tenor in swaps:
    swap_helpers_mat.append([ql.Period(int(tenor), ql.Months), ql.QuoteHandle(ql.SimpleQuote(irs[int(tenor)] / 100))])

swap_helper = [ql.SwapRateHelper(swap_rates, tenors_swap, calendar, fixedLegFrequency, fixedLegAdjustment, fixedLegDayCounter, floating_rate) for tenors_swap, swap_rates in swap_helpers_mat]

# aggregate helpers
helper = deposit_helper + swap_helper

# build curve
zc_curve = ql.PiecewiseCubicZero(curve_date, helper, ql.SimpleDayCounter())
zero_rate = []
tenors = []
# loop over maturities
for tenor in np.arange(3, 120 + 1, 3):
    maturity_date = calendar.advance(spot_date, ql.Period(int(tenor), ql.Months))
    zero_rate_curve = (zc_curve.zeroRate(maturity_date, ql.SimpleDayCounter(), ql.Compounded, ql.Annual).rate()* 100)

# build the zero curve representation into a DataFrame
zero_rates = pd.DataFrame(np.transpose(list(zip(zero_rate))), columns=list(tenors))


# constant
fixedRate = 0.02
spread =0
TENORS = np.arange(3, 120 + 1, 3)

# pre-allocate
irs_rates = {}
# calculate dates
curve_date = ql.DateParser.parseFormatted(str("2017-01-01"), "%Y-%m-%d")
ql.Settings.instance().evaluationDate = curve_date
spot_date = calendar.advance(curve_date, settlementDays, ql.Days)

# zero curve
irs_rate = []
tenors = []
maturity_dates = []
zc_rates = []
# loop over maturities
for tenor in TENORS:
    # maturity date
    maturity_date = calendar.advance(spot_date, ql.Period(int(tenor), ql.Months))
    # gather maturity dates
    # gather zc rates
    zc_rates.append(zero_rates[int(tenor)] / 100)

# build zero coupon curve object
zero_curve = ql.YieldTermStructureHandle(ql.CubicZeroCurve(maturity_dates, zc_rates, fixedLegDayCounter, calendar))
# libor curve
libor_curve = ql.YieldTermStructureHandle(ql.CubicZeroCurve(maturity_dates, zc_rates, floatingLegDayCounter, calendar))
# floating rate
floating_rate = ql.IborIndex("MyIndex", ql.Period(3, ql.Months), settlementDays, ql.USDCurrency(), calendar, floatingLegAdjustment, end_of_month, floatingLegDayCounter, libor_curve)

# build swap curve
# loop over maturities
j = 0
for maturity in maturity_dates:
    # fixed leg tenor
    fixedLegTenor = ql.Period(3, ql.Months)
    # fixed leg coupon schedule
    fixedLegSchedule = ql.Schedule(spot_date, maturity, fixedLegTenor, calendar, fixedLegAdjustment, fixedLegAdjustment, ql.DateGeneration.Forward, end_of_month)

    # floating leg tenor
    floatingLegTenor = ql.Period(3, ql.Months)
    # floating leg coupon schedule
    floatingLegSchedule = ql.Schedule(spot_date, maturity, floatingLegTenor, calendar, floatingLegAdjustment, floatingLegAdjustment, ql.DateGeneration.Forward, end_of_month)

    # build swap pricer
    swap_rate = ql.VanillaSwap(ql.VanillaSwap.Payer, face_amount, fixedLegSchedule, fixedRate, fixedLegDayCounter, floatingLegSchedule, floating_rate, spread, floatingLegDayCounter)

    # build swap curve
    swap_curve = ql.DiscountingSwapEngine(zero_curve)
    # get swap rate

    # gather par irs rate
    irs_rate.append(swap_rate.fairRate() * 100)
    # gather irs tenor
    tenor = int(TENORS[j])
    j = j + 1
    # build the swap curve representation into a DataFrame
    irs_rates = pd.DataFrame(np.transpose(list(zip(irs_rate))), columns=list(tenors))

Many thanks in advance for your help!


2 Answers 2


Maybe you should start with a simple example, because you have so many moving parts that it's hard to figure out where the difference is. Most likely some different convention between your helpers and the instruments you are trying to price.

import QuantLib as ql

today = ql.Date().todaysDate()
calendar = ql.TARGET()
spot = calendar.advance(today, 2, ql.Days)

helpers = ql.RateHelperVector()
helpers.append( ql.DepositRateHelper(0.01, ql.Euribor6M()) )

swaps = [
    ('1Y', 0.015),
    ('2Y', 0.02),
    ('3Y', 0.025)
for tenor, rate in swaps:
    swapIndex = ql.EurLiborSwapIsdaFixA(ql.Period(tenor))
    helpers.append( ql.SwapRateHelper(rate, swapIndex) )

curve = ql.PiecewiseLogLinearDiscount(spot, helpers, ql.Actual360())
yts = ql.YieldTermStructureHandle(curve)
engine = ql.DiscountingSwapEngine(yts)

index = ql.Euribor6M(yts)

print("maturity, market, model")
for tenor, rate in swaps:
    swap = ql.MakeVanillaSwap(ql.Period(tenor), index, 0.01, ql.Period('0D'), pricingEngine=engine)    
    print(f"{tenor}, {rate:.6f}, {swap.fairRate():.6f}")

maturity, market, model
1Y, 0.015000, 0.015000
2Y, 0.020000, 0.020000
3Y, 0.025000, 0.025000

  • $\begingroup$ Thanks for your example David. A couple of questions: - Is it normal (by definition) that the conversion of the 3M zero-rate into a 3M swap gives me nan values? - In the first conversion (from swaps to zero-rates), I do not use any curve to specify my floating_rate. However in the second conversion (from zero-rates to swaps), I do (and so do you). Is that a problem? - Last, it seems that using "ql.DateGeneration.Zero" makes my code crash in the second step, and only Forward works. Where can I specify that is the SwapHelper to be sure the same convention is used? $\endgroup$
    – Jessica F.
    Commented Jun 2, 2020 at 17:48

To answer the questions in your comment:

  1. What is a 3M swap rate? Either it's a fixed rate vs a shorter tenor (ex:1m) or a fixed rate vs the same tenor but forward (in this case a FRA), or if it's starting spot then it's the same as a zero rate because it has to intermediate payments.

  2. The floating rate conventions are in the definition of the floating index. Also, there are templates you can use: ql.USDLibor(ql.Period('3M'))

  3. To specify your helper, you can either use a template where the conventions are already defined ( for example: ql.UsdLiborSwapIsdaFixAm ) or use one of the several constructors. Check here for more info: https://quantlib-python-docs.readthedocs.io/en/latest/thelpers.html#swapratehelper


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.