For pricing a fix vs. float swap in QuantLib Python two YieldTermStructure Objects are necessary: one for forward rate estimation connected to a FloatingRateIndex Object, and one for discounting the cash flows.

import QuantLib as ql

print (ql.__version__) # 1.24

refDate = ql.Date(19,5,2021)

dscTS = ql.ZeroCurve([refDate + x for x in [1, 365, 3650]], [0.01, 0.02, 0.03], ql.Actual365Fixed(), ql.NullCalendar(), ql.Linear())
fwdTS = ql.ZeroCurve([refDate + x for x in [1, 365, 3650]], [0.015, 0.025, 0.035], ql.Actual365Fixed(), ql.NullCalendar(), ql.Linear())

print (type(fwdTS))   # <class 'QuantLib.QuantLib.ZeroCurve'>
print (fwdTS.nodes()) # ((Date(20,5,2021), 0.015), (Date(19,5,2022), 0.025), (Date(17,5,2031), 0.035))

swap = ql.MakeVanillaSwap(ql.Period('2Y'), ql.Euribor6M(ql.RelinkableYieldTermStructureHandle(fwdTS)),
                          0.02, ql.Period('0D'), discountingTermStructure=ql.RelinkableYieldTermStructureHandle(dscTS))

# retrieve Forward Term Structure 
first_coupon = ql.as_floating_rate_coupon(swap.floatingLeg()[0])
ibor = ql.as_iborindex(first_coupon.index())
fwdTSRetrieved = ibor.forwardingTermStructure().currentLink()

# Question 1: retrieve Discounting Term Structure
dscTSRetrieved = ??

# Question 2: getting the nodes from the retrieved Term Structure
print (type(fwdTSRetrieved))    # <class 'QuantLib.QuantLib.YieldTermStructure'>
print (fwdTSRetrieved.nodes())  # AttributeError: 'YieldTermStructure' has no attribute 'nodes'

Question 1: The forward term structure can be retrieved. Is there also a way to retrieve the discounting term structure?

Question 2: With the term structure object retrieved, is there a way (similar to what is described here) to cast the YieldTermStructure Object in order to use the nodes() method?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.