You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python QuantLib可转债定价调用ConvertibleFixedCouponBond报TypeError

QuantLib可转债定价调用接口报TypeError问题排查

问题现象

在Python环境中使用QuantLib库进行可转债定价时,调用ql.ConvertibleFixedCouponBond接口(底层执行new_ConvertibleFixedCouponBond逻辑)时抛出TypeError异常,怀疑是Python版QuantLib未完成全量接口更新,例如接口已将CallabilityPrice替换为BondPrice,但其余相关函数逻辑未做同步适配。

复现代码

import QuantLib as ql

calculation_date = ql.Date(9,1,2004)
ql.Settings.instance().evaluationDate = calculation_date

redemption = 100.00
face_amount = 100.0
spot_price = 29.04
conversion_price = 26.0
conversion_ratio = 3.84615  # BBG quotes 38.4615; had to scale by a factor of 10

issue_date = ql.Date(15,3,2002)        
maturity_date = ql.Date(15,3,2022)

settlement_days = 2
calendar = ql.UnitedStates(ql.UnitedStates.GovernmentBond)
coupon = 0.0575
frequency = ql.Semiannual
tenor = ql.Period(frequency)

day_count = ql.Thirty360()
accrual_convention = ql.Unadjusted
payment_convention = ql.Unadjusted

call_dates = [ql.Date(20,3,2007)]
call_price = 100.0
put_dates = [ql.Date(20,3,2007), ql.Date(15,3,2012), ql.Date(15,3,2017)]
put_price = 100.0

# assumptions
dividend_yield = 0.02
credit_spread_rate = 0.03  
risk_free_rate = 0.04
volatility = 0.40

callability_schedule = ql.CallabilitySchedule()


for call_date in call_dates:
   callability_price  = ql.BondPrice(call_price, 
                                            ql.BondPrice.Clean)
   callability_schedule.append(ql.Callability(callability_price, 
                                       ql.Callability.Call,
                                       call_date)
                        )
    
for put_date in put_dates:
    puttability_price = ql.BondPrice(put_price, 
                                            ql.BondPrice.Clean)
    callability_schedule.append(ql.Callability(puttability_price,
                                               ql.Callability.Put,
                                               put_date))

dividend_schedule = ql.DividendSchedule() # No dividends
dividend_amount = dividend_yield*spot_price
next_dividend_date = ql.Date(1,12,2004)
dividend_amount = spot_price*dividend_yield
for i in range(4):
    date = calendar.advance(next_dividend_date, 1, ql.Years)
    dividend_schedule.append(
        ql.FixedDividend(dividend_amount, date)
    )

schedule = ql.Schedule(issue_date, maturity_date, tenor,
                       calendar, accrual_convention, accrual_convention,
                       ql.DateGeneration.Backward, False)

credit_spread_handle = ql.QuoteHandle(ql.SimpleQuote(credit_spread_rate))
exercise = ql.AmericanExercise(calculation_date, maturity_date)

convertible_bond = ql.ConvertibleFixedCouponBond(exercise,
                                                 conversion_ratio,
                                                 dividend_schedule,
                                                 callability_schedule, 
                                                 credit_spread_handle,
                                                 issue_date,
                                                 settlement_days,
                                                 [coupon],
                                                 day_count,
                                                 schedule,
                                                 redemption)

报错信息

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-b1df79b0869b> in <module>
     69 exercise = ql.AmericanExercise(calculation_date, maturity_date)
     70 
