如果未做特别说明,文中的程序都是 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 ())

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

  • effectiveDateterminationDate:日期,日历列表的起始、结束日期,例如债券的起息日和到期日。
  • tenorPeriod 对象,相邻两个日期的间隔,例如债券的付息频率(1 年或 6 个月)或利率互换的利息重置频率(3 个月)。
  • calendar:日历表,生成日期所遵循的特定日历表。
  • convention:整数,如何调整非工作日(除最后一个日期外),取值范围是 quantlib-python 的一些预留变量。
  • terminationDateConvention:整数,如果最后一个日期是非工作日,该如何调整,取值范围是 quantlib-python 的一些预留变量。
  • ruleDateGeneration 的成员,生成日期的规则。
  • 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 天,所以是“不完整的”。

最新文章

  1. Gradle使用小结
  2. androidSDK无法更新的解决方法之一
  3. C++大数类模板
  4. kettle不能正常自动获取字段
  5. [转]ANDROID仿IOS微信滑动删除_SWIPELISTVIEW左滑删除例子
  6. Asp.Net--主题/皮肤文件
  7. chapter 1 Number/Adventurous Person
  8. 四轴飞行器1.4 姿态解算和Matlab实时姿态显示
  9. Easyui datagrid 批量编辑和提交
  10. [Python学习] 模块三.基本字符串
  11. Django实现简单分页功能
  12. FORM内置系统函数
  13. amd,cmd规范
  14. python---读取/写入excel用例数据
  15. Java 8 新特性-菜鸟教程 (8) -Java 8 日期时间 API
  16. 【Qt】窗口居中显示
  17. 【转载】java对象和byte数组互转,直接拿去用
  18. Java 代码块:静态代码块、构造代码块、构造函数块
  19. CodeForces - 55C Pie or die 想法题(猜程序)
  20. 【JS深入学习】——函数创建和重载

热门文章

  1. 在 macOS 中激活 Astash Professional
  2. jquery相对定位(包含find的使用 find相当于后代选择器)$("选择器1","选择器2")
  3. Java设计模式(7)——装饰者模式
  4. 转 org.aspectj.lang.JoinPoint-中文简要API
  5. input中的disabled、readonly和hidden
  6. ADF文件在哪个地方?
  7. awk基础04-内置函数
  8. Index--复合索引的思考1
  9. DataTable转List<T>
  10. C#用 catch 捕获异类的常用类型