# QuantLib 金融计算——案例之固息债的价格、久期、凸性和 BPS

## 计算久期和凸性

• 债券起息日：2020-03-10
• 到期兑付日：2030-03-10
• 债券期限：10 年
• 面值(元)：100.00
• 计息基准：A/A
• 息票类型：附息式固定利率
• 付息频率：年
• 票面利率（%）：3.0700
• 结算方式：T+1
import QuantLib as ql
import prettytable as pt

today = ql.Date(28, ql.July, 2020)
ql.Settings.instance().evaluationDate = today

settlementDays = 1
faceAmount = 100.0


settlementDays = 1 表示 T+1 结算，而估值日期就是 2020-07-28 这一天。

effectiveDate = ql.Date(10, ql.March, 2020)
terminationDate = ql.Date(10, ql.March, 2030)
tenor = ql.Period(1, ql.Years)
calendar = ql.China(ql.China.IB)
terminationDateConvention = convention
rule = ql.DateGeneration.Backward
endOfMonth = False

schedule = ql.Schedule(
effectiveDate,
terminationDate,
tenor,
calendar,
convention,
terminationDateConvention,
rule,
endOfMonth)

# for s in schedule:
#     print(s)

coupons = ql.DoubleVector(1)
coupons[0] = 3.07 / 100.0
accrualDayCounter = ql.ActualActual(
ql.ActualActual.Bond, schedule)

bond = ql.FixedRateBond(
settlementDays,
faceAmount,
schedule,
coupons,
accrualDayCounter,
paymentConvention)


DiscountingBondEngine 是最常见的债券定价引擎，主要用于现金流的贴现计算。

bondYield = 3.4124 / 100.0

compounding = ql.Compounded
frequency = ql.Annual

termStructure = ql.YieldTermStructureHandle(
ql.FlatForward(
settlementDays,
calendar,
bondYield,
accrualDayCounter,
compounding,
frequency))

engine = ql.DiscountingBondEngine(termStructure)
bond.setPricingEngine(engine)


cleanPrice = bond.cleanPrice()
dirtyPrice = bond.dirtyPrice()
accruedAmount = bond.accruedAmount()

duration = ql.BondFunctions.duration(
bond,
bondYield,
accrualDayCounter,
compounding,
frequency)

convexity = ql.BondFunctions.convexity(
bond,
bondYield,
accrualDayCounter,
compounding,
frequency)

bps = ql.BondFunctions.basisPointValue(
bond,
bondYield,
accrualDayCounter,
compounding,
frequency)

tab = pt.PrettyTable(['item', 'QuantLib', 'ShClearing'])

tab.float_format = '.4'

print(tab)

+----------------+----------+------------+
|      item      | QuantLib | ShClearing |
+----------------+----------+------------+
|  clean price   | 97.2212  |  97.2211   |
|  dirty price   | 98.4071  |  98.4071   |
| accrued amount |  1.1859  |   1.1859   |
|    duration    |  8.0771  |   8.0771   |
|   convexity    | 79.2206  |  79.2206   |
|      bps       |  0.0795  |   0.0795   |
+----------------+----------+------------+


## 三种久期

BondFunctionsduration 函数可以计算三种久期，分别是简单久期（Simple）、麦考利久期（Macaulay）和修正久期（Modified），只需配置久期类型参数即可，默认计算的是修正久期。

durationSimple = ql.BondFunctions.duration(
bond,
bondYield,
accrualDayCounter,
compounding,
frequency,
ql.Duration.Simple)

durationModified = ql.BondFunctions.duration(
bond,
bondYield,
accrualDayCounter,
compounding,
frequency,
ql.Duration.Modified)

durationMacaulay = ql.BondFunctions.duration(
bond,
bondYield,
accrualDayCounter,
compounding,
frequency,
ql.Duration.Macaulay)

tabDuration = pt.PrettyTable(['type', 'value'])

print(tabDuration)

+----------+-------------------+
|   type   |       value       |
+----------+-------------------+
|  Simple  | 8.352745733674992 |
| Modified | 8.077122021802985 |
| Macaulay | 8.352745733674992 |
+----------+-------------------+


## 扩展阅读

《QuantLib 金融计算》系列合集

posted @ 2020-08-05 20:49  xuruilong100  阅读(1865)  评论(1编辑  收藏  举报