Skip to content
All library documents

Avoiding Negative-Time Errors in QuantLib Zero Curves

Article Quant Q&A · Author: froyo_lyn

Summary

This note explains a negative-time pricing error when using a QuantLib zero curve to value a credit default swap. The cause is the curve’s first date: the example builds the curve from rates beginning one month after the evaluation date, so discounting or pricing requires a date earlier than the curve’s defined start. QuantLib treats that date as negative time relative to the curve and raises an error.

The proposed fix is to include the evaluation date, or the desired settlement date, as the first curve date and supply a corresponding rate. If no short rate is available, the example’s reply suggests using the one-month rate as a proxy. The issue is specifically about curve date coverage, not simply the number of settlement days. The appropriate base date and short-end rate depend on whether the curve is intended to discount to evaluation or settlement and on the instrument’s date conventions.

Key ideas

  • A zero curve starts at its earliest supplied date, not automatically at the evaluation date.
  • Pricing a cash flow before that first date can produce a negative-time error.
  • Include the evaluation or settlement date as the first curve date and provide a rate for it.
  • A nearby short maturity rate may serve as a proxy when no rate is available at the curve start.

Tags

Full text
# QuantLib CDS pricing error: negative time given


# QuantLib CDS pricing error: negative time given












I am new to QuantLib, and I am using it to price CDS. Following is my python code:

```
from QuantLib import *

calendar = TARGET()

# Set evaluation date
todaysDate = Date(4, 2, 2019)
todaysDate = calendar.adjust(todaysDate)  # business day adjustment
Settings.instance().evaluationDate = todaysDate

# build zero curve
spotRates = [0.02514, 0.026285, 0.027326299999999998,
             0.0279, 0.029616299999999998, 0.026526,
             0.026028, 0.0258695, 0.025958000000000002,
             0.0261375, 0.026355, 0.0266255,
             0.026898, 0.0271745, 0.02741,
             0.027666, 0.028107000000000004, 0.028412000000000003,
             0.028447, 0.0284165]

spotPeriod = [Period(1, Months), Period(2, Months),
              Period(3, Months), Period(6, Months),
              Period(1, Years), Period(2, Years),
              Period(3, Years), Period(4, Years),
              Period(5, Years), Period(6, Years),
              Period(7, Years), Period(8, Years),
              Period(9, Years), Period(10, Years),
              Period(11, Years), Period(12, Years),
              Period(15, Years), Period(20, Years),
              Period(25, Years), Period(30, Years)]
spotDates = [todaysDate + x for x in spotPeriod]

risk_free_rate = YieldTermStructureHandle(
    ZeroCurve(spotDates, spotRates, Actual360())
)

# CDS parameters
recovery_rate = 0.4
hazard_rate = 0.02
maturity = Date(20, 12, 2023)

hazard_curve = FlatHazardRate(todaysDate, QuoteHandle(SimpleQuote(hazard_rate)), Actual360())

# reprice instruments
nominal = 1000000.0
probability = DefaultProbabilityTermStructureHandle(hazard_curve)

schedule = Schedule(todaysDate, maturity, Period(Quarterly),
                    calendar, Following, Following,
                    DateGeneration.TwentiethIMM, False)
cds = CreditDefaultSwap(Protection.Seller, nominal, quoted_spread,
                        schedule, Following, Actual360())
engine = MidPointCdsEngine(probability, recovery_rate, risk_free_rate)
cds.setPricingEngine(engine)

print("   NPV: %g" % cds.NPV())
```

I got following error from the last line of code:

```
RuntimeError: negative time (-0.0166667) given
```

I guess I need to do some date adjustments when I construct the yield curve. Because if I try to use FlatForward yield curve, there is no error.

But I am confused about the ideas of evaluation date and settlements date, as well as the spot dates when constructing yield curve. I am wondering why do I get this negative error here? The settlements days should be 3 days.

Thank you!

## Answer by Luigi Ballabio (score 2)

https://quant.stackexchange.com/a/43892

The `ZeroCurve` instance starts at the first date you passed it, that is, one month after today's date. Any date before that will be in the past as far as the curve is concerned.

To avoid this, you will need to pass in today's date (or the settlement date, if you want to discount to that) as the first date in `spotDates`. You'll also need a corresponding rate in `spotRates`; if you don't have a value for the very short rate, the 1-month rate might work as a proxy.

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.