3
$\begingroup$

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!

$\endgroup$

1 Answer 1

2
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ Thank you, Luigi! Adding current day works. But I am also confused why this example code runs with no error: quantlib.10058.n7.nabble.com/file/n18310/CDS.py Seems like it doesn't have current date in zero curve as well.. $\endgroup$
    – froyo_lyn
    Commented Feb 5, 2019 at 19:43
  • $\begingroup$ It might just be that, in that case, no CDS date falls between today and the first node, so the curve is not asked for discounts in that range of dates. (By the way, this means that the example code you linked "works", but it discounts incorrectly.) $\endgroup$ Commented Feb 6, 2019 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.