如果未做特别说明,文中的程序都是 Python3 代码。

QuantLib 金融计算——基本组件之 Schedule 类

Schedule 类用于构造一个特定的日期列表,例如债券的付息日列表,是 QuantLib 中固定收益类产品分析最常用到的组件。

载入 QuantLib:

import QuantLib as ql

print(ql.__version__)
1.10

Schedule 对象的构造

Schedule 类对象的构造依赖于之前介绍的几个基本组件。

Schedule(effectiveDate ,
terminationDate ,
tenor,
calendar,
convention,
terminationDateConvention,
rule,
endOfMonth,
firstDate = Date (),
nextToLastDate = Date ())

这些变量的类型和解释如下:

  • effectiveDate、terminationDate:日期,日历列表的起始、结束日期,例如债券的起息日和到期日。
  • tenor:Period 对象,相邻两个日期的间隔,例如债券的付息频率(1 年或 6 个月)或利率互换的利息重置频率(3 个月)。
  • calendar:日历表,生成日期所遵循的特定日历表。
  • convention:整数,如何调整非工作日(除最后一个日期外),取值范围是 quantlib-python 的一些预留变量。
  • terminationDateConvention:整数,如果最后一个日期是非工作日,该如何调整,取值范围是 quantlib-python 的一些预留变量。
  • rule:DateGeneration 的成员,生成日期的规则。
  • endOfMonth:如果起始日期在月末,是否要求其他日期也要安排在月末(除最后一个日期外)。
  • firstDate, nextToLastDate(可选):日期,专门为生成方法 rule 提供的起始、结束日期(不常用)。

作为“容器”的 Schedule 对象

Schedule 对象的行为和 Python 中的 list 非常相似,作为一种存储 Date 对象的序列容器存在。因此下面两个函数是可用的:

  • len(sch):返回 Schedule 对象 sch 内日期的个数。
  • [i]:返回第 i 个日期。

作为序列容器,和 list 一样,Schedule 对象也是可迭代的。

假设想要获得 2017 年每月首个工作日的列表:

  • 起始、结束日期分别是 2017-01-01 和 2017-12-01。
  • 时间间隔是一个月。
  • 日历表遵循中国银行间市场的规定
  • 遇到非工作日就递延到下一工作日

例子 1:

def testingSchedule1():
effectiveDate = ql.Date(1, ql.January, 2017)
terminationDate = ql.Date(1, ql.December, 2017)
tenor = ql.Period(1, ql.Months)
calendar = ql.China(ql.China.IB)
convention = ql.Following
terminationDateConvention = ql.Following
rule = ql.DateGeneration.Forward
endOfMonth = False mySched = ql.Schedule(
effectiveDate,
terminationDate,
tenor,
calendar,
convention,
terminationDateConvention,
rule,
endOfMonth) for i in range(len(mySched)):
print(mySched[i]) print('------') for i in mySched:
print(i)
January 3rd, 2017
February 3rd, 2017
March 1st, 2017
April 1st, 2017
May 2nd, 2017
June 1st, 2017
July 3rd, 2017
August 1st, 2017
September 1st, 2017
October 9th, 2017
November 1st, 2017
December 1st, 2017
------
January 3rd, 2017
February 3rd, 2017
March 1st, 2017
April 1st, 2017
May 2nd, 2017
June 1st, 2017
July 3rd, 2017
August 1st, 2017
September 1st, 2017
October 9th, 2017
November 1st, 2017
December 1st, 2017

一些常用的成员函数

  • until(d):从日期列表中截取前半部分,并保证最后一个日期是 d。
  • isRegular(i):判断第 i 个区间是否完整。这个概念需要解释以下:如果一个 Schedule 对象含有 n 个日期,那么这个对象就含有 n-1 个区间。如果第 i 个区间的长度和事先规定的时间间隔一致,那么这个区间就是完整的(Regular)。

例子 2:

def testingSchedule2():
effectiveDate = ql.Date(1, ql.January, 2017)
terminationDate = ql.Date(1, ql.December, 2017)
tenor = ql.Period(1, ql.Months)
calendar = ql.China(ql.China.IB)
convention = ql.Following
terminationDateConvention = ql.Following
rule = ql.DateGeneration.Forward
endOfMonth = False mySched = ql.Schedule(
effectiveDate,
terminationDate,
tenor,
calendar,
convention,
terminationDateConvention,
rule,
endOfMonth) mySched = mySched.until(ql.Date(15, ql.June, 2017)) for i in mySched:
print(i) print('------') for i in range(len(mySched) - 1):
print('{}-th internal is regular? {}'.format(
i + 1, mySched.isRegular(i + 1)))
January 3rd, 2017
February 3rd, 2017
March 1st, 2017
April 1st, 2017
May 2nd, 2017
June 1st, 2017
June 15th, 2017
------
1-th internal is regular? True
2-th internal is regular? True
3-th internal is regular? True
4-th internal is regular? True
5-th internal is regular? True
6-th internal is regular? False

最后一个区间的长度只有 15 天,所以是“不完整的”。

