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

QuantLib 金融计算——案例之普通利率互换分析(1)

程序员文章站 2022-09-04 22:19:27
[TOC] QuantLib 金融计算——案例之普通利率互换分析(1) 概述 QuantLib 中涉及利率互换的功能大致分为两大类: 对存续的利率互换合约估值; 根据利率互换合约的成交报价推算隐含的期限结构。 这两类功能是紧密联系的,根据最新报价推算出的期限结构通常可以用来对存续合约进行估值。 本文 ......

quantlib 金融计算——案例之普通利率互换分析(1)

概述

quantlib 中涉及利率互换的功能大致分为两大类:

  • 对存续的利率互换合约估值;
  • 根据利率互换合约的成交报价推算隐含的期限结构。

这两类功能是紧密联系的,根据最新报价推算出的期限结构通常可以用来对存续合约进行估值。

本文接下来介绍如何具体实现对合约的估值,并以 real world tidy interest rate swap pricing 中 bloomberg 的结果作为比较基准。

bloomberg 的结果:

QuantLib 金融计算——案例之普通利率互换分析(1)

合约条款

对存续的利率互换合约进行估值,通常是根据当前的期限结构计算出浮动端(floating leg)和固定端(fixed leg)的“预期贴现现金流”,两者之差即合约的估值。需要注意的是,利率互换的估值对合约条款比较敏感。

示例中的合约是一个 euribor 6m 的利率互换,条款细则如下:

  • 浮动利率:euribor 6m
  • 固定利率:0.059820%
  • 利差:0.0%
  • 生效期:2007-01-19
  • 期限:25 y
  • 类型:支付浮动利率,收取固定利率
  • 浮动端支付频率:半年一次
  • 浮动端天数计算规则:act/360
  • 固定端支付频率:一年一次
  • 固定端天数计算规则:30u/360
  • 日历:target(匹配 trans-european automated real-time gross settlement express transfer system 的日历)
  • 估值日期:2019-04-15

实践

import quantlib as ql
import prettytable as pt

calendar = ql.target()
evaluationdate = ql.date(15, ql.april, 2019)
ql.settings.instance().evaluationdate = evaluationdate

设置期限结构

估值的核心是当前的期限结构,根据 real world tidy interest rate swap pricing 中的贴现因子数据设置估值用的期限结构。

maturity date discount factors
04/15/2019 na
04/23/2019 1.0000735
05/16/2019 1.0003059
07/16/2019 1.0007842
10/16/2019 1.0011807
04/16/2020 1.0023373
10/16/2020 1.0033115
04/16/2021 1.0039976
04/19/2022 1.0039393
04/17/2023 1.0015958
04/16/2024 0.9972325
04/16/2025 0.9907452
04/16/2026 0.9820912
04/16/2027 0.9715859
04/18/2028 0.9591332
04/16/2029 0.9455427
04/16/2030 0.9311096
04/16/2031 0.9161298
04/17/2034 0.8705738
04/18/2039 0.8017461
04/19/2044 0.7464983
04/20/2049 0.7010373
04/16/2054 0.6626670
04/16/2059 0.6289098
04/16/2064 0.5974307
04/16/2069 0.5684840
# discount curve

curvedates = [
    ql.date(15, ql.april, 2019), ql.date(23, ql.april, 2019), ql.date(16, ql.may, 2019), ql.date(16, ql.july, 2019),
    ql.date(16, ql.october, 2019), ql.date(16, ql.april, 2020), ql.date(16, ql.october, 2020), ql.date(16, ql.april, 2021),
    ql.date(19, ql.april, 2022), ql.date(17, ql.april, 2023), ql.date(16, ql.april, 2024), ql.date(16, ql.april, 2025),
    ql.date(16, ql.april, 2026), ql.date(16, ql.april, 2027), ql.date(18, ql.april, 2028), ql.date(16, ql.april, 2029),
    ql.date(16, ql.april, 2030), ql.date(16, ql.april, 2031), ql.date(17, ql.april, 2034), ql.date(18, ql.april, 2039),
    ql.date(19, ql.april, 2044), ql.date(20, ql.april, 2049), ql.date(16, ql.april, 2054), ql.date(16, ql.april, 2059),
    ql.date(16, ql.april, 2064), ql.date(16, ql.april, 2069)]

discountfactors = [
    1.0, 1.0000735, 1.0003059, 1.0007842, 1.0011807, 1.0023373, 1.0033115,
    1.0039976, 1.0039393, 1.0015958, 0.9972325, 0.9907452, 0.9820912, 0.9715859,
    0.9591332, 0.9455427, 0.9311096, 0.9161298, 0.8705738, 0.8017461, 0.7464983,
    0.7010373, 0.6626670, 0.6289098, 0.5974307, 0.5684840]

discountcurve = ql.discountcurve(
    curvedates,
    discountfactors,
    ql.actual360(),  # 与浮动端一致
    calendar)

discountcurvehandle = ql.yieldtermstructurehandle(discountcurve)

添加历史浮动利率

估值利率互换需要用到一个重要的类——iborindex,它负责根据期限结构以及合约的条款推算出隐含的远期利率,进而得到浮动端的预期现金流。

