基础
以下是设置QuantLib评估日期的命令。一切从"评估日期"开始,这意味着您想要对金融工具进行估值的日期。假设您想在2020年9月16日对"互换"进行估值,您需要先在QuantLib中设置evaluationDate。底层的C++量化库是通过SWIG打包的,而Python更像是调用这个C++库的API接口。
设置
#import the Quant Lib
import QuantLib as ql
# Let the today date whenwe want to value a instrument be
today = ql.Date(15,6,2020)
# we can set evaluationDate in QL as
ql.Settings.instance().evaluationDate = today
print(ql.Settings.instance().evaluationDate);
# prints..June 15th, 2020
# or you can do
today = ql.Date(15,12,2021);
ql.Settings.instance().setEvaluationDate(today)
print(ql.Settings.instance().evaluationDate)
# prints..December 15th, 2021
移动参考曲线的日期: 以下返回基于FlatForward的期限结构
settlementDays = 2
# Holiday calendar of united states
calendar = ql.UnitedStates()
forwardRate = 0.05
"""Day Counter provides methods for determining the length of a time period according to given market convention,
both as a number of days and as a year fraction."""
dayCounter = ql.Actual360()
# Construct flat forward rate term structure
flatForwardTermStructure = ql.FlatForward(settlementDays, calendar, forwardRate, dayCounter)
flatForwardTermStructure.referenceDate()
print("Max Date: ", flatForwardTermStructure.maxDate())
更改计算评估日期: 以下展示了评估或估值日期的使用。让我们构建一个时间表,用于创建一条现金流支腿,然后我们将计算该支腿上的利率。
today = ql.Date(15,6,2020)
ql.Settings.instance().evaluationDate = today
effectiveDate = ql.Date(15, 6, 2020)
terminationDate = ql.Date(15, 6, 2022)
创建时间表
schedule = ql.MakeSchedule(effectiveDate, terminationDate, ql.Period('6M'))
使用辅助类创建一个固定利率腿,构建一系列固定利率息票
notional = [100.0]
rate = [0.05]
leg = ql.FixedRateLeg(schedule, dayCounter, notional, rate)
利率类封装了利率复利代数运算。它管理日计数惯例、复利惯例、不同惯例之间的转换、贴现/复利因子计算,以及隐含/等价利率计算。
dayCounter = ql.Thirty360()
rate = 0.03
"""
ql/Compounding.hpp
//! Interest rate compounding rule
enum Compounding { Simple = 0, //!< \f$ 1+rt \f$
Compounded = 1, //!< \f$ (1+r)^t \f$
Continuous = 2, //!< \f$ e^{rt} \f$
SimpleThenCompounded, //!< Simple up to the first period then Compounded
CompoundedThenSimple //!< Compounded up to the first period then Simple
};
"""
compoundingType = ql.Compounded
"""
ql/time/frequency.hpp
enum Frequency { NoFrequency = -1, //!< null frequency
Once = 0, //!< only once, e.g., a zero-coupon
Annual = 1, //!< once a year
Semiannual = 2, //!< twice a year
EveryFourthMonth = 3, //!< every fourth month
Quarterly = 4, //!< every third month
Bimonthly = 6, //!< every second month
Monthly = 12, //!< once a month
EveryFourthWeek = 13, //!< every fourth week
Biweekly = 26, //!< every second week
Weekly = 52, //!< once a week
Daily = 365, //!< once a day
OtherFrequency = 999 //!< some other unknown frequency
};
"""
frequency = ql.Annual
interestRate = ql.InterestRate(rate, dayCounter, compoundingType, frequency)
4.958531764309427
ql/cashflows/Cashflows.hpp 净现值(NPV)是现金流的加总,其中每笔现金流都根据给定的固定利率进行贴现。结果会受到利率复利方式、相对频率以及天数计算器选择的影响。
ql.Settings.instance().evaluationDate = ql.Date(15,12,2020)
print( ql.CashFlows.npv(leg, rate, False) )
2.4906934531375144
数组
创建一个空数组
- ql.Array()
创建数组并用值填充
- ql.Array(size, value)
创建数组并根据a0=value,ai=ai−1+increment的规则填充
- ql.Array(size, value, increment)
矩阵
创建一个空矩阵
- ql.Matrix()
创建一个具有给定维度的矩阵
- ql.Matrix(rows, columns)
创建矩阵并用值填充
- ql.Matrix(rows, columns, value)
ql.Matrix()
ql.Matrix(2,2)
ql.Matrix(2,2,0.5)
A = ql.Matrix(3,3)
A[0][0]=0.2
A[0][1]=8.4
A[0][2]=1.5
A[1][0]=0.6
A[1][1]=1.4
A[1][2]=7.3
A[2][0]=0.8
A[2][1]=4.4
A[2][2]=3.2
可观察对象
import QuantLib as ql
flag = None
def raiseFlag():
global flag
flag = 1
me = ql.SimpleQuote(0.0)
obs = ql.Observer(raiseFlag)
obs.registerWith(me)
me.setValue(3.14)
if not flag:
print("Case 1: Observer was not notified of market element change")
flag = None
obs.unregisterWith(me)
me.setValue(3.14)
if not flag:
print("Case 2: Observer was not notified of market element change")
Quotes
SimpleQuote
- ql.SimpleQuote(value)
s = ql.SimpleQuote(0.01)
功能
数值
设置值
是否有效
s.value()
s.setValue(0.05)
s.isValid()
DerivedQuote
- ql.DerivedQuote(quoteHandle, function)
d1 = ql.SimpleQuote(0.06)
d2 = ql.DerivedQuote(ql.QuoteHandle(d1),lambda x: 10*x)
CompositeQuote
- ql.CompositeQuote(quoteHandle, quoteHandle, function)
c1 = ql.SimpleQuote(0.02)
c2 = ql.SimpleQuote(0.03)
def f(x,y):
return x+y
c3 = ql.CompositeQuote(ql.QuoteHandle(c1),ql.QuoteHandle(c2), f)
c3.value()
c4 = ql.CompositeQuote(ql.QuoteHandle(c1),ql.QuoteHandle(c2), lambda x,y:x+y)
c4.value()
DeltaVolQuote
一个用于外汇式报价的类,其中delta-期限对以隐含波动率报价
- ql.DeltaVolQuote(delta, volQuoteHandle, maturity, deltaType)
- ql.DeltaVolQuote(volQuoteHandle, deltaType, maturity, atmType)
deltaType = ql.DeltaVolQuote.Fwd # Also supports: Spot, PaSpot, PaFwd
atmType = ql.DeltaVolQuote.AtmFwd # Also supports: AtmSpot, AtmDeltaNeutral, AtmVegaMax, AtmGammaMax, AtmPutCall50
maturity = 1.0
volAtm, vol25DeltaCall, vol25DeltaPut = 0.08, 0.075, 0.095
atmDeltaQuote = ql.DeltaVolQuote(ql.QuoteHandle(ql.SimpleQuote(volAtm)), deltaType, maturity, atmType)
vol25DeltaPutQuote = ql.DeltaVolQuote(-0.25, ql.QuoteHandle(ql.SimpleQuote(vol25DeltaPut)), maturity, deltaType)
vol25DeltaCallQuote = ql.DeltaVolQuote(0.25, ql.QuoteHandle(ql.SimpleQuote(vol25DeltaCall)), maturity, deltaType)