def test_FinIborDepositsAndSwaps(valuationDate): depoBasis = FinDayCountTypes.THIRTY_E_360_ISDA depos = [] spotDays = 0 settlementDate = valuationDate.addWeekDays(spotDays) depositRate = 0.05 depo1 = FinIborDeposit(settlementDate, "1M", depositRate, depoBasis) depo2 = FinIborDeposit(settlementDate, "3M", depositRate, depoBasis) depo3 = FinIborDeposit(settlementDate, "6M", depositRate, depoBasis) depos.append(depo1) depos.append(depo2) depos.append(depo3) fras = [] swaps = [] fixedBasis = FinDayCountTypes.ACT_365F fixedFreq = FinFrequencyTypes.SEMI_ANNUAL fixedLegType = FinSwapTypes.PAY swapRate = 0.05 swap1 = FinIborSwap(settlementDate, "1Y", fixedLegType, swapRate, fixedFreq, fixedBasis) swap2 = FinIborSwap(settlementDate, "3Y", fixedLegType, swapRate, fixedFreq, fixedBasis) swap3 = FinIborSwap(settlementDate, "5Y", fixedLegType, swapRate, fixedFreq, fixedBasis) swaps.append(swap1) swaps.append(swap2) swaps.append(swap3) liborCurve = FinIborSingleCurve(valuationDate, depos, fras, swaps) return liborCurve
def buildIborCurve(valuationDate): depoDCCType = FinDayCountTypes.THIRTY_E_360_ISDA depos = [] payFixed = FinSwapTypes.PAY spotDays = 2 settlementDate = valuationDate.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 = FinIborSingleCurve(valuationDate, 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 buildFullIssuerCurve(valuationDate): dcType = FinDayCountTypes.ACT_360 depos = [] irBump = 0.0 m = 1.0 # 0.00000000000 spotDays = 0 settlementDate = valuationDate.addDays(spotDays) maturityDate = settlementDate.addMonths(1) depo1 = FinIborDeposit(settlementDate, maturityDate, m * 0.0016, dcType) maturityDate = settlementDate.addMonths(2) depo2 = FinIborDeposit(settlementDate, maturityDate, m * 0.0020, dcType) maturityDate = settlementDate.addMonths(3) depo3 = FinIborDeposit(settlementDate, maturityDate, m * 0.0024, dcType) maturityDate = settlementDate.addMonths(6) depo4 = FinIborDeposit(settlementDate, maturityDate, m * 0.0033, dcType) maturityDate = settlementDate.addMonths(12) depo5 = FinIborDeposit(settlementDate, maturityDate, m * 0.0056, dcType) depos.append(depo1) depos.append(depo2) depos.append(depo3) depos.append(depo4) depos.append(depo5) fras = [] spotDays = 2 settlementDate = valuationDate.addDays(spotDays) swaps = [] dcType = FinDayCountTypes.THIRTY_E_360_ISDA fixedFreq = FinFrequencyTypes.SEMI_ANNUAL maturityDate = settlementDate.addMonths(24) swap1 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAY, m * 0.0044 + irBump, fixedFreq, dcType) swaps.append(swap1) maturityDate = settlementDate.addMonths(36) swap2 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAY, m * 0.0078 + irBump, fixedFreq, dcType) swaps.append(swap2) maturityDate = settlementDate.addMonths(48) swap3 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAY, m * 0.0119 + irBump, fixedFreq, dcType) swaps.append(swap3) maturityDate = settlementDate.addMonths(60) swap4 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAY, m * 0.0158 + irBump, fixedFreq, dcType) swaps.append(swap4) maturityDate = settlementDate.addMonths(72) swap5 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAY, m * 0.0192 + irBump, fixedFreq, dcType) swaps.append(swap5) maturityDate = settlementDate.addMonths(84) swap6 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAY, m * 0.0219 + irBump, fixedFreq, dcType) swaps.append(swap6) maturityDate = settlementDate.addMonths(96) swap7 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAY, m * 0.0242 + irBump, fixedFreq, dcType) swaps.append(swap7) maturityDate = settlementDate.addMonths(108) swap8 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAY, m * 0.0261 + irBump, fixedFreq, dcType) swaps.append(swap8) maturityDate = settlementDate.addMonths(120) swap9 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAY, m * 0.0276 + irBump, fixedFreq, dcType) swaps.append(swap9) liborCurve = FinIborSingleCurve(valuationDate, depos, fras, swaps) cdsMarketContracts = [] cdsCoupon = 0.005743 maturityDate = valuationDate.nextCDSDate(6) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) cdsCoupon = 0.007497 maturityDate = valuationDate.nextCDSDate(12) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) cdsCoupon = 0.011132 maturityDate = valuationDate.nextCDSDate(24) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) cdsCoupon = 0.013932 maturityDate = valuationDate.nextCDSDate(36) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) cdsCoupon = 0.015764 maturityDate = valuationDate.nextCDSDate(48) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) cdsCoupon = 0.017366 maturityDate = valuationDate.nextCDSDate(60) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) cdsCoupon = 0.020928 maturityDate = valuationDate.nextCDSDate(84) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) cdsCoupon = 0.022835 maturityDate = valuationDate.nextCDSDate(120) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) recoveryRate = 0.40 issuerCurve = FinCDSCurve(valuationDate, cdsMarketContracts, liborCurve, recoveryRate) return liborCurve, issuerCurve
def buildIborSingleCurve(valuationDate): settlementDate = valuationDate.addDays(2) dcType = FinDayCountTypes.ACT_360 depos = [] fras = [] swaps = [] maturityDate = settlementDate.addMonths(1) depo1 = FinIborDeposit(valuationDate, maturityDate, -0.00251, dcType) depos.append(depo1) # Series of 1M futures startDate = settlementDate.nextIMMDate() endDate = startDate.addMonths(1) fra = FinIborFRA(startDate, endDate, -0.0023, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinIborFRA(startDate, endDate, -0.00234, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinIborFRA(startDate, endDate, -0.00225, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinIborFRA(startDate, endDate, -0.00226, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinIborFRA(startDate, endDate, -0.00219, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinIborFRA(startDate, endDate, -0.00213, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinIborFRA(startDate, endDate, -0.00186, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinIborFRA(startDate, endDate, -0.00189, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinIborFRA(startDate, endDate, -0.00175, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinIborFRA(startDate, endDate, -0.00143, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinIborFRA(startDate, endDate, -0.00126, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinIborFRA(startDate, endDate, -0.00126, dcType) fras.append(fra) ########################################################################### ########################################################################### ########################################################################### ########################################################################### fixedFreq = FinFrequencyTypes.ANNUAL dcType = FinDayCountTypes.THIRTY_E_360 fixedLegType = FinSwapTypes.PAY ####################################### maturityDate = settlementDate.addMonths(24) swapRate = -0.001506 swap1 = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreq, dcType) swaps.append(swap1) ####################################### maturityDate = settlementDate.addMonths(36) swapRate = -0.000185 swap2 = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreq, dcType) swaps.append(swap2) ####################################### maturityDate = settlementDate.addMonths(48) swapRate = 0.001358 swap3 = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreq, dcType) swaps.append(swap3) ####################################### maturityDate = settlementDate.addMonths(60) swapRate = 0.0027652 swap4 = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreq, dcType) swaps.append(swap4) ####################################### maturityDate = settlementDate.addMonths(72) swapRate = 0.0041539 swap5 = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreq, dcType) swaps.append(swap5) ####################################### maturityDate = settlementDate.addMonths(84) swapRate = 0.0054604 swap6 = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreq, dcType) swaps.append(swap6) ####################################### maturityDate = settlementDate.addMonths(96) swapRate = 0.006674 swap7 = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreq, dcType) swaps.append(swap7) ####################################### maturityDate = settlementDate.addMonths(108) swapRate = 0.007826 swap8 = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreq, dcType) swaps.append(swap8) ####################################### maturityDate = settlementDate.addMonths(120) swapRate = 0.008821 swap9 = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreq, dcType) swaps.append(swap9) ####################################### maturityDate = settlementDate.addMonths(132) swapRate = 0.0097379 swap10 = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreq, dcType) swaps.append(swap10) ####################################### maturityDate = settlementDate.addMonths(144) swapRate = 0.0105406 swap11 = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreq, dcType) swaps.append(swap11) ####################################### maturityDate = settlementDate.addMonths(180) swapRate = 0.0123927 swap12 = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreq, dcType) swaps.append(swap12) ####################################### maturityDate = settlementDate.addMonths(240) swapRate = 0.0139882 swap13 = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreq, dcType) swaps.append(swap13) ####################################### maturityDate = settlementDate.addMonths(300) swapRate = 0.0144972 swap14 = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreq, dcType) swaps.append(swap14) ####################################### maturityDate = settlementDate.addMonths(360) swapRate = 0.0146081 swap15 = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreq, dcType) swaps.append(swap15) ####################################### maturityDate = settlementDate.addMonths(420) swapRate = 0.01461897 swap16 = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreq, dcType) swaps.append(swap16) ####################################### maturityDate = settlementDate.addMonths(480) swapRate = 0.014567455 swap17 = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreq, dcType) swaps.append(swap17) ####################################### maturityDate = settlementDate.addMonths(540) swapRate = 0.0140826 swap18 = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreq, dcType) swaps.append(swap18) ####################################### maturityDate = settlementDate.addMonths(600) swapRate = 0.01436822 swap19 = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreq, dcType) swaps.append(swap19) ######################################## liborCurve = FinIborSingleCurve(valuationDate, depos, fras, swaps) testCases.header("LABEL", "DATE", "VALUE") ''' Check calibration ''' for depo in depos: v = depo.value(settlementDate, liborCurve) testCases.print("DEPO VALUE:", depo._maturityDate, v) for fra in fras: v = fra.value(settlementDate, liborCurve) testCases.print("FRA VALUE:", fra._maturityDate, v) for swap in swaps: v = swap.value(settlementDate, liborCurve) testCases.print("SWAP VALUE:", swap._maturityDate, v) return liborCurve
def buildIborCurve(tradeDate): valuationDate = tradeDate.addDays(1) dcType = FinDayCountTypes.ACT_360 depos = [] fras = [] swaps = [] dcType = FinDayCountTypes.THIRTY_E_360_ISDA fixedFreq = FinFrequencyTypes.SEMI_ANNUAL settlementDate = valuationDate maturityDate = settlementDate.addMonths(12) swap1 = FinIborSwap( settlementDate, maturityDate, FinSwapTypes.PAY, 0.0502, fixedFreq, dcType) swaps.append(swap1) maturityDate = settlementDate.addMonths(24) swap2 = FinIborSwap( settlementDate, maturityDate, FinSwapTypes.PAY, 0.0502, fixedFreq, dcType) swaps.append(swap2) maturityDate = settlementDate.addMonths(36) swap3 = FinIborSwap( settlementDate, maturityDate, FinSwapTypes.PAY, 0.0501, fixedFreq, dcType) swaps.append(swap3) maturityDate = settlementDate.addMonths(48) swap4 = FinIborSwap( settlementDate, maturityDate, FinSwapTypes.PAY, 0.0502, fixedFreq, dcType) swaps.append(swap4) maturityDate = settlementDate.addMonths(60) swap5 = FinIborSwap( settlementDate, maturityDate, FinSwapTypes.PAY, 0.0501, fixedFreq, dcType) swaps.append(swap5) liborCurve = FinIborSingleCurve(valuationDate, depos, fras, swaps) return liborCurve
def test_bloombergPricingExample(interpType): ''' This is an example of a replication of a BBG example from https://github.com/vilen22/curve-building/blob/master/Bloomberg%20Curve%20Building%20Replication.xlsx ''' valuationDate = FinDate(6, 6, 2018) # We do the O/N rate which settles on trade date spotDays = 0 settlementDate = valuationDate.addWeekDays(spotDays) depoDCCType = FinDayCountTypes.ACT_360 depos = [] depositRate = 0.0231381 maturityDate = settlementDate.addMonths(3) depo = FinIborDeposit(settlementDate, maturityDate, depositRate, depoDCCType) depos.append(depo) futs = [] fut = FinIborFuture(valuationDate, 1) futs.append(fut) fut = FinIborFuture(valuationDate, 2) futs.append(fut) fut = FinIborFuture(valuationDate, 3) futs.append(fut) fut = FinIborFuture(valuationDate, 4) futs.append(fut) fut = FinIborFuture(valuationDate, 5) futs.append(fut) fut = FinIborFuture(valuationDate, 6) futs.append(fut) fras = [None] * 6 fras[0] = futs[0].toFRA(97.6675, -0.00005) fras[1] = futs[1].toFRA(97.5200, -0.00060) fras[2] = futs[2].toFRA(97.3550, -0.00146) fras[3] = futs[3].toFRA(97.2450, -0.00263) fras[4] = futs[4].toFRA(97.1450, -0.00411) fras[5] = futs[5].toFRA(97.0750, -0.00589) accrual = FinDayCountTypes.THIRTY_E_360 freq = FinFrequencyTypes.SEMI_ANNUAL spotDays = 2 settlementDate = valuationDate.addWeekDays(spotDays) notional = ONE_MILLION fixedLegType = FinSwapTypes.PAY swaps = [] swap = FinIborSwap(settlementDate, "2Y", fixedLegType, (2.77417 + 2.77844) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwap(settlementDate, "3Y", fixedLegType, (2.86098 + 2.86582) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwap(settlementDate, "4Y", fixedLegType, (2.90240 + 2.90620) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwap(settlementDate, "5Y", fixedLegType, (2.92944 + 2.92906) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwap(settlementDate, "6Y", fixedLegType, (2.94001 + 2.94499) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwap(settlementDate, "7Y", fixedLegType, (2.95352 + 2.95998) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwap(settlementDate, "8Y", fixedLegType, (2.96830 + 2.97400) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwap(settlementDate, "9Y", fixedLegType, (2.98403 + 2.98817) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwap(settlementDate, "10Y", fixedLegType, (2.99716 + 3.00394) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwap(settlementDate, "11Y", fixedLegType, (3.01344 + 3.01596) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwap(settlementDate, "12Y", fixedLegType, (3.02276 + 3.02684) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwap(settlementDate, "15Y", fixedLegType, (3.04092 + 3.04508) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwap(settlementDate, "20Y", fixedLegType, (3.04417 + 3.05183) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwap(settlementDate, "25Y", fixedLegType, (3.03219 + 3.03621) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwap(settlementDate, "30Y", fixedLegType, (3.01030 + 3.01370) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwap(settlementDate, "40Y", fixedLegType, (2.96946 + 2.97354) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwap(settlementDate, "50Y", fixedLegType, (2.91552 + 2.93748) / 200, freq, accrual) swaps.append(swap) liborCurve = FinIborSingleCurve(valuationDate, depos, fras, swaps, interpType) # The valuation of 53714.55 is very close to the spreadsheet value 53713.96 principal = 0.0 # Pay fixed so make fixed leg value negative testCases.header("VALUATION TO TODAY DATE", " PV") testCases.print( "VALUE:", swaps[0].value(valuationDate, liborCurve, liborCurve, None)) testCases.print("FIXED:", -swaps[0]._fixedLeg.value(valuationDate, liborCurve)) testCases.print( "FLOAT:", swaps[0]._floatLeg.value(valuationDate, liborCurve, liborCurve, None)) # Pay fixed so make fixed leg value negative testCases.header("VALUATION TO SWAP SETTLEMENT DATE", " PV") testCases.print( "VALUE:", swaps[0].value(settlementDate, liborCurve, liborCurve, None)) testCases.print("FIXED:", -swaps[0]._fixedLeg.value(settlementDate, liborCurve)) testCases.print( "FLOAT:", swaps[0]._floatLeg.value(settlementDate, liborCurve, liborCurve, None)) # swaps[0].printFixedLegPV() # swaps[0].printFloatLegPV() if 1 == 0: plt.figure() years = np.linspace(0, 50, 500) dates = settlementDate.addYears(years) fwds = liborCurve.fwd(dates) plt.plot(years, fwds, label="Fwd Rate") plt.title(interpType) plt.xlabel("Years") plt.legend() years = np.linspace(0, 50, 500) dates = settlementDate.addYears(years) fwds = liborCurve.zeroRate(dates) plt.plot(years, fwds, label="Zero Rate") plt.title(interpType) plt.xlabel("Years") plt.ylabel("Rate") plt.legend()
def test_derivativePricingExample(): valuationDate = FinDate(10, 11, 2011) dccType = FinDayCountTypes.ACT_360 depos = [] # We do the O/N rate which settles on trade date spotDays = 0 settlementDate = valuationDate.addWeekDays(spotDays) depositRate = 0.001410 depo = FinIborDeposit(settlementDate, "ON", depositRate, dccType) depos.append(depo) spotDays = 1 settlementDate = valuationDate.addWeekDays(spotDays) depositRate = 0.001410 depo = FinIborDeposit(settlementDate, "TN", depositRate, dccType) depos.append(depo) spotDays = 2 settlementDate = valuationDate.addWeekDays(spotDays) depositRate = 0.001910 depo = FinIborDeposit(settlementDate, "1W", depositRate, dccType) depos.append(depo) depositRate = 0.002090 depo = FinIborDeposit(settlementDate, "2W", depositRate, dccType) depos.append(depo) depositRate = 0.002490 depo = FinIborDeposit(settlementDate, "1M", depositRate, dccType) depos.append(depo) depositRate = 0.003450 depo = FinIborDeposit(settlementDate, "2M", depositRate, dccType) depos.append(depo) depositRate = 0.004570 depo = FinIborDeposit(settlementDate, "3M", depositRate, dccType) depos.append(depo) depositRate = 0.005230 depo = FinIborDeposit(settlementDate, "4M", depositRate, dccType) depos.append(depo) depositRate = 0.005860 depo = FinIborDeposit(settlementDate, "5M", depositRate, dccType) depos.append(depo) depositRate = 0.006540 depo = FinIborDeposit(settlementDate, "6M", depositRate, dccType) depos.append(depo) depositRate = 0.007080 depo = FinIborDeposit(settlementDate, "7M", depositRate, dccType) depos.append(depo) depositRate = 0.007540 depo = FinIborDeposit(settlementDate, "8M", depositRate, dccType) depos.append(depo) depositRate = 0.008080 depo = FinIborDeposit(settlementDate, "9M", depositRate, dccType) depos.append(depo) depositRate = 0.008570 depo = FinIborDeposit(settlementDate, "10M", depositRate, dccType) depos.append(depo) depositRate = 0.009130 depo = FinIborDeposit(settlementDate, "11M", depositRate, dccType) depos.append(depo) fras = [] swaps = [] dayCountType = FinDayCountTypes.THIRTY_E_360_ISDA # dayCountType = FinDayCountTypes.ACT_360 freqType = FinFrequencyTypes.SEMI_ANNUAL fixedLegType = FinSwapTypes.PAY swapRate = 0.0058 swap = FinIborSwap(settlementDate, "1Y", fixedLegType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0060 swap = FinIborSwap(settlementDate, "2Y", fixedLegType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0072 swap = FinIborSwap(settlementDate, "3Y", fixedLegType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0096 swap = FinIborSwap(settlementDate, "4Y", fixedLegType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0124 swap = FinIborSwap(settlementDate, "5Y", fixedLegType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0173 swap = FinIborSwap(settlementDate, "7Y", fixedLegType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0219 swap = FinIborSwap(settlementDate, "10Y", fixedLegType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0283 swap = FinIborSwap(settlementDate, "30Y", fixedLegType, swapRate, freqType, dayCountType) swaps.append(swap) numRepeats = 10 start = time.time() for _ in range(0, numRepeats): _ = FinIborSingleCurve(valuationDate, depos, fras, swaps, FinInterpTypes.FLAT_FWD_RATES) end = time.time() elapsed1 = end - start start = time.time() for _ in range(0, numRepeats): _ = FinIborSingleCurve(valuationDate, depos, fras, swaps, FinInterpTypes.FLAT_FWD_RATES) end = time.time() elapsed2 = end - start testCases.header("METHOD", "TIME") testCases.print("NON-LINEAR SOLVER BOOTSTRAP", elapsed1 / numRepeats) testCases.print("LINEAR SWAP BOOTSTRAP", elapsed2 / numRepeats)
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 = [] fixedLegType = FinSwapTypes.PAY fixedDCCType = FinDayCountTypes.THIRTY_E_360 fixedFreqType = FinFrequencyTypes.SEMI_ANNUAL floatFreqType = FinFrequencyTypes.QUARTERLY notional = 1000000 principal = 0.0 floatSpread = 0.0 floatDCCType = FinDayCountTypes.ACT_360 calendarType = FinCalendarTypes.UNITED_STATES busDayAdjustRule = FinBusDayAdjustTypes.PRECEDING swapRate = 0.02776305 swap = FinIborSwap(startDate, "2Y", fixedLegType, swapRate, fixedFreqType, fixedDCCType, notional, floatSpread, floatFreqType, floatDCCType, calendarType, busDayAdjustRule) swaps.append(swap) liborCurve = FinIborSingleCurve(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 test_FinIborDepositsFRAsSwaps(): valuationDate = FinDate(18, 9, 2019) 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) fixedLegType = FinSwapTypes.PAY maturityDate = settlementDate.addMonths(36) swap = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(48) swap = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(60) swap = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(72) swap = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(84) swap = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(96) swap = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(108) swap = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(120) swap = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(132) swap = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(144) swap = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(180) swap = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(240) swap = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(300) swap = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(360) swap = FinIborSwap(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) liborCurve = FinIborSingleCurve(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 test_FinCDSCurve(): curveDate = FinDate(20, 12, 2018) swaps = [] depos = [] fras = [] fixedDCC = FinDayCountTypes.ACT_365F fixedFreq = FinFrequencyTypes.SEMI_ANNUAL fixedCoupon = 0.05 for i in range(1, 11): maturityDate = curveDate.addMonths(12 * i) swap = FinIborSwap(curveDate, maturityDate, FinSwapTypes.PAY, fixedCoupon, fixedFreq, fixedDCC) swaps.append(swap) libor_curve = FinIborSingleCurve(curveDate, depos, fras, swaps) cdsContracts = [] for i in range(1, 11): maturityDate = curveDate.addMonths(12 * i) cds = FinCDS(curveDate, maturityDate, 0.005 + 0.001 * (i - 1)) cdsContracts.append(cds) issuerCurve = FinCDSCurve(curveDate, cdsContracts, libor_curve, recoveryRate=0.40, useCache=False) testCases.header("T", "Q") n = len(issuerCurve._times) for i in range(0, n): testCases.print(issuerCurve._times[i], issuerCurve._values[i]) testCases.header("CONTRACT", "VALUE") for i in range(1, 11): maturityDate = curveDate.addMonths(12 * i) cds = FinCDS(curveDate, maturityDate, 0.005 + 0.001 * (i - 1)) v = cds.value(curveDate, issuerCurve) testCases.print(i, v) if 1 == 0: x = [0.0, 1.2, 1.6, 1.7, 10.0] qs = issuerCurve.survProb(x) print("===>", qs) x = [0.3, 1.2, 1.6, 1.7, 10.0] xx = np.array(x) qs = issuerCurve.survProb(xx) print("===>", qs) x = [0.3, 1.2, 1.6, 1.7, 10.0] dfs = issuerCurve.df(x) print("===>", dfs) x = [0.3, 1.2, 1.6, 1.7, 10.0] xx = np.array(x) dfs = issuerCurve.df(xx) print("===>", dfs)
def testFinIborCashSettledSwaption(): testCases.header("LABEL", "VALUE") valuationDate = FinDate(1, 1, 2020) settlementDate = FinDate(1, 1, 2020) depoDCCType = FinDayCountTypes.THIRTY_E_360_ISDA depos = [] depo = FinIborDeposit(settlementDate, "1W", 0.0023, depoDCCType) depos.append(depo) depo = FinIborDeposit(settlementDate, "1M", 0.0023, depoDCCType) depos.append(depo) depo = FinIborDeposit(settlementDate, "3M", 0.0023, depoDCCType) depos.append(depo) depo = FinIborDeposit(settlementDate, "6M", 0.0023, depoDCCType) depos.append(depo) # No convexity correction provided so I omit interest rate futures settlementDate = FinDate(2, 1, 2020) swaps = [] accType = FinDayCountTypes.ACT_365F fixedFreqType = FinFrequencyTypes.SEMI_ANNUAL fixedLegType = FinSwapTypes.PAY swap = FinIborSwap(settlementDate, "3Y", fixedLegType, 0.00790, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "4Y", fixedLegType, 0.01200, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "5Y", fixedLegType, 0.01570, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "6Y", fixedLegType, 0.01865, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "7Y", fixedLegType, 0.02160, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "8Y", fixedLegType, 0.02350, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "9Y", fixedLegType, 0.02540, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "10Y", fixedLegType, 0.0273, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "15Y", fixedLegType, 0.0297, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "20Y", fixedLegType, 0.0316, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "25Y", fixedLegType, 0.0335, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "30Y", fixedLegType, 0.0354, fixedFreqType, accType) swaps.append(swap) liborCurve = FinIborSingleCurve(valuationDate, depos, [], swaps, FinInterpTypes.LINEAR_ZERO_RATES) exerciseDate = settlementDate.addTenor("5Y") swapMaturityDate = exerciseDate.addTenor("5Y") swapFixedCoupon = 0.040852 swapFixedFrequencyType = FinFrequencyTypes.SEMI_ANNUAL swapFixedDayCountType = FinDayCountTypes.THIRTY_E_360_ISDA swapFloatFrequencyType = FinFrequencyTypes.QUARTERLY swapFloatDayCountType = FinDayCountTypes.ACT_360 swapNotional = 1000000 fixedLegType = FinSwapTypes.PAY swaption = FinIborSwaption(settlementDate, exerciseDate, swapMaturityDate, fixedLegType, swapFixedCoupon, swapFixedFrequencyType, swapFixedDayCountType, swapNotional, swapFloatFrequencyType, swapFloatDayCountType) model = FinModelBlack(0.1533) v = swaption.value(settlementDate, liborCurve, model) testCases.print("Swaption No-Arb Value:", v) fwdSwapRate1 = liborCurve.swapRate(exerciseDate, swapMaturityDate, swapFixedFrequencyType, swapFixedDayCountType) testCases.print("Curve Fwd Swap Rate:", fwdSwapRate1) fwdSwap = FinIborSwap(exerciseDate, swapMaturityDate, fixedLegType, swapFixedCoupon, swapFixedFrequencyType, swapFixedDayCountType) fwdSwapRate2 = fwdSwap.swapRate(settlementDate, liborCurve) testCases.print("Fwd Swap Swap Rate:", fwdSwapRate2) model = FinModelBlack(0.1533) v = swaption.cashSettledValue(valuationDate, liborCurve, fwdSwapRate2, model) testCases.print("Swaption Cash Settled Value:", v)
def test_FinIborSwaptionQLExample(): valuationDate = FinDate(4, 3, 2014) settlementDate = FinDate(4, 3, 2014) depoDCCType = FinDayCountTypes.THIRTY_E_360_ISDA depos = [] depo = FinIborDeposit(settlementDate, "1W", 0.0023, depoDCCType) depos.append(depo) depo = FinIborDeposit(settlementDate, "1M", 0.0023, depoDCCType) depos.append(depo) depo = FinIborDeposit(settlementDate, "3M", 0.0023, depoDCCType) depos.append(depo) depo = FinIborDeposit(settlementDate, "6M", 0.0023, depoDCCType) depos.append(depo) # No convexity correction provided so I omit interest rate futures swaps = [] accType = FinDayCountTypes.ACT_365F fixedFreqType = FinFrequencyTypes.SEMI_ANNUAL fixedLegType = FinSwapTypes.PAY swap = FinIborSwap(settlementDate, "3Y", fixedLegType, 0.00790, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "4Y", fixedLegType, 0.01200, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "5Y", fixedLegType, 0.01570, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "6Y", fixedLegType, 0.01865, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "7Y", fixedLegType, 0.02160, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "8Y", fixedLegType, 0.02350, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "9Y", fixedLegType, 0.02540, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "10Y", fixedLegType, 0.0273, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "15Y", fixedLegType, 0.0297, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "20Y", fixedLegType, 0.0316, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "25Y", fixedLegType, 0.0335, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "30Y", fixedLegType, 0.0354, fixedFreqType, accType) swaps.append(swap) liborCurve = FinIborSingleCurve(valuationDate, depos, [], swaps, FinInterpTypes.LINEAR_ZERO_RATES) exerciseDate = settlementDate.addTenor("5Y") swapMaturityDate = exerciseDate.addTenor("5Y") swapFixedCoupon = 0.040852 swapFixedFrequencyType = FinFrequencyTypes.SEMI_ANNUAL swapFixedDayCountType = FinDayCountTypes.THIRTY_E_360_ISDA swapFloatFrequencyType = FinFrequencyTypes.QUARTERLY swapFloatDayCountType = FinDayCountTypes.ACT_360 swapNotional = 1000000 swaptionType = FinSwapTypes.PAY swaption = FinIborSwaption(settlementDate, exerciseDate, swapMaturityDate, swaptionType, swapFixedCoupon, swapFixedFrequencyType, swapFixedDayCountType, swapNotional, swapFloatFrequencyType, swapFloatDayCountType) testCases.header("MODEL", "VALUE") model = FinModelBlack(0.1533) v = swaption.value(settlementDate, liborCurve, model) testCases.print(model.__class__, v) model = FinModelBlackShifted(0.1533, -0.008) v = swaption.value(settlementDate, liborCurve, model) testCases.print(model.__class__, v) model = FinModelSABR(0.132, 0.5, 0.5, 0.5) v = swaption.value(settlementDate, liborCurve, model) testCases.print(model.__class__, v) model = FinModelSABRShifted(0.352, 0.5, 0.15, 0.15, -0.005) v = swaption.value(settlementDate, liborCurve, model) testCases.print(model.__class__, v) model = FinModelRatesHW(0.010000000, 0.00000000001) v = swaption.value(settlementDate, liborCurve, model) testCases.print(model.__class__, v)
def test_swapValuationExample(): # Example from # https://blog.deriscope.com/index.php/en/excel-interest-rate-swap-price-dual-bootstrapping-curve vBloomberg = 388147 valuationDate = FinDate(30, 11, 2018) startDate = FinDate(27, 12, 2017) maturityDate = FinDate(27, 12, 2067) notional = 10 * ONE_MILLION fixedLegType = FinSwapTypes.RECEIVE fixedRate = 0.0150 fixedDCCType = FinDayCountTypes.THIRTY_360_BOND fixedFreqType = FinFrequencyTypes.ANNUAL floatSpread = 0.0 floatDCCType = FinDayCountTypes.ACT_360 floatFreqType = FinFrequencyTypes.SEMI_ANNUAL offMarketSwap = FinIborSwap(startDate, maturityDate, fixedLegType, fixedRate, fixedFreqType, fixedDCCType, notional, floatSpread, floatFreqType, floatDCCType) interpType = FinInterpTypes.LINEAR_ZERO_RATES depoDCCType = FinDayCountTypes.ACT_360 depos = [] ########################################################################### # MARKET ########################################################################### spotDays = 0 settlementDate = valuationDate.addWeekDays(spotDays) depo = FinIborDeposit(settlementDate, "6M", -0.2510 / 100.0, depoDCCType) depos.append(depo) fras = [] fraDCCType = FinDayCountTypes.ACT_360 fra = FinIborFRA(settlementDate.addTenor("1M"), "6M", -0.2450 / 100.0, fraDCCType) fras.append(fra) fra = FinIborFRA(settlementDate.addTenor("2M"), "6M", -0.2435 / 100.0, fraDCCType) fras.append(fra) fra = FinIborFRA(settlementDate.addTenor("3M"), "6M", -0.2400 / 100.0, fraDCCType) fras.append(fra) fra = FinIborFRA(settlementDate.addTenor("4M"), "6M", -0.2360 / 100.0, fraDCCType) fras.append(fra) fra = FinIborFRA(settlementDate.addTenor("5M"), "6M", -0.2285 / 100.0, fraDCCType) fras.append(fra) fra = FinIborFRA(settlementDate.addTenor("6M"), "6M", -0.2230 / 100.0, fraDCCType) fras.append(fra) fra = FinIborFRA(settlementDate.addTenor("7M"), "6M", -0.2110 / 100.0, fraDCCType) fras.append(fra) fra = FinIborFRA(settlementDate.addTenor("8M"), "6M", -0.1990 / 100.0, fraDCCType) fras.append(fra) fra = FinIborFRA(settlementDate.addTenor("9M"), "6M", -0.1850 / 100.0, fraDCCType) fras.append(fra) fra = FinIborFRA(settlementDate.addTenor("10M"), "6M", -0.1680 / 100.0, fraDCCType) fras.append(fra) fra = FinIborFRA(settlementDate.addTenor("11M"), "6M", -0.1510 / 100.0, fraDCCType) fras.append(fra) fra = FinIborFRA(settlementDate.addTenor("12M"), "6M", -0.1360 / 100.0, fraDCCType) fras.append(fra) swaps = [] fixedLegType = FinSwapTypes.PAY fixedDCCType = FinDayCountTypes.THIRTY_360_BOND fixedFreqType = FinFrequencyTypes.ANNUAL swap = FinIborSwap(settlementDate, "2Y", fixedLegType, -0.1525 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwap(settlementDate, "3Y", fixedLegType, -0.0185 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwap(settlementDate, "4Y", fixedLegType, 0.1315 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwap(settlementDate, "5Y", fixedLegType, 0.2745 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwap(settlementDate, "6Y", fixedLegType, 0.4135 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwap(settlementDate, "7Y", fixedLegType, 0.5439 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwap(settlementDate, "8Y", fixedLegType, 0.6652 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwap(settlementDate, "9Y", fixedLegType, 0.7784 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwap(settlementDate, "10Y", fixedLegType, 0.8799 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwap(settlementDate, "11Y", fixedLegType, 0.9715 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwap(settlementDate, "12Y", fixedLegType, 1.0517 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwap(settlementDate, "15Y", fixedLegType, 1.2369 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwap(settlementDate, "20Y", fixedLegType, 1.3965 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwap(settlementDate, "25Y", fixedLegType, 1.4472 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwap(settlementDate, "30Y", fixedLegType, 1.4585 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwap(settlementDate, "35Y", fixedLegType, 1.4595 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwap(settlementDate, "40Y", fixedLegType, 1.4535 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwap(settlementDate, "45Y", fixedLegType, 1.4410 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwap(settlementDate, "50Y", fixedLegType, 1.4335 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) iborDepos = depos.copy() iborFras = fras.copy() iborSwaps = swaps.copy() iborCurve = FinIborSingleCurve(valuationDate, iborDepos, iborFras, iborSwaps, interpType) v1 = offMarketSwap.value(valuationDate, iborCurve, iborCurve, -0.268 / 100.0) testCases.banner("DERISCOPE EXAMPLE REPLICATION") testCases.header("LABEL", "VALUE") testCases.print("BBG VALUE", vBloomberg) testCases.print("FP ONE CURVE VALUE", v1) ############################################################################### depoDCCType = FinDayCountTypes.ACT_360 depos = [] spotDays = 0 settlementDate = valuationDate.addWeekDays(spotDays) depo = FinIborDeposit(settlementDate, "1D", -0.3490 / 100.0, depoDCCType) depos.append(depo) fras = [] swaps = [] fixedLegType = FinSwapTypes.PAY fixedDCCType = FinDayCountTypes.ACT_365F fixedFreqType = FinFrequencyTypes.ANNUAL # Standard OIS with standard annual terms swap = FinOIS(settlementDate, "2W", fixedLegType, -0.3600 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinOIS(settlementDate, "1M", fixedLegType, -0.3560 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinOIS(settlementDate, "2M", fixedLegType, -0.3570 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinOIS(settlementDate, "3M", fixedLegType, -0.3580 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinOIS(settlementDate, "4M", fixedLegType, -0.3575 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinOIS(settlementDate, "5M", fixedLegType, -0.3578 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinOIS(settlementDate, "6M", fixedLegType, -0.3580 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinOIS(settlementDate, "7M", fixedLegType, -0.3600 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinOIS(settlementDate, "8M", fixedLegType, -0.3575 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinOIS(settlementDate, "9M", fixedLegType, -0.3569 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinOIS(settlementDate, "10M", fixedLegType, -0.3553 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinOIS(settlementDate, "11M", fixedLegType, -0.3534 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinOIS(settlementDate, "12M", fixedLegType, -0.3496 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinOIS(settlementDate, "18M", fixedLegType, -0.3173 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinOIS(settlementDate, "2Y", fixedLegType, -0.2671 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinOIS(settlementDate, "30M", fixedLegType, -0.2070 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinOIS(settlementDate, "3Y", fixedLegType, -0.1410 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinOIS(settlementDate, "4Y", fixedLegType, -0.0060 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinOIS(settlementDate, "5Y", fixedLegType, 0.1285 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinOIS(settlementDate, "6Y", fixedLegType, 0.2590 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinOIS(settlementDate, "7Y", fixedLegType, 0.3830 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinOIS(settlementDate, "8Y", fixedLegType, 0.5020 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinOIS(settlementDate, "9Y", fixedLegType, 0.6140 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinOIS(settlementDate, "10Y", fixedLegType, 0.7160 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinOIS(settlementDate, "11Y", fixedLegType, 0.8070 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinOIS(settlementDate, "12Y", fixedLegType, 0.8890 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinOIS(settlementDate, "15Y", fixedLegType, 1.0790 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinOIS(settlementDate, "20Y", fixedLegType, 1.2460 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinOIS(settlementDate, "25Y", fixedLegType, 1.3055 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinOIS(settlementDate, "30Y", fixedLegType, 1.3270 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinOIS(settlementDate, "35Y", fixedLegType, 1.3315 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinOIS(settlementDate, "40Y", fixedLegType, 1.3300 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinOIS(settlementDate, "50Y", fixedLegType, 1.3270 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) oisDepos = depos.copy() oisFras = fras.copy() oisSwaps = swaps.copy() # oisCurveFF = FinOISCurve(valuationDate, oisDepos, oisFras, oisSwaps, interpType) iborDualCurve = FinIborDualCurve(valuationDate, oisCurveFF, iborDepos, iborFras, iborSwaps, interpType)
def buildFullIssuerCurve2(mktSpreadBump, irBump): # https://www.markit.com/markit.jsp?jsppage=pv.jsp # YIELD CURVE 20 August 2020 SNAP AT 1600 m = 1.0 valuationDate = FinDate(24, 8, 2020) 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.PAY, m * 0.002155 + irBump, fixedFreq, dcType) swaps.append(swap1) maturityDate = settlementDate.addMonths(36) swap2 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAY, m * 0.002305 + irBump, fixedFreq, dcType) swaps.append(swap2) maturityDate = settlementDate.addMonths(48) swap3 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAY, m * 0.002665 + irBump, fixedFreq, dcType) swaps.append(swap3) maturityDate = settlementDate.addMonths(60) swap4 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAY, m * 0.003290 + irBump, fixedFreq, dcType) swaps.append(swap4) liborCurve = FinIborSingleCurve(valuationDate, 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
def buildFullIssuerCurve1(mktSpreadBump, irBump): # https://www.markit.com/markit.jsp?jsppage=pv.jsp # YIELD CURVE 8-AUG-2019 SNAP AT 1600 tradeDate = FinDate(2019, 8, 9) valuationDate = tradeDate.addDays(1) m = 1.0 # 0.00000000000 dcType = FinDayCountTypes.ACT_360 depos = [] depo1 = FinIborDeposit(valuationDate, "1D", m * 0.0220, dcType) depos.append(depo1) spotDays = 2 settlementDate = valuationDate.addDays(spotDays) maturityDate = settlementDate.addMonths(1) depo1 = FinIborDeposit(settlementDate, maturityDate, m * 0.022009, dcType) maturityDate = settlementDate.addMonths(2) depo2 = FinIborDeposit(settlementDate, maturityDate, m * 0.022138, dcType) maturityDate = settlementDate.addMonths(3) depo3 = FinIborDeposit(settlementDate, maturityDate, m * 0.021810, dcType) maturityDate = settlementDate.addMonths(6) depo4 = FinIborDeposit(settlementDate, maturityDate, m * 0.020503, dcType) maturityDate = settlementDate.addMonths(12) depo5 = FinIborDeposit(settlementDate, maturityDate, m * 0.019930, dcType) depos.append(depo1) depos.append(depo2) depos.append(depo3) depos.append(depo4) depos.append(depo5) fras = [] swaps = [] dcType = FinDayCountTypes.THIRTY_E_360_ISDA fixedFreq = FinFrequencyTypes.SEMI_ANNUAL maturityDate = settlementDate.addMonths(24) swap1 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAY, m * 0.015910 + irBump, fixedFreq, dcType) swaps.append(swap1) maturityDate = settlementDate.addMonths(36) swap2 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAY, m * 0.014990 + irBump, fixedFreq, dcType) swaps.append(swap2) maturityDate = settlementDate.addMonths(48) swap3 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAY, m * 0.014725 + irBump, fixedFreq, dcType) swaps.append(swap3) maturityDate = settlementDate.addMonths(60) swap4 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAY, m * 0.014640 + irBump, fixedFreq, dcType) swaps.append(swap4) maturityDate = settlementDate.addMonths(72) swap5 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAY, m * 0.014800 + irBump, fixedFreq, dcType) swaps.append(swap5) maturityDate = settlementDate.addMonths(84) swap6 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAY, m * 0.014995 + irBump, fixedFreq, dcType) swaps.append(swap6) maturityDate = settlementDate.addMonths(96) swap7 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAY, m * 0.015180 + irBump, fixedFreq, dcType) swaps.append(swap7) maturityDate = settlementDate.addMonths(108) swap8 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAY, m * 0.015610 + irBump, fixedFreq, dcType) swaps.append(swap8) maturityDate = settlementDate.addMonths(120) swap9 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAY, m * 0.015880 + irBump, fixedFreq, dcType) swaps.append(swap9) maturityDate = settlementDate.addMonths(144) swap10 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAY, m * 0.016430 + irBump, fixedFreq, dcType) swaps.append(swap10) liborCurve = FinIborSingleCurve(valuationDate, depos, fras, swaps) cdsMarketContracts = [] cdsCoupon = 0.04 + mktSpreadBump maturityDate = valuationDate.nextCDSDate(6) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) maturityDate = valuationDate.nextCDSDate(12) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) maturityDate = valuationDate.nextCDSDate(24) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) maturityDate = valuationDate.nextCDSDate(36) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) maturityDate = valuationDate.nextCDSDate(48) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) maturityDate = valuationDate.nextCDSDate(60) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) maturityDate = valuationDate.nextCDSDate(84) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) maturityDate = valuationDate.nextCDSDate(120) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) maturityDate = valuationDate.nextCDSDate(180) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) recoveryRate = 0.40 issuerCurve = FinCDSCurve(valuationDate, cdsMarketContracts, liborCurve, recoveryRate) return liborCurve, issuerCurve
def test_FinBondEmbeddedOptionMATLAB(): # https://fr.mathworks.com/help/fininst/optembndbybk.html # I FIND THAT THE PRICE CONVERGES TO 102.365 WHICH IS CLOSE TO 102.382 # FOUND BY MATLAB ALTHOUGH THEY DO NOT EXAMINE THE ASYMPTOTIC PRICE # WHICH MIGHT BE A BETTER MATCH - ALSO THEY DO NOT USE A REALISTIC VOL valuationDate = FinDate(1, 1, 2007) settlementDate = valuationDate ########################################################################### fixedLegType = FinSwapTypes.PAY dcType = FinDayCountTypes.THIRTY_E_360 fixedFreq = FinFrequencyTypes.ANNUAL swap1 = FinIborSwap(settlementDate, "1Y", fixedLegType, 0.0350, fixedFreq, dcType) swap2 = FinIborSwap(settlementDate, "2Y", fixedLegType, 0.0400, fixedFreq, dcType) swap3 = FinIborSwap(settlementDate, "3Y", fixedLegType, 0.0450, fixedFreq, dcType) swaps = [swap1, swap2, swap3] discountCurve = FinIborSingleCurve(valuationDate, [], [], swaps) ########################################################################### issueDate = FinDate(1, 1, 2005) maturityDate = FinDate(1, 1, 2010) coupon = 0.0525 freqType = FinFrequencyTypes.ANNUAL accrualType = FinDayCountTypes.ACT_ACT_ICMA bond = FinBond(issueDate, maturityDate, coupon, freqType, accrualType) callDates = [] callPrices = [] putDates = [] putPrices = [] putDate = FinDate(1, 1, 2008) for _ in range(0, 24): putDates.append(putDate) putPrices.append(100) putDate = putDate.addMonths(1) testCases.header("BOND PRICE", "PRICE") v = bond.cleanPriceFromDiscountCurve(settlementDate, discountCurve) testCases.print("Bond Pure Price:", v) sigma = 0.01 # This volatility is very small for a BK process a = 0.1 puttableBond = FinBondEmbeddedOption(issueDate, maturityDate, coupon, freqType, accrualType, callDates, callPrices, putDates, putPrices) testCases.header("TIME", "NumTimeSteps", "BondWithOption", "BondPure") timeSteps = range(100, 200, 10) # 1000, 10) values = [] for numTimeSteps in timeSteps: model = FinModelRatesBK(sigma, a, numTimeSteps) start = time.time() v = puttableBond.value(settlementDate, discountCurve, model) end = time.time() period = end - start testCases.print(period, numTimeSteps, v['bondwithoption'], v['bondpure']) values.append(v['bondwithoption']) if plotGraphs: plt.figure() plt.plot(timeSteps, values)