由于是对存续合约估值,需要为期限结构添加“历史浮动利率”——历史上 fixing date 上的 euribor 6m 数据。尽管只有最近一次 fixing 的 euribor 6m 利率会参与估值,但用户还是要添加更早期 fixing date 的利率,否则会报错,幸运的是它们不参与估值,可以用 0 来填充。

euriborindex = ql.euribor6m(discountcurvehandle)

# add fixing dates and rates for floating leg

unusedrate = 0.0  # not used in pricing
rate20190117 = -0.00236  # euribor-6m at 2019-01-17

euriborindex.addfixing(fixingdate=ql.date(17, ql.january, 2007), fixing=unusedrate)
euriborindex.addfixing(fixingdate=ql.date(17, ql.july, 2007), fixing=unusedrate)
euriborindex.addfixing(fixingdate=ql.date(17, ql.january, 2008), fixing=unusedrate)
euriborindex.addfixing(fixingdate=ql.date(17, ql.july, 2008), fixing=unusedrate)
euriborindex.addfixing(fixingdate=ql.date(15, ql.january, 2009), fixing=unusedrate)
euriborindex.addfixing(fixingdate=ql.date(16, ql.july, 2009), fixing=unusedrate)
euriborindex.addfixing(fixingdate=ql.date(15, ql.january, 2010), fixing=unusedrate)
euriborindex.addfixing(fixingdate=ql.date(15, ql.july, 2010), fixing=unusedrate)
euriborindex.addfixing(fixingdate=ql.date(17, ql.january, 2011), fixing=unusedrate)
euriborindex.addfixing(fixingdate=ql.date(15, ql.july, 2011), fixing=unusedrate)
euriborindex.addfixing(fixingdate=ql.date(17, ql.january, 2012), fixing=unusedrate)
euriborindex.addfixing(fixingdate=ql.date(17, ql.july, 2012), fixing=unusedrate)
euriborindex.addfixing(fixingdate=ql.date(17, ql.january, 2013), fixing=unusedrate)
euriborindex.addfixing(fixingdate=ql.date(17, ql.july, 2013), fixing=unusedrate)
euriborindex.addfixing(fixingdate=ql.date(16, ql.january, 2014), fixing=unusedrate)
euriborindex.addfixing(fixingdate=ql.date(17, ql.july, 2014), fixing=unusedrate)
euriborindex.addfixing(fixingdate=ql.date(15, ql.january, 2015), fixing=unusedrate)
euriborindex.addfixing(fixingdate=ql.date(16, ql.july, 2015), fixing=unusedrate)
euriborindex.addfixing(fixingdate=ql.date(15, ql.january, 2016), fixing=unusedrate)
euriborindex.addfixing(fixingdate=ql.date(15, ql.july, 2016), fixing=unusedrate)
euriborindex.addfixing(fixingdate=ql.date(17, ql.january, 2017), fixing=unusedrate)
euriborindex.addfixing(fixingdate=ql.date(17, ql.july, 2017), fixing=unusedrate)
euriborindex.addfixing(fixingdate=ql.date(17, ql.january, 2018), fixing=unusedrate)
euriborindex.addfixing(fixingdate=ql.date(17, ql.july, 2018), fixing=unusedrate)
euriborindex.addfixing(fixingdate=ql.date(17, ql.january, 2019), fixing=rate20190117)

注:euribor6miborindex 的派生类。

设置合约

一些基本设置:

# swap contrast

nominal = 10000000.0
spread = 0.0
swaptype = ql.vanillaswap.receiver
lengthinyears = 25
effectivedate = ql.date(19, ql.january, 2007)
terminationdate = effectivedate + ql.period(lengthinyears, ql.years)

设置固定端与浮动端的支付时间表(schedule),计算出现金流的发生日期:

# fixed leg

fixedlegfrequency = ql.period(ql.annual)
fixedlegconvention = ql.modifiedfollowing
fixedlegdaycounter = ql.thirty360(ql.thirty360.usa)
fixeddategeneration = ql.dategeneration.forward
fixedrate = 0.059820 / 100.0

fixedschedule = ql.schedule(
    effectivedate,
    terminationdate,
    fixedlegfrequency,
    calendar,
    fixedlegconvention,
    fixedlegconvention,
    fixeddategeneration,
    false)

# floating leg

floatinglegfrequency = ql.period(ql.semiannual)
floatinglegconvention = ql.modifiedfollowing
floatinglegdaycounter = ql.actual360()
floatingdategeneration = ql.dategeneration.forward

floatschedule = ql.schedule(
    effectivedate,
    terminationdate,
    floatinglegfrequency,
    calendar,
    floatinglegconvention,
    floatinglegconvention,
    floatingdategeneration,
    false)

vanillaswap 类实现了普通利率互换,vanillaswap 类将接受一个定价引擎——discountingswapengine,并根据前面配置好的现金流日期计算浮动端和固定端的预期贴现现金流。

spot25yearswap = ql.vanillaswap(
    swaptype,
    nominal,
    fixedschedule,
    fixedrate,
    fixedlegdaycounter,
    floatschedule,
    euriborindex,
    spread,
    floatinglegdaycounter)

