def test_FinIborDepositsFuturesSwaps(): spotDate = FinDate(6, 6, 2018) spotDays = 0 settlementDate = spotDate.addWeekDays(spotDays) depoDCCType = FinDayCountTypes.ACT_360 depos = [] depositRate = 0.0231381 depo = FinIborDeposit(settlementDate, "3M", depositRate, depoDCCType) depos.append(depo) depositRate = 0.027 depo = FinIborDeposit(settlementDate, "3M", depositRate, depoDCCType) depos.append(depo) depos = [] depo = FinIborDeposit(settlementDate, "1M", 0.0230, depoDCCType) depos.append(depo) depo = FinIborDeposit(settlementDate, "2M", 0.0235, depoDCCType) depos.append(depo) depo = FinIborDeposit(settlementDate, "3M", 0.0240, depoDCCType) depos.append(depo) fras = [] fraRate = futureToFRARate(97.6675, -0.00005) fraSettlementDate = spotDate.nextIMMDate() fraMaturityDate = fraSettlementDate.nextIMMDate() fra = FinIborFRA(fraSettlementDate, fraMaturityDate, fraRate, depoDCCType) fras.append(fra) fraRate = futureToFRARate(97.5200, -0.00060) fraSettlementDate = fraMaturityDate fraMaturityDate = fraSettlementDate.nextIMMDate() fra = FinIborFRA(fraSettlementDate, fraMaturityDate, fraRate, depoDCCType) fras.append(fra) fraRate = futureToFRARate(97.3550, -0.00146) fraSettlementDate = fraMaturityDate fraMaturityDate = fraSettlementDate.nextIMMDate() fra = FinIborFRA(fraSettlementDate, fraMaturityDate, fraRate, depoDCCType) fras.append(fra) fraRate = futureToFRARate(97.2450, -0.00263) fraSettlementDate = fraMaturityDate fraMaturityDate = fraSettlementDate.nextIMMDate() fra = FinIborFRA(fraSettlementDate, fraMaturityDate, fraRate, depoDCCType) fras.append(fra) fraRate = futureToFRARate(97.1450, -0.00411) fraSettlementDate = fraMaturityDate fraMaturityDate = fraSettlementDate.nextIMMDate() fra = FinIborFRA(fraSettlementDate, fraMaturityDate, fraRate, depoDCCType) fras.append(fra) fraRate = futureToFRARate(97.0750, -0.00589) fraSettlementDate = fraSettlementDate.nextIMMDate() fraMaturityDate = fraSettlementDate.nextIMMDate() fra = FinIborFRA(fraSettlementDate, fraMaturityDate, fraRate, depoDCCType) fras.append(fra) ########################################################################### spotDays = 2 startDate = spotDate.addWeekDays(spotDays) swaps = [] swapType = FinSwapTypes.PAYER fixedDCCType = FinDayCountTypes.THIRTY_E_360 fixedFreqType = FinFrequencyTypes.SEMI_ANNUAL floatFreqType = FinFrequencyTypes.QUARTERLY notional = 1000000 floatSpread = 0.0 floatDCCType = FinDayCountTypes.ACT_360 calendarType = FinCalendarTypes.US busDayAdjustRule = FinBusDayAdjustTypes.PRECEDING swapRate = 0.02776305 swap = FinIborSwap(startDate, "2Y", swapType, swapRate, fixedFreqType, fixedDCCType, notional, floatSpread, floatFreqType, floatDCCType, calendarType, busDayAdjustRule) swaps.append(swap) liborCurve = FinIborCurve(spotDate, depos, fras, swaps) times = np.linspace(0.0, 2.0, 25) dates = spotDate.addYears(times) zeroRates = liborCurve.zeroRate(dates) fwdRates = liborCurve.fwd(dates) if PLOT_GRAPHS: plt.figure(figsize=(8, 6)) plt.plot(times, zeroRates * 100, label="zero rates") plt.plot(times, fwdRates * 100, label="fwd rates") plt.xlabel("Times") plt.ylabel("CC forward rates") plt.legend() print("==============================================================") for fra in fras: print(fra) print("==============================================================") endDate = spotDate df = liborCurve.df(endDate) print(endDate, df) endDate = settlementDate df = liborCurve.df(endDate) print(endDate, df) endDate = FinDate(20, 6, 2018) df = liborCurve.df(endDate) print(endDate, df) for depo in depos: endDate = depo._maturityDate df = liborCurve.df(endDate) print(endDate, df) for fra in fras: endDate = fra._maturityDate df = liborCurve.df(endDate) print(endDate, df) for swap in swaps: endDate = swap._maturityDate df = liborCurve.df(endDate) print(endDate, df) swap.printFixedLegPV(spotDate) swap.printFloatLegPV(spotDate)
def buildIborCurve(valueDate): depoDCCType = FinDayCountTypes.THIRTY_E_360_ISDA depos = [] payFixed = FinSwapTypes.PAYER spotDays = 2 settlementDate = valueDate.addWeekDays(spotDays) depositRate = 0.050 maturityDate = settlementDate.addMonths(1) depo1 = FinIborDeposit(settlementDate, maturityDate, depositRate, depoDCCType) maturityDate = settlementDate.addMonths(3) depo2 = FinIborDeposit(settlementDate, maturityDate, depositRate, depoDCCType) maturityDate = settlementDate.addMonths(6) depo3 = FinIborDeposit(settlementDate, maturityDate, depositRate, depoDCCType) maturityDate = settlementDate.addMonths(9) depo4 = FinIborDeposit(settlementDate, maturityDate, depositRate, depoDCCType) maturityDate = settlementDate.addMonths(12) depo5 = FinIborDeposit(settlementDate, maturityDate, depositRate, depoDCCType) depos.append(depo1) depos.append(depo2) depos.append(depo3) depos.append(depo4) depos.append(depo5) fras = [] fixedDCCType = FinDayCountTypes.ACT_365F fixedFreqType = FinFrequencyTypes.SEMI_ANNUAL swaps = [] swapRate = 0.05 maturityDate = settlementDate.addMonths(24) swap1 = FinIborSwap(settlementDate, maturityDate, swapRate, payFixed, fixedFreqType, fixedDCCType) swaps.append(swap1) maturityDate = settlementDate.addMonths(36) swap2 = FinIborSwap(settlementDate, maturityDate, swapRate, payFixed, fixedFreqType, fixedDCCType) swaps.append(swap2) maturityDate = settlementDate.addMonths(48) swap3 = FinIborSwap(settlementDate, maturityDate, swapRate, payFixed, fixedFreqType, fixedDCCType) swaps.append(swap3) maturityDate = settlementDate.addMonths(60) swap4 = FinIborSwap(settlementDate, maturityDate, swapRate, payFixed, fixedFreqType, fixedDCCType) swaps.append(swap4) maturityDate = settlementDate.addMonths(72) swap5 = FinIborSwap(settlementDate, maturityDate, swapRate, payFixed, fixedFreqType, fixedDCCType) swaps.append(swap5) maturityDate = settlementDate.addMonths(84) swap6 = FinIborSwap(settlementDate, maturityDate, swapRate, payFixed, fixedFreqType, fixedDCCType) swaps.append(swap6) maturityDate = settlementDate.addMonths(96) swap7 = FinIborSwap(settlementDate, maturityDate, swapRate, payFixed, fixedFreqType, fixedDCCType) swaps.append(swap7) maturityDate = settlementDate.addMonths(108) swap8 = FinIborSwap(settlementDate, maturityDate, swapRate, payFixed, fixedFreqType, fixedDCCType) swaps.append(swap8) maturityDate = settlementDate.addMonths(120) swap9 = FinIborSwap(settlementDate, maturityDate, swapRate, payFixed, fixedFreqType, fixedDCCType) swaps.append(swap9) liborCurve = FinIborCurve(settlementDate, depos, fras, swaps) if 1 == 0: import numpy as np numSteps = 40 dt = 10 / numSteps times = np.linspace(0.0, 10.0, numSteps + 1) df0 = 1.0 for t in times[1:]: df1 = liborCurve.df(t) fwd = (df0 / df1 - 1.0) / dt print(t, df1, fwd) df0 = df1 return liborCurve
def test_FinIborDepositsFRAsSwaps(): valuationDate = FinDate(2019, 9, 18) dccType = FinDayCountTypes.THIRTY_E_360_ISDA depos = [] spotDays = 0 settlementDate = valuationDate.addWeekDays(spotDays) depositRate = 0.050 maturityDate = settlementDate.addMonths(1) depo = FinIborDeposit(settlementDate, maturityDate, depositRate, dccType) depos.append(depo) maturityDate = settlementDate.addMonths(2) depo = FinIborDeposit(settlementDate, maturityDate, depositRate, dccType) depos.append(depo) maturityDate = settlementDate.addMonths(3) depo = FinIborDeposit(settlementDate, maturityDate, depositRate, dccType) depos.append(depo) maturityDate = settlementDate.addMonths(6) depo = FinIborDeposit(settlementDate, maturityDate, depositRate, dccType) depos.append(depo) maturityDate = settlementDate.addMonths(9) depo = FinIborDeposit(settlementDate, maturityDate, depositRate, dccType) depos.append(depo) maturityDate = settlementDate.addMonths(12) depo = FinIborDeposit(settlementDate, maturityDate, depositRate, dccType) depos.append(depo) fras = [] # 1 x 4 FRA fraRate = 0.04 fraSettlementDate = settlementDate.addMonths(9) fraMaturityDate = settlementDate.addMonths(13) fra = FinIborFRA(fraSettlementDate, fraMaturityDate, fraRate, dccType) fras.append(fra) # 4 x 7 FRA fraRate = 0.03 fraSettlementDate = settlementDate.addMonths(13) fraMaturityDate = settlementDate.addMonths(17) fra = FinIborFRA(fraSettlementDate, fraMaturityDate, fraRate, dccType) fras.append(fra) # 4 x 7 FRA fraRate = 0.07 fraSettlementDate = settlementDate.addMonths(17) fraMaturityDate = settlementDate.addMonths(21) fra = FinIborFRA(fraSettlementDate, fraMaturityDate, fraRate, dccType) fras.append(fra) swaps = [] fixedDCCType = FinDayCountTypes.ACT_365F fixedFreqType = FinFrequencyTypes.SEMI_ANNUAL swapRate = 0.05 # maturityDate = settlementDate.addMonths(24) # swap = FinIborSwap(settlementDate, maturityDate, swapRate, fixedFreqType, # fixedDCCType) # swaps.append(swap) swapType = FinSwapTypes.PAYER maturityDate = settlementDate.addMonths(36) swap = FinIborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(48) swap = FinIborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(60) swap = FinIborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(72) swap = FinIborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(84) swap = FinIborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(96) swap = FinIborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(108) swap = FinIborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(120) swap = FinIborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(132) swap = FinIborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(144) swap = FinIborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(180) swap = FinIborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(240) swap = FinIborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(300) swap = FinIborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(360) swap = FinIborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) liborCurve = FinIborCurve(valuationDate, depos, fras, swaps) df = liborCurve.df(settlementDate) testCases.header("SETTLEMENT DATE", "DF") testCases.print(str(settlementDate), df) testCases.header("DATE", "DF") for deposit in depos: df = liborCurve.df(deposit._maturityDate) testCases.print(str(deposit._maturityDate), df) for swap in swaps: df = liborCurve.df(swap._maturityDate) testCases.print(str(swap._maturityDate), df)
def buildFullIssuerCurve2(mktSpreadBump, irBump): # https://www.markit.com/markit.jsp?jsppage=pv.jsp # YIELD CURVE 20 August 2020 SNAP AT 1600 m = 1.0 settlementDate = FinDate(24, 8, 2020) dcType = FinDayCountTypes.ACT_360 depos = [] maturityDate = settlementDate.addMonths(1) depo1 = FinIborDeposit(settlementDate, maturityDate, m * 0.001709, dcType) maturityDate = settlementDate.addMonths(2) depo2 = FinIborDeposit(settlementDate, maturityDate, m * 0.002123, dcType) maturityDate = settlementDate.addMonths(3) depo3 = FinIborDeposit(settlementDate, maturityDate, m * 0.002469, dcType) maturityDate = settlementDate.addMonths(6) depo4 = FinIborDeposit(settlementDate, maturityDate, m * 0.003045, dcType) maturityDate = settlementDate.addMonths(12) depo5 = FinIborDeposit(settlementDate, maturityDate, m * 0.004449, dcType) depos.append(depo1) depos.append(depo2) depos.append(depo3) depos.append(depo4) depos.append(depo5) swaps = [] dcType = FinDayCountTypes.THIRTY_E_360_ISDA fixedFreq = FinFrequencyTypes.SEMI_ANNUAL maturityDate = settlementDate.addMonths(24) swap1 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.002155 + irBump, fixedFreq, dcType) swaps.append(swap1) maturityDate = settlementDate.addMonths(36) swap2 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.002305 + irBump, fixedFreq, dcType) swaps.append(swap2) maturityDate = settlementDate.addMonths(48) swap3 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.002665 + irBump, fixedFreq, dcType) swaps.append(swap3) maturityDate = settlementDate.addMonths(60) swap4 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.003290 + irBump, fixedFreq, dcType) swaps.append(swap4) liborCurve = FinIborCurve(settlementDate, depos, [], swaps) cdsCoupon = 0.01 + mktSpreadBump cdsMarketContracts = [] effectiveDate = FinDate(21, 8, 2020) cds = FinCDS(effectiveDate, "6M", cdsCoupon) cdsMarketContracts.append(cds) cds = FinCDS(effectiveDate, "1Y", cdsCoupon) cdsMarketContracts.append(cds) cds = FinCDS(effectiveDate, "2Y", cdsCoupon) cdsMarketContracts.append(cds) cds = FinCDS(effectiveDate, "3Y", cdsCoupon) cdsMarketContracts.append(cds) cds = FinCDS(effectiveDate, "4Y", cdsCoupon) cdsMarketContracts.append(cds) cds = FinCDS(effectiveDate, "5Y", cdsCoupon) cdsMarketContracts.append(cds) cds = FinCDS(effectiveDate, "7Y", cdsCoupon) cdsMarketContracts.append(cds) cds = FinCDS(effectiveDate, "10Y", cdsCoupon) cdsMarketContracts.append(cds) recoveryRate = 0.40 issuerCurve = FinCDSCurve(settlementDate, cdsMarketContracts, liborCurve, recoveryRate) testCases.header("DATE", "DISCOUNT_FACTOR", "SURV_PROB") years = np.linspace(0.0, 10.0, 20) dates = settlementDate.addYears(years) for dt in dates: df = liborCurve.df(dt) q = issuerCurve.survProb(dt) testCases.print("%16s" % dt, "%12.8f" % df, "%12.8f" % q) return liborCurve, issuerCurve