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