swapengine = ql.discountingswapengine(discountcurvehandle)
spot25yearswap.setpricingengine(swapengine)

估值

bloomberg 对浮动端和固定端的估值考虑了本金,而 quantlib 默认不考虑本金,所以浮动端和固定端的 npv 要自己计算。

fixednpv = 0.0
floatingnpv = 0.0

fixedtable = pt.prettytable(['date', 'amount'])

for cf in spot25yearswap.fixedleg():
    if cf.date() > evaluationdate:
        fixedtable.add_row([str(cf.date()), cf.amount()])
        fixednpv = fixednpv + discountcurvehandle.discount(cf.date()) * cf.amount()

fixednpv = fixednpv + discountcurvehandle.discount(
    spot25yearswap.fixedleg()[-1].date()) * nominal

floatingtable = pt.prettytable(['date', 'amount'])

for cf in spot25yearswap.floatingleg():
    if cf.date() > evaluationdate:
        floatingtable.add_row([str(cf.date()), cf.amount()])
        floatingnpv = floatingnpv + discountcurvehandle.discount(cf.date()) * cf.amount()

floatingnpv = floatingnpv + discountcurvehandle.discount(
    spot25yearswap.floatingleg()[-1].date()) * nominal

npvtable = pt.prettytable(['npvs', 'amount'])
npvtable.add_row(['total', spot25yearswap.npv()])
npvtable.add_row(['fixed leg npv', fixednpv])
npvtable.add_row(['floating leg npv', floatingnpv])

npvtable.align = 'r'
npvtable.float_format = '.2'
print('npvs:')
print(npvtable)
print()

fixedtable.align = 'r'
fixedtable.float_format = '.4'
print('fixed leg cash flows (no nominal):')
print(fixedtable)
print()

floatingtable.align = 'r'
floatingtable.float_format = '.4'
print('floating leg cash flows (no nominal):')
print(floatingtable)

结果:

npvs:
+------------------+------------+
|             npvs |     amount |
+------------------+------------+
|            total | -877065.26 |
|    fixed leg npv | 9119162.21 |
| floating leg npv | 9996227.47 |
+------------------+------------+

fixed leg cash flows (no nominal):
+--------------------+-----------+
|               date |    amount |
+--------------------+-----------+
| january 20th, 2020 | 5965.3833 |
| january 19th, 2021 | 5965.3833 |
| january 19th, 2022 | 5982.0000 |
| january 19th, 2023 | 5982.0000 |
| january 19th, 2024 | 5982.0000 |
| january 20th, 2025 | 5998.6167 |
| january 19th, 2026 | 5965.3833 |
| january 19th, 2027 | 5982.0000 |
| january 19th, 2028 | 5982.0000 |
| january 19th, 2029 | 5982.0000 |
| january 21st, 2030 | 6015.2333 |
| january 20th, 2031 | 5965.3833 |
| january 19th, 2032 | 5965.3833 |
+--------------------+-----------+

floating leg cash flows (no nominal):
+--------------------+-------------+
|               date |      amount |
+--------------------+-------------+
|    july 19th, 2019 | -11734.4444 |
| january 20th, 2020 |  -9883.8108 |
|    july 20th, 2020 | -10526.4706 |
| january 19th, 2021 |  -8236.3411 |
|    july 19th, 2021 |  -3118.9504 |
| january 19th, 2022 |    290.3520 |
|    july 19th, 2022 |   6002.4970 |
| january 19th, 2023 |  11853.1381 |
|    july 19th, 2023 |  16803.6213 |
| january 19th, 2024 |  22032.9795 |
|    july 19th, 2024 |  27371.4264 |
| january 20th, 2025 |  33134.5740 |
|    july 21st, 2025 |  38526.4090 |
| january 19th, 2026 |  43841.7013 |
|    july 20th, 2026 |  49022.3996 |
| january 19th, 2027 |  54065.4048 |
|    july 19th, 2027 |  58756.3184 |
| january 19th, 2028 |  64707.0763 |
|    july 19th, 2028 |  67946.7395 |
| january 19th, 2029 |  72599.6699 |
|    july 19th, 2029 |  74090.1906 |
| january 21st, 2030 |  78693.2841 |
|    july 19th, 2030 |  77892.3324 |
| january 20th, 2031 |  82544.3792 |
|    july 21st, 2031 |  83194.2799 |
| january 19th, 2032 |  84980.7972 |
+--------------------+-------------+

估值差异可能的来源

与 bloomberg 的结果相比尽管非常接近,但还是存在差异,估值差异的来源可能如下:

  • 在期限结构上插值的技术细节不一致。discountcurve 对贴现因子进行对数线性插值,bloomberg 的技术细节不得而知。
  • 浮动端和固定端的天数计算规则不一致,而期限结构的天数计算规则与浮动端保持一致。天数计算规则的不一致使得同一“日期”对浮动端和固定端来说意味着不同的“时间”,bloomberg 如何处理这种不一致也不得而知。

下一步

  • 分析国内市场上的利率互换。
  • 从利率互换的成交报价中推算期限结构。