def test_fullPriceCDS(): liborCurve, issuerCurve = buildFullIssuerCurve(0.0, 0.0) # This is the 10 year contract at an off market coupon maturityDate = FinDate(2029, 6, 20) cdsCoupon = 0.0150 notional = ONE_MILLION longProtection = False tradeDate = FinDate(2019, 8, 9) valuationDate = tradeDate.addDays(1) effectiveDate = valuationDate cdsContract = FinCDS(effectiveDate, maturityDate, cdsCoupon, notional, longProtection) cdsRecovery = 0.40 print("LABEL", "VALUE") spd = cdsContract.parSpread(valuationDate, issuerCurve, cdsRecovery) * 10000.0 print("PAR_SPREAD", spd) v = cdsContract.value(valuationDate, issuerCurve, cdsRecovery) print("FULL_VALUE", v[0]) print("CLEAN_VALUE", v[1]) p = cdsContract.cleanPrice(valuationDate, issuerCurve, cdsRecovery) print("CLEAN_PRICE", p) accruedDays = cdsContract.accruedDays() print("ACCRUED_DAYS", accruedDays) accruedInterest = cdsContract.accruedInterest() print("ACCRUED_COUPON", accruedInterest) protPV = cdsContract.protectionLegPV(valuationDate, issuerCurve, cdsRecovery) print("PROTECTION_PV", protPV) premPV = cdsContract.premiumLegPV(valuationDate, issuerCurve, cdsRecovery) print("PREMIUM_PV", premPV) fullRPV01, cleanRPV01 = cdsContract.riskyPV01(valuationDate, issuerCurve) print("FULL_RPV01", fullRPV01) print("CLEAN_RPV01", cleanRPV01) bump = 1.0 / 10000.0 # 1 bp liborCurve, issuerCurve = buildFullIssuerCurve(bump, 0) v_bump = cdsContract.value(valuationDate, issuerCurve, cdsRecovery) dv = v_bump[0] - v[0] print("CREDIT_DV01", dv) # Interest Rate Bump liborCurve, issuerCurve = buildFullIssuerCurve(0, bump) v_bump = cdsContract.value(valuationDate, issuerCurve, cdsRecovery) dv = v_bump[0] - v[0] print("INTEREST_DV01", dv)
def test_valueCDSIndex(): # We treat an index as a CDS contract with a flat CDS curve tradeDate = FinDate(2006, 2, 7) liborCurve = buildIborCurve(tradeDate) issuerCurve = buildIssuerCurve(tradeDate, liborCurve) stepInDate = tradeDate.addDays(1) valuationDate = stepInDate maturityDate = FinDate(2010, 6, 20) cdsRecovery = 0.40 notional = 10.0 * ONE_MILLION longProtection = True indexCoupon = 0.004 cdsIndexContract = FinCDS(stepInDate, maturityDate, indexCoupon, notional, longProtection) # cdsIndexContract.print(valuationDate) testCases.header("LABEL", "VALUE") spd = cdsIndexContract.parSpread( valuationDate, issuerCurve, cdsRecovery) * 10000.0 testCases.print("PAR SPREAD", spd) v = cdsIndexContract.value(valuationDate, issuerCurve, cdsRecovery) testCases.print("FULL VALUE", v['full_pv']) testCases.print("CLEAN VALUE", v['clean_pv']) p = cdsIndexContract.cleanPrice(valuationDate, issuerCurve, cdsRecovery) testCases.print("CLEAN PRICE", p) accruedDays = cdsIndexContract.accruedDays() testCases.print("ACCRUED DAYS", accruedDays) accruedInterest = cdsIndexContract.accruedInterest() testCases.print("ACCRUED COUPON", accruedInterest) protPV = cdsIndexContract.protectionLegPV( valuationDate, issuerCurve, cdsRecovery) testCases.print("PROTECTION LEG PV", protPV) premPV = cdsIndexContract.premiumLegPV( valuationDate, issuerCurve, cdsRecovery) testCases.print("PREMIUM LEG PV", premPV) fullRPV01, cleanRPV01 = cdsIndexContract.riskyPV01( valuationDate, issuerCurve) testCases.print("FULL RPV01", fullRPV01) testCases.print("CLEAN RPV01", cleanRPV01)
def test_fullPriceCDSwaption(): # This reproduces example on page 38 of Open Gamma note on CDS Option tradeDate = FinDate(2014, 2, 5) _, issuerCurve = buildFullIssuerCurve(tradeDate) stepInDate = tradeDate.addDays(1) valuationDate = stepInDate expiryDate = FinDate(2014, 3, 20) maturityDate = FinDate(2019, 6, 20) cdsRecovery = 0.40 notional = 100.0 longProtection = False cdsCoupon = 0.0 # NOT KNOWN cdsContract = FinCDS(stepInDate, maturityDate, cdsCoupon, notional, longProtection) testCases.banner( "=============================== CDS ===============================") # cdsContract.print(valuationDate) testCases.header("LABEL", "VALUE") spd = cdsContract.parSpread( valuationDate, issuerCurve, cdsRecovery) * 10000.0 testCases.print("PAR SPREAD:", spd) v = cdsContract.value(valuationDate, issuerCurve, cdsRecovery) testCases.print("FULL VALUE", v['full_pv']) testCases.print("CLEAN VALUE", v['clean_pv']) p = cdsContract.cleanPrice(valuationDate, issuerCurve, cdsRecovery) testCases.print("CLEAN PRICE", p) accruedDays = cdsContract.accruedDays() testCases.print("ACCRUED DAYS", accruedDays) accruedInterest = cdsContract.accruedInterest() testCases.print("ACCRUED COUPON", accruedInterest) protPV = cdsContract.protectionLegPV( valuationDate, issuerCurve, cdsRecovery) testCases.print("PROTECTION LEG PV", protPV) premPV = cdsContract.premiumLegPV(valuationDate, issuerCurve, cdsRecovery) testCases.print("PREMIUM LEG PV", premPV) fullRPV01, cleanRPV01 = cdsContract.riskyPV01(valuationDate, issuerCurve) testCases.print("FULL RPV01", fullRPV01) testCases.print("CLEAN RPV01", cleanRPV01) # cdsContract.printFlows(issuerCurve) testCases.banner( "=========================== FORWARD CDS ===========================") cdsContract = FinCDS(expiryDate, maturityDate, cdsCoupon, notional, longProtection) # cdsContract.print(valuationDate) spd = cdsContract.parSpread( valuationDate, issuerCurve, cdsRecovery) * 10000.0 testCases.print("PAR SPREAD", spd) v = cdsContract.value(valuationDate, issuerCurve, cdsRecovery) testCases.print("FULL VALUE", v['full_pv']) testCases.print("CLEAN VALUE", v['clean_pv']) protPV = cdsContract.protectionLegPV( valuationDate, issuerCurve, cdsRecovery) testCases.print("PROTECTION LEG PV", protPV) premPV = cdsContract.premiumLegPV(valuationDate, issuerCurve, cdsRecovery) testCases.print("PREMIUM LEG PV", premPV) fullRPV01, cleanRPV01 = cdsContract.riskyPV01(valuationDate, issuerCurve) testCases.print("FULL RPV01", fullRPV01) testCases.print("CLEAN RPV01", cleanRPV01) # cdsContract.printFlows(issuerCurve) testCases.banner( "========================== CDS OPTIONS ============================") cdsCoupon = 0.01 volatility = 0.3 testCases.print("Expiry Date:", str(expiryDate)) testCases.print("Maturity Date:", str(maturityDate)) testCases.print("CDS Coupon:", cdsCoupon) testCases.header("STRIKE", "FULL VALUE", "IMPLIED VOL") for strike in np.linspace(100, 300, 41): cdsOption = FinCDSOption(expiryDate, maturityDate, strike / 10000.0, notional) v = cdsOption.value(valuationDate, issuerCurve, volatility) vol = cdsOption.impliedVolatility(valuationDate, issuerCurve, v) testCases.print(strike, v, vol)
def test_fullPriceCDSModelCheck(): testCases.print("Example", "MARKIT CHECK 19 Aug 2020") liborCurve, issuerCurve = buildFullIssuerCurve2(0.0, 0.0) # This is the 10 year contract at an off market coupon maturityDate = FinDate(20, 6, 2025) cdsCoupon = 0.050 notional = ONE_MILLION longProtection = True tradeDate = FinDate(20, 8, 2020) effectiveDate = FinDate(21, 8, 2020) valuationDate = tradeDate cdsContract = FinCDS(effectiveDate, maturityDate, cdsCoupon, notional, longProtection) cdsRecovery = 0.40 testCases.header("LABEL", "VALUE") spd = cdsContract.parSpread(valuationDate, issuerCurve, cdsRecovery) * 10000.0 testCases.print("PAR_SPREAD", spd) v = cdsContract.value(valuationDate, issuerCurve, cdsRecovery) testCases.print("FULL_VALUE", v['full_pv']) testCases.print("CLEAN_VALUE", v['clean_pv']) p = cdsContract.cleanPrice(valuationDate, issuerCurve, cdsRecovery) testCases.print("CLEAN_PRICE", p) accruedDays = cdsContract.accruedDays() testCases.print("ACCRUED_DAYS", accruedDays) accruedInterest = cdsContract.accruedInterest() testCases.print("ACCRUED_COUPON", accruedInterest) protPV = cdsContract.protectionLegPV(valuationDate, issuerCurve, cdsRecovery) testCases.print("PROTECTION_PV", protPV) premPV = cdsContract.premiumLegPV(valuationDate, issuerCurve, cdsRecovery) testCases.print("PREMIUM_PV", premPV) rpv01 = cdsContract.riskyPV01(valuationDate, issuerCurve) testCases.print("FULL_RPV01", rpv01['full_rpv01']) testCases.print("CLEAN_RPV01", rpv01['clean_rpv01']) creditDV01 = cdsContract.creditDV01(valuationDate, issuerCurve, cdsRecovery) testCases.print("CREDIT DV01", creditDV01) interestDV01 = cdsContract.interestDV01(valuationDate, issuerCurve, cdsRecovery) testCases.print("INTEREST DV01", interestDV01) # Consider fast approximation t = (maturityDate - valuationDate) / gDaysInYear z = liborCurve.df(maturityDate) r = -np.log(z) / t mktSpread = 0.01 v_approx = cdsContract.valueFastApprox(valuationDate, r, mktSpread, cdsRecovery) testCases.header("FAST VALUATIONS", "VALUE") testCases.print("FULL APPROX VALUE", v_approx[0]) testCases.print("CLEAN APPROX VALUE", v_approx[1]) testCases.print("APPROX CREDIT DV01", v_approx[2]) testCases.print("APPROX INTEREST DV01", v_approx[3])
def test_fullPriceCDS1(): mktSpread = 0.040 testCases.header("Example", "Markit 9 Aug 2019") liborCurve, issuerCurve = buildFullIssuerCurve1(0.0, 0.0) # This is the 10 year contract at an off market coupon maturityDate = FinDate(2029, 6, 20) cdsCoupon = 0.0150 notional = ONE_MILLION longProtection = False tradeDate = FinDate(2019, 8, 9) valuationDate = tradeDate.addDays(1) effectiveDate = valuationDate cdsContract = FinCDS(effectiveDate, maturityDate, cdsCoupon, notional, longProtection) cdsRecovery = 0.40 testCases.header("LABEL", "VALUE") spd = cdsContract.parSpread(valuationDate, issuerCurve, cdsRecovery) * 10000.0 testCases.print("PAR_SPREAD", spd) v = cdsContract.value(valuationDate, issuerCurve, cdsRecovery) testCases.print("FULL_VALUE", v['full_pv']) testCases.print("CLEAN_VALUE", v['clean_pv']) p = cdsContract.cleanPrice(valuationDate, issuerCurve, cdsRecovery) testCases.print("CLEAN_PRICE", p) accruedDays = cdsContract.accruedDays() testCases.print("ACCRUED_DAYS", accruedDays) accruedInterest = cdsContract.accruedInterest() testCases.print("ACCRUED_COUPON", accruedInterest) protPV = cdsContract.protectionLegPV(valuationDate, issuerCurve, cdsRecovery) testCases.print("PROTECTION_PV", protPV) premPV = cdsContract.premiumLegPV(valuationDate, issuerCurve, cdsRecovery) testCases.print("PREMIUM_PV", premPV) fullRPV01, cleanRPV01 = cdsContract.riskyPV01(valuationDate, issuerCurve) testCases.print("FULL_RPV01", fullRPV01) testCases.print("CLEAN_RPV01", cleanRPV01) # cdsContract.printFlows(issuerCurve) bump = 1.0 / 10000.0 # 1 bp liborCurve, issuerCurve = buildFullIssuerCurve1(bump, 0) v_bump = cdsContract.value(valuationDate, issuerCurve, cdsRecovery) dv = v_bump['full_pv'] - v['full_pv'] testCases.print("CREDIT_DV01", dv) # Interest Rate Bump liborCurve, issuerCurve = buildFullIssuerCurve1(0, bump) v_bump = cdsContract.value(valuationDate, issuerCurve, cdsRecovery) dv = v_bump['full_pv'] - v['full_pv'] testCases.print("INTEREST_DV01", dv) t = (maturityDate - valuationDate) / gDaysInYear z = liborCurve.df(maturityDate) r = -np.log(z) / t v_approx = cdsContract.valueFastApprox(valuationDate, r, mktSpread, cdsRecovery) testCases.print("FULL APPROX VALUE", v_approx[0]) testCases.print("CLEAN APPROX VALUE", v_approx[1]) testCases.print("APPROX CREDIT DV01", v_approx[2]) testCases.print("APPROX INTEREST DV01", v_approx[3])