首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >QuantLib :如何计算修改后的债券存续期?

QuantLib :如何计算修改后的债券存续期?
EN

Stack Overflow用户
提问于 2020-01-31 07:44:28
回答 1查看 1.4K关注 0票数 0

我遵循了GB在这个网站上的说明http://gouthamanbalaraman.com/blog/quantlib-bond-modeling.html

代码如下-定义了固定利率债券,创建了具有期限结构的债券引擎

代码语言:javascript
运行
复制
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()
businessConvention = ql.Unadjusted
dateGeneration = ql.DateGeneration.Backward
monthEnd = False
schedule = ql.Schedule (issueDate, maturityDate, tenor, calendar, businessConvention, businessConvention, 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)

现在,我如何获得mod。保证金的期限?我知道要使用的函数是ql.BondFunctions.duration(bond,ytm,ql.Duration.Modified),但这对我不起作用。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-31 22:14:21

持续时间方法的输入率必须是一个InterestRate对象,而不是一个简单的浮点数,否则您必须通过约定。第一个参数是键,在你的例子中是fixedRateBond

试试这个:

代码语言:javascript
运行
复制
rate = ql.InterestRate(ytm, ql.ActualActual(), ql.Compounded, ql.Annual)
ql.BondFunctions.duration(fixedRateBond,rate,ql.Duration.Modified)

或者这样:

代码语言:javascript
运行
复制
ql.BondFunctions.duration(fixedRateBond,ytm,ql.ActualActual(), ql.Compounded, ql.Annual, ql.Duration.Modified)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59996148

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档