<jupyter_start><jupyter_text>[Modeling Fixed Rate Bonds Using QuantLib](http://gouthamanbalaraman.com/blog/quantlib-bond-modeling.html)[<NAME>](http://gouthamanbalaraman.com) Let's consider a hypothetical bond with a par value of 100, that pays 6% couponsemi-annually issued on January 15th, 2015 and set to mature on January 15th, 2016.The bond will pay a coupon on July 15th, 2015 and January 15th, 2016. The paramount of 100 will also be paid on the January 15th, 2016.To make things simpler, lets assume that we know the spot rates of the treasury asof January 15th, 2015. The annualized spot rates are 0.5% for 6 monthsand 0.7% for 1 year point. Lets calculate the fair value of this bond.<jupyter_code>3/pow(1+0.005, 0.5) + (100 + 3)/(1+0.007)<jupyter_output><empty_output><jupyter_text>Lets calculate the same using QuantLib<jupyter_code>import QuantLib as ql
todaysDate = ql.Date(15, 1, 2015)
ql.Settings.instance().evaluationDate = todaysDate
spotDates = [ql.Date(15, 1, 2015), ql.Date(15, 7, 2015), ql.Date(15, 1, 2016)]
spotRates = [0.0, 0.005, 0.007]
dayCount = ql.Thirty360()
calendar = ql.UnitedStates()
interpolation = ql.Linear()
compounding = ql.Compounded
compoundingFrequency = ql.Annual
spotCurve = ql.ZeroCurve(spotDates, spotRates, dayCount, calendar, interpolation,
                             compounding, compoundingFrequency)
spotCurveHandle = ql.YieldTermStructureHandle(spotCurve)
issueDate = ql.Date(15, 1, 2015)
maturityDate = ql.Date(15, 1, 2016)
tenor = ql.Period(ql.Semiannual)
calendar = ql.UnitedStates()
bussinessConvention = ql.Unadjusted
dateGeneration = ql.DateGeneration.Backward
monthEnd = False
schedule = ql.Schedule (issueDate, maturityDate, tenor, calendar, bussinessConvention,
                            bussinessConvention , dateGeneration, monthEnd)
list(schedule)
# Now lets build the coupon
dayCount = ql.Thirty360()
couponRate = .06
coupons = [couponRate]

# Now lets construct the FixedRateBond
settlementDays = 0
faceValue = 100
fixedRateBond = ql.FixedRateBond(settlementDays, faceValue, schedule, coupons, dayCount)

# create a bond engine with the term structure as input;
# set the bond to use this bond engine
bondEngine = ql.DiscountingBondEngine(spotCurveHandle)
fixedRateBond.setPricingEngine(bondEngine)

# Finally the price
fixedRateBond.NPV()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09533988884138521, 'fraction_numerical': 0.05557930739632321, 'mean_word_length': 5.12565445026178, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '393752', 'license': 'MIT', 'n_tokens_mistral': 892, 'n_tokens_neox': 764, 'n_words': 231, 'path': 'notebook/Quantlib_example/modeling-bonds.ipynb', 'repo_name': 'domosute/conda-quantlib', 'size': '2352'}