欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用Quantlib,通过YTM计算债券净值

程序员文章站 2022-06-28 10:32:55
债券标的为170005,我的python代码如下: 输出结果为: 92.2573494559606192.1975285022507892.453642632685560.040682117938995360.0424500000008563 1、两种pricevalue的计算结果不一样,是我理解错 ......

债券标的为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的区别是啥?