如果未做特別說明,文中的程序都是 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 天,所以是“不完整的”。