Я следовал инструкциям GB на этом веб-сайте http://gouthamanbalaraman.com/blog/quantlib-bond-modeling.html
и коды приведены ниже - определены облигации с фиксированной ставкой, созданы бондинги с временной структурой
import matplotlib
matplotlib.use('macosx')
import matplotlib.pyplot as plt
import QuantLib as ql
import pandas as pd
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)
# define the fixed rate bond.
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)
# coupons
dayCount = ql.Thirty360()
couponRate = .06
coupons = [couponRate]
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)
print(fixedRateBond.NPV())
#calculating yields
targetPrice = fixedRateBond.cleanPrice()
day_count = dayCount
compounding = ql.Compounded
frequency = 2
ytm = fixedRateBond.bondYield(targetPrice, day_count, compounding, frequency)
print(ytm)
Теперь как мне получить мод. продолжительность для связи? Я понимаю, что нужно использовать функцию ql.BondFunctions.duration(bond,ytm,ql.Duration.Modified)
, но у меня это не сработало.