Quantlib - 发出一个奇怪的错误

时间:2018-01-24 10:44:18

标签: python yield quantlib

我正在使用Quantlib来引导曲线,然后获得折扣率。

一般来说,通常的步骤都可以。

但是,对于下面给出的数据,它会引发一个奇怪的错误。

代码:

def create_ois_swaps(self, ois_swap_rates, helpers=None):
    ''' Creates a OIS rate helper from incoming OIS rates
        Input:
        ois_swap_rates: list of tuples comprising of (start_date, end_date, rate, label)
    '''
    if self.helpers is None:
        self.helpers = [
            DatedOISRateHelper(start_date, end_date, QuoteHandle(SimpleQuote(rate / 100)), self.ff_local)
            for start_date, end_date, rate in [tuple((fixed_bond.pydate_to_qldate(sd),
                                                      fixed_bond.pydate_to_qldate(ed),
                                                      rate)) for sd, ed, rate, label
                                               in ois_swap_rates if label not in ['ONTN', 'TN']]]
    else:
        self.helpers += [DatedOISRateHelper(start_date,
                                            end_date,
                                            QuoteHandle(SimpleQuote(rate / 100)), self.ff_local)
                         for start_date, end_date, rate in [tuple((fixed_bond.pydate_to_qldate(sd),
                                                                   fixed_bond.pydate_to_qldate(ed),
                                                                   rate)) for sd, ed, rate, label
                                                            in ois_swap_rates if label not in ['ONTN', 'TN']]]
    # for start_date, end_date, rate in ois_swap_rates]
    self.ois_curve_c = PiecewiseLogCubicDiscount(0, self.calendar, self.helpers, Actual365Fixed())
    self.ois_curve_c.enableExtrapolation()

def bootstrap_usd_ois_3M_curve(self,
                               usd_3M_swap_rates,
                               discountCurve,
                               bootStrapMethod=BootStrapMethod.PiecewiseLogCubicDiscount):

    discount_curve = RelinkableYieldTermStructureHandle()
    discount_curve.linkTo(discountCurve)
    self.helpers += [SwapRateHelper(QuoteHandle(SimpleQuote(rate / 100)),
                                    Period(int(label[:-1]), Years),
                                    TARGET(),
                                    Semiannual,
                                    Unadjusted,
                                    Thirty360(Thirty360.BondBasis),
                                    Euribor3M(),
                                    QuoteHandle(),
                                    Period(0, Days),
                                    discount_curve)
                     for sd, ed, rate, label in usd_3M_swap_rates if label not in ['ONTN', 'TN']]
    # for rate, tenor in usd_3M_swap_rates]

    if bootStrapMethod == BootStrapMethod.PiecewiseLogCubicDiscount:
        self.usd_3M_c = PiecewiseLogCubicDiscount(0, TARGET(), self.helpers, Actual365Fixed())
    elif bootStrapMethod == BootStrapMethod.PiecewiseFlatForward:
        self.usd_3M_c = PiecewiseFlatForward(0, TARGET(), self.helpers, Actual365Fixed())

    # Also, we enable extrapolation beyond the maturity of the last helper; that is mostly
    # for convenience as we retrieve rates to plot the curve near its far end.
    self.usd_3M_c.enableExtrapolation()

在我的主代码中,我将上述两个函数称为: -

创建OIS曲线

usd_ois.create_ois_swaps(ois_rate_ql)

引导曲线

usd_ois.bootstrap_usd_ois_3M_curve(usd_3M_swap_rates=libor_rate_ql, discountCurve=usd_ois.ois_curve_c, bootStrapMethod=BootStrapMethod.PiecewiseFlatForward)

日期:

  

曲线估值日期:2017.01.02

for discounting_Rate: -

  

start_date:2017.01.02   end_date:2018.01.01   dayCount:ACT / 360

错误讯息:

  

return _QuantLib.YieldTermStructure_forwardRate(self,* args)   RuntimeError:负时间(-0.00273973)给出`

曲线对象

  

curve object   使用的数据:   libor and OIS rates used for bootstrapping

曲线对象的状态

state of curve object

注意我有一个 1)折扣OIS曲线和a 2)前向3M曲线

估价日期为2017年1月2日

我正在打电话的折扣曲线如下: -    ois_curve.ois_curve_c.forwardRate(pydate_to_qldate(start_date), pydate_to_qldate(end_date), daycount, Simple).rate() * 100

其中start_Date = 2017年1月2日    end_date = 2018年1月2日

我在一系列日期中运行相同的代码。大多数日期 - 它是成功的,但很少有日期 - 它奇怪地抛出了这个错误

1 个答案:

答案 0 :(得分:1)

作为参考,我在此总结了上述评论。评估日期(2017年1月2日)是曲线使用的日历的假日;因此,曲线将其参考日期移至下一个工作日。此时,就曲线而言,1月2日是过去的,并且在该日期要求汇率会导致错误。

我同意,至少应该使错误更具可读性。我不确定将评估日期设置为假日应该会使曲线无效。抛出错误可能不可行;我们可能使用不同的日历有不同的曲线,只要我们只使用有效的曲线,将评估日期设置为其中一个的假期应该没问题。

相关问题