使用Quantlib,通过YTM计算债券

发布时间:2019-03-10 19:53:53编辑:auto阅读(2891)

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

关键字