返回顶部
首页 > 资讯 > 后端开发 > Python >使用Quantlib,通过YTM计算债券
  • 750
分享到

使用Quantlib,通过YTM计算债券

债券QuantlibYTM 2023-01-30 22:01:54 750人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

债券标的为170005,我的python代码如下: 1 import QuantLib as ql 2 3 faceAmount = 100.0 4 redemption = 100.0 5 issueDate = ql.D

债券标的为170005,我的python代码如下:

 1 import QuantLib as ql
 2 
 3 faceAmount = 100.0
 4 redemption = 100.0
 5 issueDate = ql.Date(20, 2, 2017)
 6 maturity = ql.Date(20, 2, 2047)
 7 couponRate = 0.0377
 8 coupons = [couponRate]
 9 ytm = 0.04245
10 calendar = ql.China(ql.China.IB)
11 frequency = ql.Semiannual
12 compounce = ql.Compounded
13 dayCounter = ql.ActualActual(ql.ActualActual.ISMA)
14 
15 accuracy=1.0e-8
16 maxNum = 500
17 today = calendar.adjust(ql.Date(14, 9, 2018))
18 ql.Settings.evaluationDate = today
19 settlementDays = 0
20 settlementDate = calendar.advance(
21         today,
22         ql.Period(settlementDays, ql.Days))
23 
24 discountingTermStructure = ql.RelinkableYieldTermStructureHandle()
25 flatTermStructure = ql.FlatForward(settlementDate,
26                                    ytm,
27                                    dayCounter,
28                                    compounce,
29                                    frequency)
30 
31 discountingTermStructure.linkTo(flatTermStructure)
32 bondEngin = ql.DiscountingBondEngine(discountingTermStructure)
33 
34 schedule = ql.Schedule(issueDate,
35                           maturity,
36                           ql.Period(frequency),
37                           ql.China(ql.China.IB),
38                           ql.Following,
39                           ql.Following,
40                           ql.DateGeneration.Backward,
41                           False)
42 fixedRateBond = ql.FixedRateBond(settlementDays,
43                        faceAmount,
44                        schedule,
45                        coupons,
46                        dayCounter,
47                        ql.Following,
48                        redemption,
49                        issueDate)
50 fixedRateBond.setPricingEngine(bondEngin)
51 
52 print(fixedRateBond.cleanPrice())
53 print(fixedRateBond.cleanPrice(0.04245,dayCounter,compounce,frequency,ql.Date(14,9,2018)))
54 print(fixedRateBond.dirtyPrice(0.04245,dayCounter,compounce,frequency))
55 print(fixedRateBond.bondYield(95,dayCounter,compounce,frequency,ql.Date(14,9,2018),accuracy,maxNum))
56 print(flatTermStructure.zeroRate(ql.Date(14,9,2018),dayCounter,compounce, frequency).rate())

输出结果为: 

92.25734945596061
92.19752850225078
92.45364263268556
0.04068211793899536
0.0424500000008563

1、两种pricevalue的计算结果不一样,是我理解错了嘛?

2、NPV和cleanprice的区别是啥?

--结束END--

本文标题: 使用Quantlib,通过YTM计算债券

本文链接: https://lsjlt.com/news/179573.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作