Quantlib Python收益率曲线引导问题



我使用Quantlib来计算掉期的价格。在计算掉期价格之前,我创建了用于计算的量子库曲线。

不幸的是,我得到了一个RuntimerError: RuntimeError:第二腿:第一次迭代:在第一个活着的仪器上失败,支柱2023年4月4日,到期2023年4月4日,参考日期2022年9月30日:根没有括号:f[0.600742,1.66461] ->[1.241044 e-02, 1.241044 e-02] .

在github/stackoverflow上,我看到许多其他人遇到了同样的问题,其中需要除以100的速率或quantlib日期没有正确设置。看来我又遇到一个我解决不了的问题了。

同样,当我只使用年费率(而不是月费率)时,代码运行良好并给我一个答案。如有任何帮助,不胜感激。

我使用下面的代码:

from QuantLib import *
import pandas as pd
today = Date(30, September, 2022)
Settings.instance().evaluationDate = today
def create_ql_curve():
estr_short = pd.DataFrame({'Tenor': [6], 'Rate': [1.7466]})
estr_long = pd.DataFrame({'Tenor': [1,2,5,10,15,20], 'Rate': [2.2828, 2.547, 2.661, 2.804, 2.85, 2.35]})
euribor_short = pd.DataFrame({'Tenor': [6], 'Rate': [1.7466]})
euribor_long = pd.DataFrame({'Tenor': [1,2,5,10,15,20], 'Rate': [2.2828, 2.547, 2.661, 2.804, 2.85, 2.35]})
ois_helpers = [OISRateHelper(0,
Period(int(tenor), Months),
QuoteHandle(SimpleQuote(rate / 100)),
Eonia(),
YieldTermStructureHandle(),
True)
for rate, tenor in zip(estr_short['Rate'], estr_short['Tenor'])]
ois_helpers += [OISRateHelper(0,
Period(int(tenor), Years),
QuoteHandle(SimpleQuote(rate / 100)),
Eonia(),
YieldTermStructureHandle(),
True)  # telescopicValueDates. When set to True bootstrapping is a lot faster.
for rate, tenor in zip(estr_long['Rate'], estr_long['Tenor'])]
discount_curve = PiecewiseLogCubicDiscount(0, TARGET(), ois_helpers, Actual365Fixed())
discount_curve.enableExtrapolation()
helpers = [SwapRateHelper(QuoteHandle(SimpleQuote(rate / 100)),
Period(int(tenor), Months),
TARGET(),
Annual,
Unadjusted,
Thirty360(),
Euribor6M(),
QuoteHandle(),
Period(0, Days),
YieldTermStructureHandle(discount_curve))
for rate, tenor in zip(euribor_short['Rate'], euribor_short['Tenor'])]
helpers += [SwapRateHelper(QuoteHandle(SimpleQuote(rate / 100)),
Period(int(tenor), Years),
TARGET(),
Annual,
Unadjusted,
Thirty360(),
Euribor6M(),
QuoteHandle(),
Period(0, Days),
YieldTermStructureHandle(discount_curve))
for rate, tenor in zip(euribor_long['Rate'], euribor_long['Tenor'])]
euribor_curve = PiecewiseLogCubicDiscount(0, TARGET(), helpers, Actual365Fixed())
euribor_curve.enableExtrapolation()
return discount_curve, euribor_curve
discount_curve, euribor_curve = create_ql_curve()
forecast_handle = RelinkableYieldTermStructureHandle(euribor_curve)
discount_handle = RelinkableYieldTermStructureHandle(discount_curve)
index_6M = Euribor6M(forecast_handle)
swap_engine = DiscountingSwapEngine(discount_handle)
#  swaps
start_date = Date(23, April, 2014)
maturity_date = Date(23, April, 2023)
fixed_schedule = Schedule(start_date, maturity_date,
Period(1, Years), TARGET(), Unadjusted, Unadjusted,
DateGeneration.Forward, False)
floating_schedule = Schedule(start_date, maturity_date,
Period(6, Months), TARGET(), ModifiedFollowing, ModifiedFollowing,
DateGeneration.Forward, True)
notional = 115000000
fixed_rate = 0.01727
fixed_leg_daycount = Actual360()
float_spread = 0
float_leg_daycount = Actual360()
calendar = TARGET()
previous = calendar.advance(today, -27, Weeks)
dates = [calendar.advance(previous, n, Days) for n in range(150)]
rates = [0.005] * 150
for date, rate in zip(dates, rates):
index_6M.addFixing(date, rate)

swap = VanillaSwap(VanillaSwap.Receiver, notional,
fixed_schedule, fixed_rate, Thirty360(),
floating_schedule, index_6M, 0.0, Actual360())
swap.setPricingEngine(swap_engine)
price = swap.NPV()

发现它:我试图使用SwapRateHelper每月费率不起作用。当使用月费率制作曲线时,您应该使用以下内容:

helpers = [DepositRateHelper(QuoteHandle(SimpleQuote(rate / 100)),
Period(6, Months), 3,
TARGET(), Following, False, Actual360())]

代替:

helpers = [SwapRateHelper(QuoteHandle(SimpleQuote(rate / 100)),
Period(int(tenor), Months),
TARGET(),
Annual,
Unadjusted,
Thirty360(),
Euribor6M(),
QuoteHandle(),
Period(0, Days),
YieldTermStructureHandle(discount_curve))
for rate, tenor in zip(euribor_short['Rate'], euribor_short['Tenor'])]

最新更新