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.