Skip to content
All library documents

Constructing a Zero-Coupon Swap with QuantLib’s Python API

Article Quant Q&A · Author: John83

Summary

The question asks how to price a zero-coupon swap in QuantLib after receiving an attribute error for the requested instrument. The accepted reply shows a Python example using QuantLib 1.30: it sets an evaluation date, builds a discount curve from discount factors and dates, wraps the curve in a yield-term-structure handle, and creates a custom floating-rate index. It then constructs the swap from start and end dates, notional, fixed rate, day-count convention, index, and calendar, attaches a discounting swap engine, and calculates net present value.

The example provides a working pattern and reports an NPV for its own inputs. It does not directly repair the original snippet, whose curve data are empty and whose instrument arguments differ. The sample also uses a Swedish index and calendar rather than the requested Brazilian CDI setup, so adapting it requires checking the installed QuantLib bindings, instrument signature, curve conventions, dates, and market data.

Key ideas

  • QuantLib’s Python bindings can expose a ZeroCouponSwap constructor even when a particular setup reports that the attribute is missing.
  • A pricing example requires a dated discount curve, a yield-curve handle, and an index for the floating leg.
  • The swap is priced by attaching a discounting swap engine and requesting its net present value.
  • The accepted example uses different currency and calendar conventions from the question’s Brazilian case.
  • Check that curve inputs and constructor arguments match the installed library version and intended market conventions.

Tags

Full text
# QuantLib: How to price or construct a zero coupon swap using Quantlib


# QuantLib: How to price or construct a zero coupon swap using Quantlib












I am trying to construct and price the zero coupon swap. However its giving me the AttributeError: module 'Quantlib' has no attribute 'ZeroCouponSwap'. Please let me know how to price the zero coupon swap using below details.

```
# construct discount curve and libor curve

maturity = []
discountfactor = []
day_count = ql.Actual360()
calendar = ql.JointCalendar(ql.UnitedStates(), ql.UnitedKingdom())
yield_curve = ql.DiscountCurve(maturity, discountfactor, day_count, calendar)
yield_curve.enableExtrapolation()
curve_handle = ql.YieldTermStructureHandle(yield_curve)

# BRL index
 BRL_index = ql.OvernightIndex('cdi', 0, ql.BRLCurrency(), ql.Brazil(), ql.Business252())
notional = 10000000
fixed_rate = 0.025
fixed_leg_daycount = ql.Business252()
float_spread = 0.0
float_leg_daycount = ql.Business252()
payment frequency = ql.Once

# creating schedule for zero coupon swap

fixed_schedule = ql.MakeSchedule(EffectiveDate, terminationDate, ql.Once)

 float_schedule = ql.MakeSchedule (EffectiveDate, terminationDate, ql.Once, calendar, ql.ModifiedFollowing, False)

 swap = ql.ZeroCouponSwap(ql.Receiver, 1000000, fixed_schedule, 
                     fixed_rate, fixed_leg_daycount, curve_handle)

 engine = ql.DiscountingSwapEngine(yield_curve)
 swap.setPricingEngine(engine)
```

## Answer by Xiarpedia (score 2, accepted)

https://quant.stackexchange.com/a/77440

I am running QuantLib version 1.30 and it works for me. Here is the code I compiled to investigate as unfortunately yours did not work for me

```
import QuantLib as ql

# Set the reference date
ql.Settings.instance().evaluationDate = ql.Date(1, 1, 2022)

dfs = [1, 0.965, 0.94]  # discount factors
dates = [
    ql.Date(1, 1, 2022),
    ql.Date(1, 1, 2023),
    ql.Date(1, 1, 2024),
]  # maturity dates of the discount factors

day_counter = ql.Actual360()

# Create the discount curve
curve = ql.DiscountCurve(dates, dfs, day_counter)

# The curve will note be linked in case we want to update the quotes later on
ts_handle = ql.YieldTermStructureHandle(curve)

custom_index= ql.IborIndex(
    "MyIndex",
    ql.Period("3m"),
    0,
    ql.SEKCurrency(),
    ql.Sweden(),
    ql.ModifiedFollowing,
    False,
    ql.Actual360(),
    ts_handle,
)

start_date= ql.Date(3,2,2022)
end_date = ql.Date(3,6,2022)

swap = ql.ZeroCouponSwap(
    ql.Swap.Receiver,
    1000000,
    start_date,
    end_date,
    0.02,
    day_counter,
    custom_index,
    ql.Sweden(),
)

engine = ql.DiscountingSwapEngine(ts_handle)
swap.setPricingEngine(engine)
print(swap.NPV())
```

Which gives me the NPV -5090. As I have not previously worked with ZeroCouponSwap I used the following sources to investigate your issue:

- https://github.com/lballabio/QuantLib-SWIG/blob/master/SWIG/swap.i

- https://github.com/lballabio/QuantLib/blob/master/test-suite/zerocouponswap.cpp

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.