使用Python QuantLib创建OIS Swaption时遇类型错误求助
关于QuantLib创建OIS标的Swaption的问题
问题代码
calc_date = ql.Date(23,6,2023) exercise_date = calendar.advance(calc_date, ql.Period('1y')) exercise = ql.EuropeanExercise(exercise_date) settlementType = ql.Settlement.Physical settlementMethod = ql.Settlement.ParYieldCurve underlying_swap = ql.MakeOIS(ql.Period('1y'), sofrIndex, 0.052401, nominal=notional) swaption = ql.Swaption(underlying_swap, 0.03943, settlementType, settlementMethod)
报错信息
TypeError: Wrong number or type of arguments for overloaded function 'new_Swaption'.
Possible C/C++ prototypes are:
Swaption::Swaption(ext::shared_ptr< VanillaSwap > const &,ext::shared_ptr< Exercise > const &,Settlement::Type,Settlement::Method)
Swaption::Swaption(ext::shared_ptr< VanillaSwap > const &,ext::shared_ptr< Exercise > const &,Settlement::Type)
Swaption::Swaption(ext::shared_ptr< VanillaSwap > const &,ext::shared_ptr< Exercise > const &)
错误原因
- 参数顺序完全错误:Swaption构造函数的第二个参数必须是
Exercise对象的智能指针,但你传入了执行价格0.03943,导致参数数量和类型不匹配。 - 标的类型不兼容:
MakeOIS返回的是OIS类型对象,而QuantLib的Swaption构造函数仅接受VanillaSwap的智能指针——OIS继承自Swap而非VanillaSwap,因此无法直接传入。
解决方案
QuantLib支持创建OIS标的的Swaption,但需要调整标的Swap的构造方式并修正Swaption的参数传递:
修正后的代码
calc_date = ql.Date(23,6,2023) ql.Settings.instance().evaluationDate = calc_date # 必须设置估值日期 calendar = ql.UnitedStates() # 根据实际场景调整日历 exercise_date = calendar.advance(calc_date, ql.Period('1y')) exercise = ql.EuropeanExercise(exercise_date) settlementType = ql.Settlement.Physical settlementMethod = ql.Settlement.ParYieldCurve notional = 1000000 # 根据实际需求设置名义本金 sofrIndex = ql.SOFR() # 确保指数已正确初始化 # 构造OIS风格的VanillaSwap,固定利率即为Swaption的行权价 underlying_swap = ql.MakeVanillaSwap( ql.Period('1y'), sofrIndex, 0.03943, # Swaption行权价 nominal=notional, fixedLegTenor=ql.Period('1y'), # OIS固定腿通常按年付息 floatingLegTenor=ql.Period('1D') # OIS浮动腿为隔夜 ) # 将Swap转换为Swaption要求的shared_ptr<VanillaSwap> swap_ptr = ext.shared_ptr(ql.VanillaSwap)(underlying_swap) # 正确构造Swaption swaption = ql.Swaption(swap_ptr, exercise, settlementType, settlementMethod)
关键说明
- 普通VanillaSwap可以直接用于Swaption,是因为其类型完全匹配构造函数要求;而OIS属于独立的Swap子类,因此需要用
MakeVanillaSwap模拟OIS的结构(浮动腿用隔夜指数、固定腿按年付息等),生成符合要求的VanillaSwap对象。 - Swaption的行权价通过标的Swap的固定利率体现,无需单独传入构造函数;构造函数的核心参数顺序为:
VanillaSwap智能指针→Exercise对象→结算类型(可选)→结算方法(可选)。
内容的提问来源于stack exchange,提问作者D. Welch
相关产品推荐
相关产品推荐

