I am stuck trying to price a CmsRateBond in QuantLib. Here's the error that occurred when I run print(ql.as_coupon(bond.cashflows()[-2]).rate())
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
c:\Users\...\question.py in
----> 74 print(ql.as_coupon(bond.cashflows()[-2]).rate())
~\Anaconda3\lib\site-packages\QuantLib\QuantLib.py in rate(self)
9694
9695 def rate(self):
-> 9696 return _QuantLib.Coupon_rate(self)
9697
9698 def accrualPeriod(self):
RuntimeError: pricer not set
If I'm not mistaken I can solve the issue by including a pricer (ql.CmsCouponPricer?). Unfortunately I haven't found anything helpful on the web / in python cookbook regarding CMS bonds. I copy my code below (vectors spot_rates, forwards are imported from csv file):
import QuantLib as ql
issue_date = ql.Date(4, 4, 2013)
maturity_date = ql.Date(4, 4, 2025)
float_coupon_daycount = ql.Actual360()
float_coupon_period = ql.Period("1Y")
quotedMargin = 0.0195
faceAmount = 100.0
last_floating_rate = -0.000149
settlement_days = 2
# valuation date
calc_date = ql.Date(14, 12, 2020)
ql.Settings.instance().evaluationDate = calc_date
# Discounting curve
discounting_curve = ql.ZeroCurve(
spot_dates,
spot_rates,
ql.ActualActual(),
)
discounting_curve_handle = ql.YieldTermStructureHandle(discounting_curve)
# Forecasting curve
forecasting_curve = ql.ForwardCurve(dates, forwards, float_coupon_daycount)
# Index
index = ql.EuriborSwapIsdaFixA(
ql.Period("10Y"), ql.YieldTermStructureHandle(forecasting_curve)
)
#Schedule
schedule = ql.Schedule(
issue_date,
maturity_date,
ql.Period("1Y"),
index.fixingCalendar(),
ql.ModifiedFollowing,
ql.ModifiedFollowing,
ql.DateGeneration.Backward,
False,
)
# CMS bond
bond = ql.CmsRateBond(
settlement_days,
faceAmount,
schedule,
index,
index.dayCounter(),
ql.Unadjusted,
fixingDays=index.fixingDays(),
gearings=[1],
spreads=[quotedMargin],
caps=[],
floors=[0],
)
# add last fixing rate
fixingDates = [
cf.fixingDate() for cf in map(ql.as_floating_rate_coupon, bond.cashflows()[:-1])
]
missing_fixingDate = list(filter(lambda x: x < calc_date, fixingDates))[-1]
index.addFixing(missing_fixingDate, last_floating_rate)
print(ql.as_coupon(bond.cashflows()[-2]).rate()) # RuntimeError: pricer not set
I'm using QuantLib 1.19 installed via pip.
I would like to verify the coupons. Could you please help me out and provide the code needed to test my code? Thank you.
First you have to setup a cms pricer. You can have a look at the SWIG tests ( https://github.com/lballabio/QuantLib-SWIG/blob/master/Python/test/cms.py )
Here is a simple example:
volQuote = ql.QuoteHandle(ql.SimpleQuote(0.2))
swaptionVol = ql.ConstantSwaptionVolatility(0, ql.TARGET(), ql.ModifiedFollowing, volQuote, ql.Actual365Fixed())
swvol_handle = ql.SwaptionVolatilityStructureHandle(swaptionVol)
mean_reversion = ql.QuoteHandle(ql.SimpleQuote(0.01))
cms_pricer = ql.LinearTsrPricer(swvol_handle, mean_reversion)
Then you can assign that pricer to the bond coupons:
ql.setCouponPricer(bond.cashflows(), cms_pricer)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.