QuantLib 金融计算——基本组件之 Schedule 类的更多相关文章

  1. QuantLib 金融计算——基本组件之 Currency 类

    目录 QuantLib 金融计算--基本组件之 Currency 类 概述 构造函数 成员函数 如果未做特别说明,文中的程序都是 python3 代码. QuantLib 金融计算--基本组件之 Cu ...

  2. QuantLib 金融计算——基本组件之 Date 类

    目录 QuantLib 金融计算--基本组件之 Date 类 Date 对象的构造 一些常用的成员函数 一些常用的静态函数 为估值计算配置日期 如果未做特别说明,文中的程序都是 Python3 代码. ...

  3. QuantLib 金融计算——基本组件之 Calendar 类

    目录 QuantLib 金融计算--基本组件之 Calendar 类 Calendar 对象的构造 一些常用的成员函数 自定义假期列表 工作日修正 如果未做特别说明,文中的程序都是 Python3 代 ...

  4. QuantLib 金融计算——基本组件之 DayCounter 类

    目录 QuantLib 金融计算--基本组件之 DayCounter 类 DayCounter 对象的构造 一些常用的成员函数 如果未做特别说明,文中的程序都是 Python3 代码. QuantLi ...

  5. QuantLib 金融计算——基本组件之 DateGeneration 类

    目录 QuantLib 金融计算--基本组件之 DateGeneration 类 QuantLib 金融计算--基本组件之 DateGeneration 类 许多产品的估值依赖于对未来现金流的分析,因 ...

  6. QuantLib 金融计算——基本组件之 Index 类

    目录 QuantLib 金融计算--基本组件之 Index 类 QuantLib 金融计算--基本组件之 Index 类 Index 类用于表示已知的指数或者收益率,例如 Libor 或 Shibor ...

  7. QuantLib 金融计算——基本组件之 InterestRate 类

    目录 QuantLib 金融计算--基本组件之 InterestRate 类 InterestRate 对象的构造 一些常用的成员函数 如果未做特别说明,文中的程序都是 Python3 代码. Qua ...

  8. QuantLib 金融计算——基本组件之 ExchangeRateManager 类

    目录 QuantLib 金融计算--基本组件之 ExchangeRateManager 类 概述 Money 类中的汇率转换配置 ExchangeRateManager 函数 如果未做特别说明,文中的 ...

  9. QuantLib 金融计算——基本组件之 Money 类

    目录 QuantLib 金融计算--基本组件之 Money 类 概述 构造函数 成员函数 如果未做特别说明,文中的程序都是 python3 代码. QuantLib 金融计算--基本组件之 Money ...

随机推荐

  1. Cookie、Session、Token

    一.发展史 .最初.Web基本上就是文档的浏览而已,既然是浏览,作为服务器,不需要记录谁在某一段时间里都浏览了什么文档,每次请求都是一个新的HTTP协议,就是请求加相应,尤其是我不用记住是谁刚刚发了H ...

  2. 特性(property)

    6.4 特性(property) 1 什么是特性property property是一种特殊的属性,访问它时会执行一段功能(函数)然后返回值 import math class Circle: def ...

  3. JavaScript RegExp.exec() 方法

    定义和用法: exec() 方法用于检索字符串中的正则表达式的匹配. 语法: RegExpObject.exec(string); RegExpObject:必须参数,正则表达式: string:必须 ...

  4. Solr分词搜索结果不准确

    Solr的schema.xml默认配置分词后条件取 OR 例如:大众1.6T  系统会自动分词为  [大众] [1.6T](ps:不同分词器分词效果不同)   会搜索出包含 [大众 OR  1.6T] ...

  5. Oracle GoldenGate 四、数据过滤和数据项匹配

    写在开始前 从两周前我花了大量的业余时间阅读GoldenGate官方文档,并根据文档实践和进一步学习了解GoldenGate,以下便是根据官方文档理解总结的GoldenGate学习内容: Oracle ...

  6. Windows sql语句正则匹配导出数据到本地 The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

    尝试使用 into outfile导出数据的时候出现错误: The MySQL server is running with the --secure-file-priv option so it c ...

  7. ZOJ2748 Free Kick 2017-04-18 20:40 40人阅读 评论(0) 收藏

    Free Kick Time Limit: 2 Seconds      Memory Limit: 65536 KB In a soccer game, a direct free kick is ...

  8. 团体程序设计天梯赛L2-021 点赞狂魔 2017-04-18 11:39 154人阅读 评论(0) 收藏

    L2-021. 点赞狂魔 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 微博上有个"点赞"功能,你可以为你 ...

  9. ZOJ2201 No Brainer 2017-04-16 19:21 54人阅读 评论(0) 收藏

    No Brainer Time Limit: 2 Seconds      Memory Limit: 65536 KB Zombies love to eat brains. Yum. Input ...

  10. 个人整理的一些iOS Entitlements

    收集了不少Entitlement,当然也肯定有遗漏.有的就是key的字面意思,就不多做解释.不过有的虽然字面意思好理解,不过具体的用处不太清楚,就写的Unknown use.在替换entitlemen ...