【发布时间】:2020-07-03 08:41:32
【问题描述】:
我想为固定利率债券定价如下 -
import QuantLib as ql
import pandas as pd
import math
todaysDate = ql.Date(31, 8, 2019)
ql.Settings.instance().evaluationDate = todaysDate
## define available Spot rates
spotDates = [ql.Date(1,10,2019), ql.Date(1,11,2019), ql.Date(1,12,2019)]
spotRates = [0.066682, 0.067199, 0.067502] ### Compounded continuously
dayCount = ql.Actual365Fixed()
calendar = ql.UnitedStates()
interpolation = ql.Linear()
compounding = ql.Compounded
compoundingFrequency = ql.Continuous
spotCurve = ql.YieldTermStructureHandle(ql.ZeroCurve(spotDates, spotRates, dayCount, calendar, interpolation, compounding, compoundingFrequency))
## Define Fixed rate bond with only one coupon payment at maturity
issueDate = todaysDate
maturityDate = ql.Date(1,11,2019)
businessConvention = ql.Following
monthEnd = False
dates = [maturityDate]
rolling_convention = ql.Following
schedule = ql.Schedule(dates, calendar, businessConvention)
dayCount = ql.Actual365Fixed()
couponRate = .05
settlementDays = 0
faceValue = 100
fixedRateBond = ql.FixedRateBond(settlementDays, faceValue, schedule, couponRate, dayCount, businessConvention, issueDate)
bondEngine = ql.DiscountingBondEngine(spotCurve)
fixedRateBond.setPricingEngine(bondEngine)
fixedRateBond.NPV()
print(fixedRateBond.NPV())
上述实施失败;我无法理解的原因。在线fixedRateBond代,我得到以下错误-
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/QuantLib/QuantLib.py", line 15250, in __init__
_QuantLib.FixedRateBond_swiginit(self, _QuantLib.new_FixedRateBond(*args))
TypeError: Wrong number or type of arguments for overloaded function 'new_FixedRateBond'.
Possible C/C++ prototypes are:
FixedRateBond::FixedRateBond(Integer,Real,Schedule const &,std::vector< Rate,std::allocator< Rate > > const &,DayCounter const &,BusinessDayConvention,Real,Date,Calendar const &,Period const &,Calendar const &,BusinessDayConvention,bool)
FixedRateBond::FixedRateBond(Integer,Real,Schedule const &,std::vector< Rate,std::allocator< Rate > > const &,DayCounter const &,BusinessDayConvention,Real,Date,Calendar const &,Period const &,Calendar const &,BusinessDayConvention)
FixedRateBond::FixedRateBond(Integer,Real,Schedule const &,std::vector< Rate,std::allocator< Rate > > const &,DayCounter const &,BusinessDayConvention,Real,Date,Calendar const &,Period const &,Calendar const &)
FixedRateBond::FixedRateBond(Integer,Real,Schedule const &,std::vector< Rate,std::allocator< Rate > > const &,DayCounter const &,BusinessDayConvention,Real,Date,Calendar const &,Period const &)
FixedRateBond::FixedRateBond(Integer,Real,Schedule const &,std::vector< Rate,std::allocator< Rate > > const &,DayCounter const &,BusinessDayConvention,Real,Date,Calendar const &)
FixedRateBond::FixedRateBond(Integer,Real,Schedule const &,std::vector< Rate,std::allocator< Rate > > const &,DayCounter const &,BusinessDayConvention,Real,Date)
FixedRateBond::FixedRateBond(Integer,Real,Schedule const &,std::vector< Rate,std::allocator< Rate > > const &,DayCounter const &,BusinessDayConvention,Real)
FixedRateBond::FixedRateBond(Integer,Real,Schedule const &,std::vector< Rate,std::allocator< Rate > > const &,DayCounter const &,BusinessDayConvention)
FixedRateBond::FixedRateBond(Integer,Real,Schedule const &,std::vector< Rate,std::allocator< Rate > > const &,DayCounter const &)
FixedRateBond::FixedRateBond(Integer,Real,Schedule const &,std::vector< InterestRate,std::allocator< InterestRate > > const &,BusinessDayConvention,Real,Date const &,Calendar const &,Period const &,Calendar const &,BusinessDayConvention,bool)
FixedRateBond::FixedRateBond(Integer,Real,Schedule const &,std::vector< InterestRate,std::allocator< InterestRate > > const &,BusinessDayConvention,Real,Date const &,Calendar const &,Period const &,Calendar const &,BusinessDayConvention)
FixedRateBond::FixedRateBond(Integer,Real,Schedule const &,std::vector< InterestRate,std::allocator< InterestRate > > const &,BusinessDayConvention,Real,Date const &,Calendar const &,Period const &,Calendar const &)
FixedRateBond::FixedRateBond(Integer,Real,Schedule const &,std::vector< InterestRate,std::allocator< InterestRate > > const &,BusinessDayConvention,Real,Date const &,Calendar const &,Period const &)
FixedRateBond::FixedRateBond(Integer,Real,Schedule const &,std::vector< InterestRate,std::allocator< InterestRate > > const &,BusinessDayConvention,Real,Date const &,Calendar const &)
FixedRateBond::FixedRateBond(Integer,Real,Schedule const &,std::vector< InterestRate,std::allocator< InterestRate > > const &,BusinessDayConvention,Real,Date const &)
FixedRateBond::FixedRateBond(Integer,Real,Schedule const &,std::vector< InterestRate,std::allocator< InterestRate > > const &,BusinessDayConvention,Real)
FixedRateBond::FixedRateBond(Integer,Real,Schedule const &,std::vector< InterestRate,std::allocator< InterestRate > > const &,BusinessDayConvention)
FixedRateBond::FixedRateBond(Integer,Real,Schedule const &,std::vector< InterestRate,std::allocator< InterestRate > > const &)
FixedRateBond::FixedRateBond(Integer,Calendar const &,Real,Date const &,Date const &,Period const &,std::vector< Rate,std::allocator< Rate > > const &,DayCounter const &,BusinessDayConvention,BusinessDayConvention,Real,Date const &,Date const &,DateGeneration::Rule,bool,Calendar const &,Period const &,Calendar const &,BusinessDayConvention const,bool)
FixedRateBond::FixedRateBond(Integer,Calendar const &,Real,Date const &,Date const &,Period const &,std::vector< Rate,std::allocator< Rate > > const &,DayCounter const &,BusinessDayConvention,BusinessDayConvention,Real,Date const &,Date const &,DateGeneration::Rule,bool,Calendar const &,Period const &,Calendar const &,BusinessDayConvention const)
FixedRateBond::FixedRateBond(Integer,Calendar const &,Real,Date const &,Date const &,Period const &,std::vector< Rate,std::allocator< Rate > > const &,DayCounter const &,BusinessDayConvention,BusinessDayConvention,Real,Date const &,Date const &,DateGeneration::Rule,bool,Calendar const &,Period const &,Calendar const &)
FixedRateBond::FixedRateBond(Integer,Calendar const &,Real,Date const &,Date const &,Period const &,std::vector< Rate,std::allocator< Rate > > const &,DayCounter const &,BusinessDayConvention,BusinessDayConvention,Real,Date const &,Date const &,DateGeneration::Rule,bool,Calendar const &,Period const &)
FixedRateBond::FixedRateBond(Integer,Calendar const &,Real,Date const &,Date const &,Period const &,std::vector< Rate,std::allocator< Rate > > const &,DayCounter const &,BusinessDayConvention,BusinessDayConvention,Real,Date const &,Date const &,DateGeneration::Rule,bool,Calendar const &)
FixedRateBond::FixedRateBond(Integer,Calendar const &,Real,Date const &,Date const &,Period const &,std::vector< Rate,std::allocator< Rate > > const &,DayCounter const &,BusinessDayConvention,BusinessDayConvention,Real,Date const &,Date const &,DateGeneration::Rule,bool)
FixedRateBond::FixedRateBond(Integer,Calendar const &,Real,Date const &,Date const &,Period const &,std::vector< Rate,std::allocator< Rate > > const &,DayCounter const &,BusinessDayConvention,BusinessDayConvention,Real,Date const &,Date const &,DateGeneration::Rule)
FixedRateBond::FixedRateBond(Integer,Calendar const &,Real,Date const &,Date const &,Period const &,std::vector< Rate,std::allocator< Rate > > const &,DayCounter const &,BusinessDayConvention,BusinessDayConvention,Real,Date const &,Date const &)
FixedRateBond::FixedRateBond(Integer,Calendar const &,Real,Date const &,Date const &,Period const &,std::vector< Rate,std::allocator< Rate > > const &,DayCounter const &,BusinessDayConvention,BusinessDayConvention,Real,Date const &)
FixedRateBond::FixedRateBond(Integer,Calendar const &,Real,Date const &,Date const &,Period const &,std::vector< Rate,std::allocator< Rate > > const &,DayCounter const &,BusinessDayConvention,BusinessDayConvention,Real)
FixedRateBond::FixedRateBond(Integer,Calendar const &,Real,Date const &,Date const &,Period const &,std::vector< Rate,std::allocator< Rate > > const &,DayCounter const &,BusinessDayConvention,BusinessDayConvention)
FixedRateBond::FixedRateBond(Integer,Calendar const &,Real,Date const &,Date const &,Period const &,std::vector< Rate,std::allocator< Rate > > const &,DayCounter const &,BusinessDayConvention)
FixedRateBond::FixedRateBond(Integer,Calendar const &,Real,Date const &,Date const &,Period const &,std::vector< Rate,std::allocator< Rate > > const &,DayCounter const &
我原以为价格应该是-
print(105*math.exp(-0.067199*dayCount.yearFraction(issueDate,maturityDate)))
任何指向正确方法的指针都将受到高度赞赏。
修改在 David Duarte 回复后:
现在,我为债券定价的新代码集如下所示 -
import QuantLib as ql
import pandas as pd
import math
todaysDate = ql.Date(31, 8, 2019)
ql.Settings.instance().evaluationDate = todaysDate
spotDates = [ql.Date(1,10,2019), ql.Date(1,11,2019), ql.Date(1,12,2019)]
spotRates = [0.066682, 0.067199, 0.067502] ### Compounded continuously
dayCount = ql.Actual365Fixed()
calendar = ql.UnitedStates()
interpolation = ql.Linear()
compounding = ql.Compounded
compoundingFrequency = ql.Continuous
spotCurve = ql.YieldTermStructureHandle(ql.ZeroCurve(spotDates, spotRates, dayCount, calendar, interpolation, compounding, compoundingFrequency))
issueDate = todaysDate
maturityDate = ql.Date(1,11,2019)
businessConvention = ql.Following
monthEnd = False
dates = [maturityDate]
rolling_convention = ql.Following
schedule = ql.Schedule(dates, calendar, businessConvention)
# pd.DataFrame({'date': list(schedule)})
dayCount = ql.Actual365Fixed()
couponRate = .05
settlementDays = 0
faceValue = 100
fixedRateBond = ql.FixedRateBond(settlementDays, calendar, faceValue, issueDate, maturityDate, ql.Period(ql.Once), [couponRate],dayCount)
bondEngine = ql.DiscountingBondEngine(spotCurve)
fixedRateBond.setPricingEngine(bondEngine)
fixedRateBond.NPV()
有了这个,我得到错误 -
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/QuantLib/QuantLib.py", line 8767, in NPV
return _QuantLib.Instrument_NPV(self)
RuntimeError: negative time (-0.0767123) given
知道为什么会出现这个错误吗?
进一步修改以纠正负值错误:
基本上我添加了一个rate of 0 来对应today's spot rate 来定义spotCurve
import QuantLib as ql
import pandas as pd
import math
todaysDate = ql.Date(31, 8, 2019)
ql.Settings.instance().evaluationDate = todaysDate
spotDates = [todaysDate, ql.Date(1,10,2019), ql.Date(1,11,2019), ql.Date(1,12,2019)]
spotRates = [0, 0.066682, 0.067199, 0.067502] ### Compounded continuously
dayCount = ql.Actual365Fixed()
calendar = ql.UnitedStates()
interpolation = ql.Linear()
compounding = ql.Compounded
compoundingFrequency = ql.Continuous
spotCurve = ql.YieldTermStructureHandle(ql.ZeroCurve(spotDates, spotRates, dayCount, calendar, interpolation, compounding, compoundingFrequency))
issueDate = todaysDate
maturityDate = ql.Date(1,11,2019)
businessConvention = ql.Following
monthEnd = False
dates = [maturityDate]
rolling_convention = ql.Following
schedule = ql.Schedule(dates, calendar, businessConvention)
# pd.DataFrame({'date': list(schedule)})
dayCount = ql.Actual365Fixed()
couponRate = .05
settlementDays = 0
faceValue = 100
fixedRateBond = ql.FixedRateBond(settlementDays, calendar, faceValue, issueDate, maturityDate, ql.Period(ql.Once), [couponRate],dayCount)
bondEngine = ql.DiscountingBondEngine(spotCurve)
fixedRateBond.setPricingEngine(bondEngine)
fixedRateBond.NPV()
但是我得到的值是99.68276963841888。我是否应该得到 103.808278875 的值,它来自这个计算 - 105*math.exp(-0.067199*dayCount.yearFraction(issueDate,maturityDate))
【问题讨论】:
-
您遇到什么错误?发布完整的回溯
-
@buran ,我用错误消息更新了我的原始帖子。
-
查看 docs for FixedRateBond 它需要 8 个或 5 个参数,而您传递了 7 个。我没有查看您传递的确切参数的详细信息。这正是错误所说的 -
Wrong number or type of arguments