---> 71 convertible_bond = ql.ConvertibleFixedCouponBond(exercise,
     72                                                  conversion_ratio,
     73                                                  dividend_schedule,

~\anaconda3\lib\site-packages\QuantLib\QuantLib.py in __init__(self, *args)
  18679 
  18680     def __init__(self, *args):
> 18681         _QuantLib.ConvertibleFixedCouponBond_swiginit(self, _QuantLib.new_ConvertibleFixedCouponBond(*args))
  18682     __swig_destroy__ = _QuantLib.delete_ConvertibleFixedCouponBond
  18683 

TypeError: Wrong number or type of arguments for overloaded function 'new_ConvertibleFixedCouponBond'.
  Possible C/C++ prototypes are:
    ConvertibleFixedCouponBond::ConvertibleFixedCouponBond(ext::shared_ptr< Exercise > const &,Real,std::vector< ext::shared_ptr< Callability >,std::allocator< ext::shared_ptr< Callability > > > const &,Date const &,Integer,std::vector< Rate,std::allocator< Rate > > const &,DayCounter const &,Schedule const &,Real,Period const &,Calendar const &,BusinessDayConvention const,bool)
    ConvertibleFixedCouponBond::ConvertibleFixedCouponBond(ext::shared_ptr< Exercise > const &,Real,std::vector< ext::shared_ptr< Callability >,std::allocator< ext::shared_ptr< Callability > > > const &,Date const &,Integer,std::vector< Rate,std::allocator< Rate > > const &,DayCounter const &,Schedule const &,Real,Period const &,Calendar const &,BusinessDayConvention const)
    ConvertibleFixedCouponBond::ConvertibleFixedCouponBond(ext::shared_ptr< Exercise > const &,Real,std::vector< ext::shared_ptr< Callability >,std::allocator< ext::shared_ptr< Callability > > > const &,Date const &,Integer,std::vector< Rate,std::allocator< Rate > > const &,DayCounter const &,Schedule const &,Real,Period const &,Calendar const &)
    ConvertibleFixedCouponBond::ConvertibleFixedCouponBond(ext::shared_ptr< Exercise > const &,Real,std::vector< ext::shared_ptr< Callability >,std::allocator< ext::shared_ptr< Callability > > > const &,Date const &,Integer,std::vector< Rate,std::allocator< Rate > > const &,DayCounter const &,Schedule const &,Real,Period const &)
    ConvertibleFixedCouponBond::ConvertibleFixedCouponBond(ext::shared_ptr< Exercise > const &,Real,std::vector< ext::shared_ptr< Callability >,std::allocator< ext::shared_ptr< Callability > > > const &,Date const &,Integer,std::vector< Rate,std::allocator< Rate > > const &,DayCounter const &,Schedule const &,Real)
    ConvertibleFixedCouponBond::ConvertibleFixedCouponBond(ext::shared_ptr< Exercise > const &,Real,std::vector< ext::shared_ptr< Callability >,std::allocator< ext::shared_ptr< Callability > > > const &,Date const &,Integer,std::vector< Rate,std::allocator< Rate > > const &,DayCounter const &,Schedule const &)

问题原因与修复方案

这个报错和CallabilityPrice/BondPrice的接口适配无关,赎回回售日程的构建逻辑是正确的,核心问题是传入构造函数的参数列表、参数顺序和当前安装版本的QuantLib接口要求完全不匹配:

  • 从报错输出的C++函数原型可以看到,当前版本的ConvertibleFixedCouponBond构造函数根本不接收dividend_schedule(股息日程)、credit_spread_handle(信用利差句柄)这两个入参,这两个参数是更高版本QuantLib才加入该构造函数的,当前版本中这两类参数需要传入定价引擎,而非可转债对象本身。
  • 错误地将dividend_schedule放在了第三个传参位置,挤占了本应传入callability_schedule(赎回回售日程)的位置,后续所有参数顺序全部错位,还多传了两个接口不识别的参数,自然触发参数类型/数量不匹配的报错。

修复方式

调整构造可转债对象的传参,删掉不属于当前版本接口的入参,按原型要求的顺序传参即可,修正后的构造代码如下:

convertible_bond = ql.ConvertibleFixedCouponBond(
    exercise,
    conversion_ratio,
    callability_schedule,  # 第三个参数直接传赎回回售日程,不要插入股息日程
    issue_date,
    settlement_days,
    [coupon],
    day_count,
    schedule,
    redemption
    # 后续的付息周期、日历、计息惯例等参数使用默认值即可,不需要额外传入
)

之前定义的股息率、信用利差、无风险利率、波动率参数,在后续为可转债绑定二叉树/有限差分定价引擎时,传入对应引擎的构造函数即可,不需要在初始化可转债对象时传入。


内容的提问来源于stack exchange,提问作者Ji Seob Lim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.27 11:24:41