def test_LiborSwap(): # I have tried to reproduce the example from the blog by Ioannis Rigopoulos # https://blog.deriscope.com/index.php/en/excel-interest-rate-swap-price-dual-bootstrapping-curve startDate = FinDate(2017, 12, 27) endDate = FinDate(2067, 12, 27) fixedCoupon = 0.015 fixedFreqType = FinFrequencyTypes.ANNUAL fixedDayCountType = FinDayCountTypes.THIRTY_E_360 floatSpread = 0.0 floatFreqType = FinFrequencyTypes.SEMI_ANNUAL floatDayCountType = FinDayCountTypes.ACT_360 firstFixing = -0.00268 swapCalendarType = FinCalendarTypes.WEEKEND busDayAdjustType = FinBusDayAdjustTypes.FOLLOWING dateGenRuleType = FinDateGenRuleTypes.BACKWARD swapType = FinLiborSwapTypes.RECEIVER notional = 10.0 * ONE_MILLION swap = FinLiborSwap(startDate, endDate, swapType, fixedCoupon, fixedFreqType, fixedDayCountType, notional, floatSpread, floatFreqType, floatDayCountType, swapCalendarType, busDayAdjustType, dateGenRuleType) ''' Now perform a valuation after the swap has seasoned but with the same curve being used for discounting and working out the implied future Libor rates. ''' valuationDate = FinDate(30, 11, 2018) settlementDate = valuationDate.addDays(2) liborCurve = buildLiborCurve(valuationDate) v = swap.value(settlementDate, liborCurve, liborCurve, firstFixing) v_bbg = 388147.0 testCases.header("LABEL", "VALUE") testCases.print("SWAP_VALUE USING ONE_CURVE", v) testCases.print("BLOOMBERG VALUE", v_bbg) testCases.print("DIFFERENCE VALUE", v_bbg - v)
def test_dp_example(): # http://www.derivativepricing.com/blogpage.asp?id=8 startDate = FinDate(14, 11, 2011) endDate = FinDate(14, 11, 2016) fixedFreqType = FinFrequencyTypes.SEMI_ANNUAL swapCalendarType = FinCalendarTypes.TARGET busDayAdjustType = FinBusDayAdjustTypes.MODIFIED_FOLLOWING dateGenRuleType = FinDateGenRuleTypes.BACKWARD fixedDayCountType = FinDayCountTypes.THIRTY_E_360_ISDA swapType = FinLiborSwapTypes.PAYER fixedCoupon = 0.0124 notional = ONE_MILLION swap = FinLiborSwap(startDate, endDate, swapType, fixedCoupon=fixedCoupon, fixedFreqType=fixedFreqType, fixedDayCountType=fixedDayCountType, floatFreqType=FinFrequencyTypes.SEMI_ANNUAL, floatDayCountType=FinDayCountTypes.ACT_360, notional=notional, calendarType=swapCalendarType, busDayAdjustType=busDayAdjustType, dateGenRuleType=dateGenRuleType) # swap.printFixedLegFlows() dts = [FinDate(14, 11, 2011), FinDate(14, 5, 2012), FinDate(14, 11, 2012), FinDate(14, 5, 2013), FinDate(14, 11, 2013), FinDate(14, 5, 2014), FinDate(14, 11, 2014), FinDate(14, 5, 2015), FinDate(16, 11, 2015), FinDate(16, 5, 2016), FinDate(14, 11, 2016)] dfs = [0.9999843, 0.9966889, 0.9942107, 0.9911884, 0.9880738, 0.9836490, 0.9786276, 0.9710461, 0.9621778, 0.9514315, 0.9394919] valuationDate = startDate curve = FinDiscountCurve(valuationDate, dts, np.array(dfs), FinInterpTypes.FLAT_FORWARDS) v = swap.value(valuationDate, curve, curve) # swap.printFixedLegPV() # swap.printFloatLegPV() # This is essentially zero testCases.header("LABEL", "VALUE") testCases.print("Swap Value on a Notional of $1M:", v)
def buildLiborCurve(tradeDate): valuationDate = tradeDate.addDays(1) dcType = FinDayCountTypes.ACT_360 depos = [] depos = [] fras = [] swaps = [] dcType = FinDayCountTypes.THIRTY_E_360_ISDA fixedFreq = FinFrequencyTypes.SEMI_ANNUAL settlementDate = valuationDate maturityDate = settlementDate.addMonths(12) swap1 = FinLiborSwap(settlementDate, maturityDate, 0.0502, fixedFreq, dcType) swaps.append(swap1) maturityDate = settlementDate.addMonths(24) swap2 = FinLiborSwap(settlementDate, maturityDate, 0.0502, fixedFreq, dcType) swaps.append(swap2) maturityDate = settlementDate.addMonths(36) swap3 = FinLiborSwap(settlementDate, maturityDate, 0.0501, fixedFreq, dcType) swaps.append(swap3) maturityDate = settlementDate.addMonths(48) swap4 = FinLiborSwap(settlementDate, maturityDate, 0.0502, fixedFreq, dcType) swaps.append(swap4) maturityDate = settlementDate.addMonths(60) swap5 = FinLiborSwap(settlementDate, maturityDate, 0.0501, fixedFreq, dcType) swaps.append(swap5) liborCurve = FinLiborCurve("USD_LIBOR", settlementDate, depos, fras, swaps) return liborCurve
def test_FinLiborDepositsAndSwaps(valuationDate): depoBasis = FinDayCountTypes.THIRTY_E_360_ISDA depos = [] spotDays = 0 settlementDate = valuationDate.addWorkDays(spotDays) depositRate = 0.05 depo1 = FinLiborDeposit(settlementDate, "1M", depositRate, depoBasis) depo2 = FinLiborDeposit(settlementDate, "3M", depositRate, depoBasis) depo3 = FinLiborDeposit(settlementDate, "6M", depositRate, depoBasis) depos.append(depo1) depos.append(depo2) depos.append(depo3) fras = [] swaps = [] fixedBasis = FinDayCountTypes.ACT_365F fixedFreq = FinFrequencyTypes.SEMI_ANNUAL swapType = FinLiborSwapTypes.PAYER swapRate = 0.05 swap1 = FinLiborSwap(settlementDate, "1Y", swapType, swapRate, fixedFreq, fixedBasis) swap2 = FinLiborSwap(settlementDate, "3Y", swapType, swapRate, fixedFreq, fixedBasis) swap3 = FinLiborSwap(settlementDate, "5Y", swapType, swapRate, fixedFreq, fixedBasis) swaps.append(swap1) swaps.append(swap2) swaps.append(swap3) liborCurve = FinLiborCurve(settlementDate, depos, fras, swaps) return liborCurve
def test_FinCDSCurve(): curveDate = FinDate(2018, 12, 20) swaps = [] depos = [] fras = [] fixedDCC = FinDayCountTypes.ACT_365_ISDA fixedFreq = FinFrequencyTypes.SEMI_ANNUAL fixedCoupon = 0.05 for i in range(1, 11): maturityDate = curveDate.addMonths(12 * i) swap = FinLiborSwap( curveDate, maturityDate, fixedCoupon, fixedFreq, fixedDCC) swaps.append(swap) libor_curve = FinLiborCurve("USD_LIBOR", 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)
def testFinLiborCashSettledSwaption(): testCases.header("LABEL", "VALUE") valuationDate = FinDate(1, 1, 2020) settlementDate = FinDate(1, 1, 2020) depoDCCType = FinDayCountTypes.THIRTY_E_360_ISDA depos = [] depo = FinLiborDeposit(settlementDate, "1W", 0.0023, depoDCCType) depos.append(depo) depo = FinLiborDeposit(settlementDate, "1M", 0.0023, depoDCCType) depos.append(depo) depo = FinLiborDeposit(settlementDate, "3M", 0.0023, depoDCCType) depos.append(depo) depo = FinLiborDeposit(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 swapType = FinLiborSwapTypes.PAYER swap = FinLiborSwap(settlementDate, "3Y", swapType, 0.00790, fixedFreqType, accType) swaps.append(swap) swap = FinLiborSwap(settlementDate, "4Y", swapType, 0.01200, fixedFreqType, accType) swaps.append(swap) swap = FinLiborSwap(settlementDate, "5Y", swapType, 0.01570, fixedFreqType, accType) swaps.append(swap) swap = FinLiborSwap(settlementDate, "6Y", swapType, 0.01865, fixedFreqType, accType) swaps.append(swap) swap = FinLiborSwap(settlementDate, "7Y", swapType, 0.02160, fixedFreqType, accType) swaps.append(swap) swap = FinLiborSwap(settlementDate, "8Y", swapType, 0.02350, fixedFreqType, accType) swaps.append(swap) swap = FinLiborSwap(settlementDate, "9Y", swapType, 0.02540, fixedFreqType, accType) swaps.append(swap) swap = FinLiborSwap(settlementDate, "10Y", swapType, 0.0273, fixedFreqType, accType) swaps.append(swap) swap = FinLiborSwap(settlementDate, "15Y", swapType, 0.0297, fixedFreqType, accType) swaps.append(swap) swap = FinLiborSwap(settlementDate, "20Y", swapType, 0.0316, fixedFreqType, accType) swaps.append(swap) swap = FinLiborSwap(settlementDate, "25Y", swapType, 0.0335, fixedFreqType, accType) swaps.append(swap) swap = FinLiborSwap(settlementDate, "30Y", swapType, 0.0354, fixedFreqType, accType) swaps.append(swap) liborCurve = FinLiborCurve(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 = FinLiborSwapTypes.PAYER swaption = FinLiborSwaption(settlementDate, exerciseDate, swapMaturityDate, swaptionType, swapFixedCoupon, swapFixedFrequencyType, swapFixedDayCountType, swapNotional, swapFloatFrequencyType, swapFloatDayCountType) model = FinModelBlack(0.1533) v = swaption.value(settlementDate, liborCurve, model) testCases.print("Swaption No-Arb Value:", v) fwdSwapRate = liborCurve.swapRate(valuationDate, swapMaturityDate, swapFixedFrequencyType, swapFixedDayCountType) testCases.print("Fwd Swap Rate:", fwdSwapRate) model = FinModelBlack(0.1533) v = swaption.cashSettledValue(valuationDate, liborCurve, fwdSwapRate, model) testCases.print("Swaption Cash Settled Value:", v)
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 = FinLiborDeposit(settlementDate, maturityDate, m * 0.001709, dcType) maturityDate = settlementDate.addMonths(2) depo2 = FinLiborDeposit(settlementDate, maturityDate, m * 0.002123, dcType) maturityDate = settlementDate.addMonths(3) depo3 = FinLiborDeposit(settlementDate, maturityDate, m * 0.002469, dcType) maturityDate = settlementDate.addMonths(6) depo4 = FinLiborDeposit(settlementDate, maturityDate, m * 0.003045, dcType) maturityDate = settlementDate.addMonths(12) depo5 = FinLiborDeposit(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 = FinLiborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.002155 + irBump, fixedFreq, dcType) swaps.append(swap1) maturityDate = settlementDate.addMonths(36) swap2 = FinLiborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.002305 + irBump, fixedFreq, dcType) swaps.append(swap2) maturityDate = settlementDate.addMonths(48) swap3 = FinLiborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.002665 + irBump, fixedFreq, dcType) swaps.append(swap3) maturityDate = settlementDate.addMonths(60) swap4 = FinLiborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.003290 + irBump, fixedFreq, dcType) swaps.append(swap4) liborCurve = FinLiborCurve(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
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 settlementDate = FinDate(1, 1, 2007) ########################################################################### swapType = FinLiborSwapTypes.PAYER dcType = FinDayCountTypes.THIRTY_E_360 fixedFreq = FinFrequencyTypes.ANNUAL swap1 = FinLiborSwap(settlementDate, "1Y", swapType, 0.0350, fixedFreq, dcType) swap2 = FinLiborSwap(settlementDate, "2Y", swapType, 0.0400, fixedFreq, dcType) swap3 = FinLiborSwap(settlementDate, "3Y", swapType, 0.0450, fixedFreq, dcType) swaps = [swap1, swap2, swap3] discountCurve = FinLiborCurve(settlementDate, [], [], swaps) ########################################################################### issueDate = FinDate(1, 1, 2005) maturityDate = FinDate(1, 1, 2010) coupon = 0.0525 frequencyType = FinFrequencyTypes.ANNUAL accrualType = FinDayCountTypes.ACT_ACT_ICMA bond = FinBond(issueDate, maturityDate, coupon, frequencyType, 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, frequencyType, accrualType, callDates, callPrices, putDates, putPrices) testCases.header("PERIOD", "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)
def test_FinLiborSwaptionQLExample(): # valuationDate = FinDate(28, 2, 2014) settlementDate = FinDate(4, 3, 2014) depoDCCType = FinDayCountTypes.THIRTY_E_360_ISDA depos = [] depo = FinLiborDeposit(settlementDate, "1W", 0.0023, depoDCCType) depos.append(depo) depo = FinLiborDeposit(settlementDate, "1M", 0.0023, depoDCCType) depos.append(depo) depo = FinLiborDeposit(settlementDate, "3M", 0.0023, depoDCCType) depos.append(depo) depo = FinLiborDeposit(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 swapType = FinLiborSwapTypes.PAYER swap = FinLiborSwap(settlementDate, "3Y", swapType, 0.00790, fixedFreqType, accType) swaps.append(swap) swap = FinLiborSwap(settlementDate, "4Y", swapType, 0.01200, fixedFreqType, accType) swaps.append(swap) swap = FinLiborSwap(settlementDate, "5Y", swapType, 0.01570, fixedFreqType, accType) swaps.append(swap) swap = FinLiborSwap(settlementDate, "6Y", swapType, 0.01865, fixedFreqType, accType) swaps.append(swap) swap = FinLiborSwap(settlementDate, "7Y", swapType, 0.02160, fixedFreqType, accType) swaps.append(swap) swap = FinLiborSwap(settlementDate, "8Y", swapType, 0.02350, fixedFreqType, accType) swaps.append(swap) swap = FinLiborSwap(settlementDate, "9Y", swapType, 0.02540, fixedFreqType, accType) swaps.append(swap) swap = FinLiborSwap(settlementDate, "10Y", swapType, 0.0273, fixedFreqType, accType) swaps.append(swap) swap = FinLiborSwap(settlementDate, "15Y", swapType, 0.0297, fixedFreqType, accType) swaps.append(swap) swap = FinLiborSwap(settlementDate, "20Y", swapType, 0.0316, fixedFreqType, accType) swaps.append(swap) swap = FinLiborSwap(settlementDate, "25Y", swapType, 0.0335, fixedFreqType, accType) swaps.append(swap) swap = FinLiborSwap(settlementDate, "30Y", swapType, 0.0354, fixedFreqType, accType) swaps.append(swap) liborCurve = FinLiborCurve(settlementDate, 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 = FinLiborSwapTypes.PAYER swaption = FinLiborSwaption(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_FinLiborBermudanSwaptionBKModel(): ''' Replicate examples in paper by Leif Andersen which can be found at file:///C:/Users/Dominic/Downloads/SSRN-id155208.pdf ''' valuationDate = FinDate(1, 1, 2011) settlementDate = valuationDate exerciseDate = settlementDate.addYears(1) swapMaturityDate = settlementDate.addYears(4) swapFixedCoupon = 0.060 swapFixedFrequencyType = FinFrequencyTypes.SEMI_ANNUAL swapFixedDayCountType = FinDayCountTypes.ACT_365F liborCurve = FinDiscountCurveFlat(valuationDate, 0.0625, FinFrequencyTypes.SEMI_ANNUAL) fwdPayerSwap = FinLiborSwap(exerciseDate, swapMaturityDate, FinLiborSwapTypes.PAYER, swapFixedCoupon, swapFixedFrequencyType, swapFixedDayCountType) fwdSwapValue = fwdPayerSwap.value(settlementDate, liborCurve, liborCurve) testCases.header("LABEL", "VALUE") testCases.print("FWD SWAP VALUE", fwdSwapValue) # fwdPayerSwap.printFixedLegPV() # Now we create the European swaptions swapType = FinLiborSwapTypes.PAYER europeanSwaptionPay = FinLiborSwaption(settlementDate, exerciseDate, swapMaturityDate, swapType, swapFixedCoupon, swapFixedFrequencyType, swapFixedDayCountType) swapType = FinLiborSwapTypes.RECEIVER europeanSwaptionRec = FinLiborSwaption(settlementDate, exerciseDate, swapMaturityDate, swapType, swapFixedCoupon, swapFixedFrequencyType, swapFixedDayCountType) ########################################################################### ########################################################################### ########################################################################### # BLACK'S MODEL ########################################################################### ########################################################################### ########################################################################### testCases.banner("======= ZERO VOLATILITY ========") model = FinModelBlack(0.0000001) testCases.print("Black Model", model._volatility) valuePay = europeanSwaptionPay.value(settlementDate, liborCurve, model) testCases.print("EUROPEAN BLACK PAY VALUE ZERO VOL:", valuePay) valueRec = europeanSwaptionRec.value(settlementDate, liborCurve, model) testCases.print("EUROPEAN BLACK REC VALUE ZERO VOL:", valueRec) payRec = valuePay - valueRec testCases.print("PAYER MINUS RECEIVER :", payRec) testCases.banner("======= 20%% BLACK VOLATILITY ========") model = FinModelBlack(0.20) testCases.print("Black Model", model._volatility) valuePay = europeanSwaptionPay.value(settlementDate, liborCurve, model) testCases.print("EUROPEAN BLACK PAY VALUE:", valuePay) valueRec = europeanSwaptionRec.value(settlementDate, liborCurve, model) testCases.print("EUROPEAN BLACK REC VALUE:", valueRec) payRec = valuePay - valueRec testCases.print("PAYER MINUS RECEIVER :", payRec) ########################################################################### ########################################################################### ########################################################################### # BK MODEL ########################################################################### ########################################################################### ########################################################################### testCases.banner("=======================================================") testCases.banner("=======================================================") testCases.banner("==================== BK MODEL =========================") testCases.banner("=======================================================") testCases.banner("=======================================================") testCases.banner("======= 0% VOLATILITY EUROPEAN SWAPTION BK MODEL ======") # Used BK with constant short-rate volatility sigma = 0.00001 a = 0.01 numTimeSteps = 200 model = FinModelRatesBK(sigma, a, numTimeSteps) valuePay = europeanSwaptionPay.value(valuationDate, liborCurve, model) testCases.print("EUROPEAN BK PAY VALUE:", valuePay) valueRec = europeanSwaptionRec.value(valuationDate, liborCurve, model) testCases.print("EUROPEAN BK REC VALUE:", valueRec) payRec = valuePay - valueRec testCases.print("PAYER MINUS RECEIVER :", payRec) testCases.banner("======= 20% VOLATILITY EUROPEAN SWAPTION BK MODEL ========") # Used BK with constant short-rate volatility sigma = 0.20 a = 0.01 model = FinModelRatesBK(sigma, a, numTimeSteps) testCases.banner("BK MODEL SWAPTION CLASS EUROPEAN EXERCISE") valuePay = europeanSwaptionPay.value(valuationDate, liborCurve, model) testCases.print("EUROPEAN BK PAY VALUE:", valuePay) valueRec = europeanSwaptionRec.value(valuationDate, liborCurve, model) testCases.print("EUROPEAN BK REC VALUE:", valueRec) payRec = valuePay - valueRec testCases.print("PAYER MINUS RECEIVER :", payRec) ########################################################################### # Now we create the Bermudan swaptions but only allow European exercise swapType = FinLiborSwapTypes.PAYER exerciseType = FinOptionExerciseTypes.EUROPEAN bermudanSwaptionPay = FinLiborBermudanSwaption(settlementDate, exerciseDate, swapMaturityDate, swapType, exerciseType, swapFixedCoupon, swapFixedFrequencyType, swapFixedDayCountType) swapType = FinLiborSwapTypes.RECEIVER exerciseType = FinOptionExerciseTypes.EUROPEAN bermudanSwaptionRec = FinLiborBermudanSwaption(settlementDate, exerciseDate, swapMaturityDate, swapType, exerciseType, swapFixedCoupon, swapFixedFrequencyType, swapFixedDayCountType) testCases.banner("======= 0% VOLATILITY BERMUDAN SWAPTION EUROPEAN EXERCISE BK MODEL ========") # Used BK with constant short-rate volatility sigma = 0.000001 a = 0.01 model = FinModelRatesBK(sigma, a, numTimeSteps) testCases.banner("BK MODEL BERMUDAN SWAPTION CLASS EUROPEAN EXERCISE") valuePay = bermudanSwaptionPay.value(valuationDate, liborCurve, model) testCases.print("BERMUDAN BK PAY VALUE:", valuePay) valueRec = bermudanSwaptionRec.value(valuationDate, liborCurve, model) testCases.print("BERMUDAN BK REC VALUE:", valueRec) payRec = valuePay - valueRec testCases.print("PAYER MINUS RECEIVER :", payRec) testCases.banner("======= 20% VOLATILITY BERMUDAN SWAPTION EUROPEAN EXERCISE BK MODEL ========") # Used BK with constant short-rate volatility sigma = 0.2 a = 0.01 model = FinModelRatesBK(sigma, a, numTimeSteps) testCases.banner("BK MODEL BERMUDAN SWAPTION CLASS EUROPEAN EXERCISE") valuePay = bermudanSwaptionPay.value(valuationDate, liborCurve, model) testCases.print("BERMUDAN BK PAY VALUE:", valuePay) valueRec = bermudanSwaptionRec.value(valuationDate, liborCurve, model) testCases.print("BERMUDAN BK REC VALUE:", valueRec) payRec = valuePay - valueRec testCases.print("PAYER MINUS RECEIVER :", payRec) ########################################################################### # Now we create the Bermudan swaptions but allow Bermudan exercise ########################################################################### swapType = FinLiborSwapTypes.PAYER exerciseType = FinOptionExerciseTypes.BERMUDAN bermudanSwaptionPay = FinLiborBermudanSwaption(settlementDate, exerciseDate, swapMaturityDate, swapType, exerciseType, swapFixedCoupon, swapFixedFrequencyType, swapFixedDayCountType) swapType = FinLiborSwapTypes.RECEIVER exerciseType = FinOptionExerciseTypes.BERMUDAN bermudanSwaptionRec = FinLiborBermudanSwaption(settlementDate, exerciseDate, swapMaturityDate, swapType, exerciseType, swapFixedCoupon, swapFixedFrequencyType, swapFixedDayCountType) testCases.banner("======= ZERO VOLATILITY BERMUDAN SWAPTION BERMUDAN EXERCISE BK MODEL ========") # Used BK with constant short-rate volatility sigma = 0.000001 a = 0.01 model = FinModelRatesBK(sigma, a, numTimeSteps) testCases.banner("BK MODEL BERMUDAN SWAPTION CLASS BERMUDAN EXERCISE") valuePay = bermudanSwaptionPay.value(valuationDate, liborCurve, model) testCases.print("BERMUDAN BK PAY VALUE:", valuePay) valueRec = bermudanSwaptionRec.value(valuationDate, liborCurve, model) testCases.print("BERMUDAN BK REC VALUE:", valueRec) payRec = valuePay - valueRec testCases.print("PAYER MINUS RECEIVER :", payRec) testCases.banner("======= 20% VOLATILITY BERMUDAN SWAPTION BERMUDAN EXERCISE BK MODEL ========") # Used BK with constant short-rate volatility sigma = 0.20 a = 0.01 model = FinModelRatesBK(sigma, a, numTimeSteps) testCases.banner("BK MODEL BERMUDAN SWAPTION CLASS BERMUDAN EXERCISE") valuePay = bermudanSwaptionPay.value(valuationDate, liborCurve, model) testCases.print("BERMUDAN BK PAY VALUE:", valuePay) valueRec = bermudanSwaptionRec.value(valuationDate, liborCurve, model) testCases.print("BERMUDAN BK REC VALUE:", valueRec) payRec = valuePay - valueRec testCases.print("PAYER MINUS RECEIVER :", payRec) ########################################################################### ########################################################################### ########################################################################### # BDT MODEL ########################################################################### ########################################################################### ########################################################################### testCases.banner("=======================================================") testCases.banner("=======================================================") testCases.banner("======================= BDT MODEL =====================") testCases.banner("=======================================================") testCases.banner("=======================================================") testCases.banner("====== 0% VOLATILITY EUROPEAN SWAPTION BDT MODEL ======") # Used BK with constant short-rate volatility sigma = 0.00001 numTimeSteps = 200 model = FinModelRatesBDT(sigma, numTimeSteps) valuePay = europeanSwaptionPay.value(valuationDate, liborCurve, model) testCases.print("EUROPEAN BDT PAY VALUE:", valuePay) valueRec = europeanSwaptionRec.value(valuationDate, liborCurve, model) testCases.print("EUROPEAN BDT REC VALUE:", valueRec) payRec = valuePay - valueRec testCases.print("PAYER MINUS RECEIVER :", payRec) testCases.banner("===== 20% VOLATILITY EUROPEAN SWAPTION BDT MODEL ======") # Used BK with constant short-rate volatility sigma = 0.20 a = 0.01 model = FinModelRatesBDT(sigma, numTimeSteps) testCases.banner("BDT MODEL SWAPTION CLASS EUROPEAN EXERCISE") valuePay = europeanSwaptionPay.value(valuationDate, liborCurve, model) testCases.print("EUROPEAN BDT PAY VALUE:", valuePay) valueRec = europeanSwaptionRec.value(valuationDate, liborCurve, model) testCases.print("EUROPEAN BDT REC VALUE:", valueRec) payRec = valuePay - valueRec testCases.print("PAYER MINUS RECEIVER :", payRec) ########################################################################### # Now we create the Bermudan swaptions but only allow European exercise swapType = FinLiborSwapTypes.PAYER exerciseType = FinOptionExerciseTypes.EUROPEAN bermudanSwaptionPay = FinLiborBermudanSwaption(settlementDate, exerciseDate, swapMaturityDate, swapType, exerciseType, swapFixedCoupon, swapFixedFrequencyType, swapFixedDayCountType) swapType = FinLiborSwapTypes.RECEIVER bermudanSwaptionRec = FinLiborBermudanSwaption(settlementDate, exerciseDate, swapMaturityDate, swapType, exerciseType, swapFixedCoupon, swapFixedFrequencyType, swapFixedDayCountType) testCases.banner("======= 0% VOLATILITY BERMUDAN SWAPTION EUROPEAN EXERCISE BDT MODEL ========") # Used BK with constant short-rate volatility sigma = 0.000001 model = FinModelRatesBDT(sigma, numTimeSteps) testCases.banner("BK MODEL BERMUDAN SWAPTION CLASS EUROPEAN EXERCISE") valuePay = bermudanSwaptionPay.value(valuationDate, liborCurve, model) testCases.print("BERMUDAN BDT PAY VALUE:", valuePay) valueRec = bermudanSwaptionRec.value(valuationDate, liborCurve, model) testCases.print("BERMUDAN BDT REC VALUE:", valueRec) payRec = valuePay - valueRec testCases.print("PAYER MINUS RECEIVER :", payRec) testCases.banner("======= 20% VOLATILITY BERMUDAN SWAPTION EUROPEAN EXERCISE BDT MODEL ========") # Used BK with constant short-rate volatility sigma = 0.2 model = FinModelRatesBDT(sigma, numTimeSteps) testCases.banner("BDT MODEL BERMUDAN SWAPTION CLASS EUROPEAN EXERCISE") valuePay = bermudanSwaptionPay.value(valuationDate, liborCurve, model) testCases.print("BERMUDAN BDT PAY VALUE:", valuePay) valueRec = bermudanSwaptionRec.value(valuationDate, liborCurve, model) testCases.print("BERMUDAN BDT REC VALUE:", valueRec) payRec = valuePay - valueRec testCases.print("PAYER MINUS RECEIVER :", payRec) ########################################################################### # Now we create the Bermudan swaptions but allow Bermudan exercise ########################################################################### swapType = FinLiborSwapTypes.PAYER exerciseType = FinOptionExerciseTypes.BERMUDAN bermudanSwaptionPay = FinLiborBermudanSwaption(settlementDate, exerciseDate, swapMaturityDate, swapType, exerciseType, swapFixedCoupon, swapFixedFrequencyType, swapFixedDayCountType) swapType = FinLiborSwapTypes.RECEIVER bermudanSwaptionRec = FinLiborBermudanSwaption(settlementDate, exerciseDate, swapMaturityDate, swapType, exerciseType, swapFixedCoupon, swapFixedFrequencyType, swapFixedDayCountType) testCases.banner("======= ZERO VOLATILITY BERMUDAN SWAPTION BERMUDAN EXERCISE BDT MODEL ========") # Used BK with constant short-rate volatility sigma = 0.000001 a = 0.01 model = FinModelRatesBDT(sigma, numTimeSteps) testCases.banner("BK MODEL BERMUDAN SWAPTION CLASS BERMUDAN EXERCISE") valuePay = bermudanSwaptionPay.value(valuationDate, liborCurve, model) testCases.print("BERMUDAN BDT PAY VALUE:", valuePay) valueRec = bermudanSwaptionRec.value(valuationDate, liborCurve, model) testCases.print("BERMUDAN BDT REC VALUE:", valueRec) payRec = valuePay - valueRec testCases.print("PAYER MINUS RECEIVER :", payRec) testCases.banner("======= 20% VOLATILITY BERMUDAN SWAPTION BERMUDAN EXERCISE BDT MODEL ========") # Used BK with constant short-rate volatility sigma = 0.20 a = 0.01 model = FinModelRatesBDT(sigma, numTimeSteps) print("BDT MODEL BERMUDAN SWAPTION CLASS BERMUDAN EXERCISE") valuePay = bermudanSwaptionPay.value(valuationDate, liborCurve, model) testCases.print("BERMUDAN BDT PAY VALUE:", valuePay) valueRec = bermudanSwaptionRec.value(valuationDate, liborCurve, model) testCases.print("BERMUDAN BDT REC VALUE:", valueRec) payRec = valuePay - valueRec testCases.print("PAYER MINUS RECEIVER :", payRec) ########################################################################### ########################################################################### ########################################################################### # BDT MODEL ########################################################################### ########################################################################### ########################################################################### testCases.banner("=======================================================") testCases.banner("=======================================================") testCases.banner("======================= HW MODEL ======================") testCases.banner("=======================================================") testCases.banner("=======================================================") testCases.banner("====== 0% VOLATILITY EUROPEAN SWAPTION HW MODEL ======") sigma = 0.0000001 a = 0.1 numTimeSteps = 200 model = FinModelRatesHW(sigma, a, numTimeSteps) valuePay = europeanSwaptionPay.value(valuationDate, liborCurve, model) testCases.print("EUROPEAN HW PAY VALUE:", valuePay) valueRec = europeanSwaptionRec.value(valuationDate, liborCurve, model) testCases.print("EUROPEAN HW REC VALUE:", valueRec) payRec = valuePay - valueRec testCases.print("PAYER MINUS RECEIVER :", payRec) testCases.banner("===== 20% VOLATILITY EUROPEAN SWAPTION BDT MODEL ======") # Used BK with constant short-rate volatility sigma = 0.01 a = 0.01 model = FinModelRatesHW(sigma, a, numTimeSteps) testCases.banner("HW MODEL SWAPTION CLASS EUROPEAN EXERCISE") valuePay = europeanSwaptionPay.value(valuationDate, liborCurve, model) testCases.print("EUROPEAN HW PAY VALUE:", valuePay) valueRec = europeanSwaptionRec.value(valuationDate, liborCurve, model) testCases.print("EUROPEAN HW REC VALUE:", valueRec) payRec = valuePay - valueRec testCases.print("PAYER MINUS RECEIVER :", payRec) ########################################################################### # Now we create the Bermudan swaptions but only allow European exercise swapType = FinLiborSwapTypes.PAYER exerciseType = FinOptionExerciseTypes.EUROPEAN bermudanSwaptionPay = FinLiborBermudanSwaption(settlementDate, exerciseDate, swapMaturityDate, swapType, exerciseType, swapFixedCoupon, swapFixedFrequencyType, swapFixedDayCountType) swapType = FinLiborSwapTypes.RECEIVER bermudanSwaptionRec = FinLiborBermudanSwaption(settlementDate, exerciseDate, swapMaturityDate, swapType, exerciseType, swapFixedCoupon, swapFixedFrequencyType, swapFixedDayCountType) testCases.banner("======= 0% VOLATILITY BERMUDAN SWAPTION EUROPEAN EXERCISE HW MODEL ========") sigma = 0.000001 model = FinModelRatesHW(sigma, a, numTimeSteps) testCases.banner("BK MODEL BERMUDAN SWAPTION CLASS EUROPEAN EXERCISE") valuePay = bermudanSwaptionPay.value(valuationDate, liborCurve, model) testCases.print("BERMUDAN BDT PAY VALUE:", valuePay) valueRec = bermudanSwaptionRec.value(valuationDate, liborCurve, model) testCases.print("BERMUDAN BDT REC VALUE:", valueRec) payRec = valuePay - valueRec testCases.print("PAYER MINUS RECEIVER :", payRec) testCases.banner("======= 100bp VOLATILITY BERMUDAN SWAPTION EUROPEAN EXERCISE HW MODEL ========") # Used BK with constant short-rate volatility sigma = 0.01 model = FinModelRatesHW(sigma, a, numTimeSteps) testCases.banner("BDT MODEL BERMUDAN SWAPTION CLASS EUROPEAN EXERCISE") valuePay = bermudanSwaptionPay.value(valuationDate, liborCurve, model) testCases.print("BERMUDAN BDT PAY VALUE:", valuePay) valueRec = bermudanSwaptionRec.value(valuationDate, liborCurve, model) testCases.print("BERMUDAN BDT REC VALUE:", valueRec) payRec = valuePay - valueRec testCases.print("PAYER MINUS RECEIVER :", payRec) ########################################################################### # Now we create the Bermudan swaptions but allow Bermudan exercise ########################################################################### swapType = FinLiborSwapTypes.PAYER exerciseType = FinOptionExerciseTypes.BERMUDAN bermudanSwaptionPay = FinLiborBermudanSwaption(settlementDate, exerciseDate, swapMaturityDate, swapType, exerciseType, swapFixedCoupon, swapFixedFrequencyType, swapFixedDayCountType) swapType = FinLiborSwapTypes.RECEIVER bermudanSwaptionRec = FinLiborBermudanSwaption(settlementDate, exerciseDate, swapMaturityDate, swapType, exerciseType, swapFixedCoupon, swapFixedFrequencyType, swapFixedDayCountType) testCases.banner("======= ZERO VOLATILITY BERMUDAN SWAPTION BERMUDAN EXERCISE HW MODEL ========") # Used BK with constant short-rate volatility sigma = 0.000001 a = 0.01 model = FinModelRatesHW(sigma, a, numTimeSteps) testCases.banner("HW MODEL BERMUDAN SWAPTION CLASS BERMUDAN EXERCISE") valuePay = bermudanSwaptionPay.value(valuationDate, liborCurve, model) testCases.print("BERMUDAN HW PAY VALUE:", valuePay) valueRec = bermudanSwaptionRec.value(valuationDate, liborCurve, model) testCases.print("BERMUDAN HW REC VALUE:", valueRec) payRec = valuePay - valueRec testCases.print("PAYER MINUS RECEIVER :", payRec) testCases.banner("======= 100bps VOLATILITY BERMUDAN SWAPTION BERMUDAN EXERCISE HW MODEL ========") # Used BK with constant short-rate volatility sigma = 0.01 a = 0.01 model = FinModelRatesHW(sigma, a, numTimeSteps) testCases.banner("HW MODEL BERMUDAN SWAPTION CLASS BERMUDAN EXERCISE") valuePay = bermudanSwaptionPay.value(valuationDate, liborCurve, model) testCases.print("BERMUDAN HW PAY VALUE:", valuePay) valueRec = bermudanSwaptionRec.value(valuationDate, liborCurve, model) testCases.print("BERMUDAN HW REC VALUE:", valueRec) payRec = valuePay - valueRec testCases.print("PAYER MINUS RECEIVER :", payRec)
def test_FinCDSCurve(): curveDate = FinDate(2018, 12, 20) 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 = FinLiborSwap(curveDate, maturityDate, FinLiborSwapTypes.PAYER, fixedCoupon, fixedFreq, fixedDCC) swaps.append(swap) libor_curve = FinLiborCurve(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 buildFullIssuerCurve(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) dcType = FinDayCountTypes.ACT_360 depos = [] m = 1.0 # 0.00000000000 spotDays = 2 settlementDate = valuationDate.addDays(spotDays) maturityDate = settlementDate.addMonths(1) depo1 = FinLiborDeposit(settlementDate, maturityDate, m * 0.022009, dcType) maturityDate = settlementDate.addMonths(2) depo2 = FinLiborDeposit(settlementDate, maturityDate, m * 0.022138, dcType) maturityDate = settlementDate.addMonths(3) depo3 = FinLiborDeposit(settlementDate, maturityDate, m * 0.021810, dcType) maturityDate = settlementDate.addMonths(6) depo4 = FinLiborDeposit(settlementDate, maturityDate, m * 0.020503, dcType) maturityDate = settlementDate.addMonths(12) depo5 = FinLiborDeposit(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 = FinLiborSwap(settlementDate, maturityDate, m * 0.015910 + irBump, fixedFreq, dcType) swaps.append(swap1) maturityDate = settlementDate.addMonths(36) swap2 = FinLiborSwap(settlementDate, maturityDate, m * 0.014990 + irBump, fixedFreq, dcType) swaps.append(swap2) maturityDate = settlementDate.addMonths(48) swap3 = FinLiborSwap(settlementDate, maturityDate, m * 0.014725 + irBump, fixedFreq, dcType) swaps.append(swap3) maturityDate = settlementDate.addMonths(60) swap4 = FinLiborSwap(settlementDate, maturityDate, m * 0.014640 + irBump, fixedFreq, dcType) swaps.append(swap4) maturityDate = settlementDate.addMonths(72) swap5 = FinLiborSwap(settlementDate, maturityDate, m * 0.014800 + irBump, fixedFreq, dcType) swaps.append(swap5) maturityDate = settlementDate.addMonths(84) swap6 = FinLiborSwap(settlementDate, maturityDate, m * 0.014995 + irBump, fixedFreq, dcType) swaps.append(swap6) maturityDate = settlementDate.addMonths(96) swap7 = FinLiborSwap(settlementDate, maturityDate, m * 0.015180 + irBump, fixedFreq, dcType) swaps.append(swap7) maturityDate = settlementDate.addMonths(108) swap8 = FinLiborSwap(settlementDate, maturityDate, m * 0.015610 + irBump, fixedFreq, dcType) swaps.append(swap8) maturityDate = settlementDate.addMonths(120) swap9 = FinLiborSwap(settlementDate, maturityDate, m * 0.015880 + irBump, fixedFreq, dcType) swaps.append(swap9) maturityDate = settlementDate.addMonths(144) swap10 = FinLiborSwap(settlementDate, maturityDate, m * 0.016430 + irBump, fixedFreq, dcType) swaps.append(swap10) liborCurve = FinLiborOneCurve("USD_LIBOR", settlementDate, 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) # for cds in cdsMarketContracts: # print("CDS Maturity Date",cds._maturityDate) recoveryRate = 0.40 issuerCurve = FinCDSCurve(valuationDate, cdsMarketContracts, liborCurve, recoveryRate) return liborCurve, issuerCurve
def buildLiborCurve(valuationDate): depoDCCType = FinDayCountTypes.THIRTY_E_360_ISDA depos = [] spotDays = 2 settlementDate = valuationDate.addWorkDays(spotDays) depositRate = 0.050 maturityDate = settlementDate.addMonths(1) depo1 = FinLiborDeposit( settlementDate, maturityDate, depositRate, depoDCCType) maturityDate = settlementDate.addMonths(3) depo2 = FinLiborDeposit( settlementDate, maturityDate, depositRate, depoDCCType) maturityDate = settlementDate.addMonths(6) depo3 = FinLiborDeposit( settlementDate, maturityDate, depositRate, depoDCCType) maturityDate = settlementDate.addMonths(9) depo4 = FinLiborDeposit( settlementDate, maturityDate, depositRate, depoDCCType) maturityDate = settlementDate.addMonths(12) depo5 = FinLiborDeposit( settlementDate, maturityDate, depositRate, depoDCCType) depos.append(depo1) depos.append(depo2) depos.append(depo3) depos.append(depo4) depos.append(depo5) fras = [] fixedDCCType = FinDayCountTypes.ACT_365_ISDA fixedFreqType = FinFrequencyTypes.SEMI_ANNUAL swaps = [] swapRate = 0.05 maturityDate = settlementDate.addMonths(24) swap1 = FinLiborSwap( settlementDate, maturityDate, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap1) maturityDate = settlementDate.addMonths(36) swap2 = FinLiborSwap( settlementDate, maturityDate, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap2) maturityDate = settlementDate.addMonths(48) swap3 = FinLiborSwap( settlementDate, maturityDate, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap3) maturityDate = settlementDate.addMonths(60) swap4 = FinLiborSwap( settlementDate, maturityDate, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap4) maturityDate = settlementDate.addMonths(72) swap5 = FinLiborSwap( settlementDate, maturityDate, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap5) maturityDate = settlementDate.addMonths(84) swap6 = FinLiborSwap( settlementDate, maturityDate, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap6) maturityDate = settlementDate.addMonths(96) swap7 = FinLiborSwap( settlementDate, maturityDate, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap7) maturityDate = settlementDate.addMonths(108) swap8 = FinLiborSwap( settlementDate, maturityDate, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap8) maturityDate = settlementDate.addMonths(120) swap9 = FinLiborSwap( settlementDate, maturityDate, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap9) liborCurve = FinLiborCurve("USD_LIBOR", 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 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 = FinLiborDeposit(settlementDate, maturityDate, m * 0.0016, dcType) maturityDate = settlementDate.addMonths(2) depo2 = FinLiborDeposit(settlementDate, maturityDate, m * 0.0020, dcType) maturityDate = settlementDate.addMonths(3) depo3 = FinLiborDeposit(settlementDate, maturityDate, m * 0.0024, dcType) maturityDate = settlementDate.addMonths(6) depo4 = FinLiborDeposit(settlementDate, maturityDate, m * 0.0033, dcType) maturityDate = settlementDate.addMonths(12) depo5 = FinLiborDeposit(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 = FinLiborSwap(settlementDate, maturityDate, FinLiborSwapTypes.PAYER, m * 0.0044 + irBump, fixedFreq, dcType) swaps.append(swap1) maturityDate = settlementDate.addMonths(36) swap2 = FinLiborSwap(settlementDate, maturityDate, FinLiborSwapTypes.PAYER, m * 0.0078 + irBump, fixedFreq, dcType) swaps.append(swap2) maturityDate = settlementDate.addMonths(48) swap3 = FinLiborSwap(settlementDate, maturityDate, FinLiborSwapTypes.PAYER, m * 0.0119 + irBump, fixedFreq, dcType) swaps.append(swap3) maturityDate = settlementDate.addMonths(60) swap4 = FinLiborSwap(settlementDate, maturityDate, FinLiborSwapTypes.PAYER, m * 0.0158 + irBump, fixedFreq, dcType) swaps.append(swap4) maturityDate = settlementDate.addMonths(72) swap5 = FinLiborSwap(settlementDate, maturityDate, FinLiborSwapTypes.PAYER, m * 0.0192 + irBump, fixedFreq, dcType) swaps.append(swap5) maturityDate = settlementDate.addMonths(84) swap6 = FinLiborSwap(settlementDate, maturityDate, FinLiborSwapTypes.PAYER, m * 0.0219 + irBump, fixedFreq, dcType) swaps.append(swap6) maturityDate = settlementDate.addMonths(96) swap7 = FinLiborSwap(settlementDate, maturityDate, FinLiborSwapTypes.PAYER, m * 0.0242 + irBump, fixedFreq, dcType) swaps.append(swap7) maturityDate = settlementDate.addMonths(108) swap8 = FinLiborSwap(settlementDate, maturityDate, FinLiborSwapTypes.PAYER, m * 0.0261 + irBump, fixedFreq, dcType) swaps.append(swap8) maturityDate = settlementDate.addMonths(120) swap9 = FinLiborSwap(settlementDate, maturityDate, FinLiborSwapTypes.PAYER, m * 0.0276 + irBump, fixedFreq, dcType) swaps.append(swap9) liborCurve = FinLiborCurve(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
from financepy.finutils import * from financepy.products.libor.FinLiborSwap import FinLiborSwap from financepy.products.libor.FinLiborCurve import FinLiborCurve s = labelToString("FRED", "WILMA") print(s) s = labelToString(2.3, 5.3) print(s) v = [0.5, 0.2, 0.8] s = labelToString("Prices", v) ############################################################################### settlementDate = FinDate(1, 1, 2007) dcType = FinDayCountTypes.ACT_360 fixedFreq = FinFrequencyTypes.SEMI_ANNUAL swap1 = FinLiborSwap(settlementDate, FinDate(1, 1, 2008), 0.05, fixedFreq, dcType) swap2 = FinLiborSwap(settlementDate, FinDate(1, 1, 2009), 0.05, fixedFreq, dcType) swap3 = FinLiborSwap(settlementDate, FinDate(1, 1, 2010), 0.05, fixedFreq, dcType) swaps = [swap1, swap2, swap3] discountCurve = FinLiborCurve(settlementDate, [], [], swaps) print(discountCurve)
def test_bloombergPricingExample(): ''' 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.addWorkDays(spotDays) depoDCCType = FinDayCountTypes.ACT_360 depos = [] depositRate = 0.0231381 maturityDate = settlementDate.addMonths(3) depo = FinLiborDeposit(settlementDate, maturityDate, depositRate, depoDCCType) depos.append(depo) futs = [] fut = FinLiborFuture(valuationDate, 1) futs.append(fut) fut = FinLiborFuture(valuationDate, 2) futs.append(fut) fut = FinLiborFuture(valuationDate, 3) futs.append(fut) fut = FinLiborFuture(valuationDate, 4) futs.append(fut) fut = FinLiborFuture(valuationDate, 5) futs.append(fut) fut = FinLiborFuture(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.addWorkDays(spotDays) notional = ONE_MILLION swapType = FinLiborSwapTypes.PAYER swaps = [] swap = FinLiborSwap(settlementDate, "2Y", swapType, (2.77417 + 2.77844) / 200, freq, accrual, notional) swaps.append(swap) swap = FinLiborSwap(settlementDate, "3Y", swapType, (2.86098 + 2.86582) / 200, freq, accrual) swaps.append(swap) swap = FinLiborSwap(settlementDate, "4Y", swapType, (2.90240 + 2.90620) / 200, freq, accrual) swaps.append(swap) swap = FinLiborSwap(settlementDate, "5Y", swapType, (2.92944 + 2.92906) / 200, freq, accrual) swaps.append(swap) swap = FinLiborSwap(settlementDate, "6Y", swapType, (2.94001 + 2.94499) / 200, freq, accrual) swaps.append(swap) swap = FinLiborSwap(settlementDate, "7Y", swapType, (2.95352 + 2.95998) / 200, freq, accrual) swaps.append(swap) swap = FinLiborSwap(settlementDate, "8Y", swapType, (2.96830 + 2.97400) / 200, freq, accrual) swaps.append(swap) swap = FinLiborSwap(settlementDate, "9Y", swapType, (2.98403 + 2.98817) / 200, freq, accrual) swaps.append(swap) swap = FinLiborSwap(settlementDate, "10Y", swapType, (2.99716 + 3.00394) / 200, freq, accrual) swaps.append(swap) swap = FinLiborSwap(settlementDate, "11Y", swapType, (3.01344 + 3.01596) / 200, freq, accrual) swaps.append(swap) swap = FinLiborSwap(settlementDate, "12Y", swapType, (3.02276 + 3.02684) / 200, freq, accrual) swaps.append(swap) swap = FinLiborSwap(settlementDate, "15Y", swapType, (3.04092 + 3.04508) / 200, freq, accrual) swaps.append(swap) swap = FinLiborSwap(settlementDate, "20Y", swapType, (3.04417 + 3.05183) / 200, freq, accrual) swaps.append(swap) swap = FinLiborSwap(settlementDate, "25Y", swapType, (3.03219 + 3.03621) / 200, freq, accrual) swaps.append(swap) swap = FinLiborSwap(settlementDate, "30Y", swapType, (3.01030 + 3.01370) / 200, freq, accrual) swaps.append(swap) swap = FinLiborSwap(settlementDate, "40Y", swapType, (2.96946 + 2.97354) / 200, freq, accrual) swaps.append(swap) swap = FinLiborSwap(settlementDate, "50Y", swapType, (2.91552 + 2.93748) / 200, freq, accrual) swaps.append(swap) liborCurve = FinLiborCurve(valuationDate, depos, fras, swaps) # The valuation of 53714.55 is very close to the spreadsheet value 53713.96 principal = 0.0 testCases.header("VALUATION TO TODAY DATE", " PV") testCases.print( "VALUE:", swaps[0].value(valuationDate, liborCurve, liborCurve, None, principal)) testCases.print( "FIXED:", swaps[0].fixedLegValue(valuationDate, liborCurve, principal)) testCases.print( "FLOAT:", swaps[0].floatLegValue(valuationDate, liborCurve, liborCurve, None, principal)) testCases.header("VALUATION TO SWAP SETTLEMENT DATE", " PV") testCases.print( "VALUE:", swaps[0].value(settlementDate, liborCurve, liborCurve, None, principal)) testCases.print( "FIXED:", swaps[0].fixedLegValue(settlementDate, liborCurve, principal)) testCases.print( "FLOAT:", swaps[0].floatLegValue(settlementDate, liborCurve, liborCurve, None, principal))
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.addWorkDays(spotDays) depositRate = 0.001410 depo = FinLiborDeposit(settlementDate, "ON", depositRate, dccType) depos.append(depo) spotDays = 1 settlementDate = valuationDate.addWorkDays(spotDays) depositRate = 0.001410 depo = FinLiborDeposit(settlementDate, "TN", depositRate, dccType) depos.append(depo) spotDays = 2 settlementDate = valuationDate.addWorkDays(spotDays) depositRate = 0.001910 depo = FinLiborDeposit(settlementDate, "1W", depositRate, dccType) depos.append(depo) depositRate = 0.002090 depo = FinLiborDeposit(settlementDate, "2W", depositRate, dccType) depos.append(depo) depositRate = 0.002490 depo = FinLiborDeposit(settlementDate, "1M", depositRate, dccType) depos.append(depo) depositRate = 0.003450 depo = FinLiborDeposit(settlementDate, "2M", depositRate, dccType) depos.append(depo) depositRate = 0.004570 depo = FinLiborDeposit(settlementDate, "3M", depositRate, dccType) depos.append(depo) depositRate = 0.005230 depo = FinLiborDeposit(settlementDate, "4M", depositRate, dccType) depos.append(depo) depositRate = 0.005860 depo = FinLiborDeposit(settlementDate, "5M", depositRate, dccType) depos.append(depo) depositRate = 0.006540 depo = FinLiborDeposit(settlementDate, "6M", depositRate, dccType) depos.append(depo) depositRate = 0.007080 depo = FinLiborDeposit(settlementDate, "7M", depositRate, dccType) depos.append(depo) depositRate = 0.007540 depo = FinLiborDeposit(settlementDate, "8M", depositRate, dccType) depos.append(depo) depositRate = 0.008080 depo = FinLiborDeposit(settlementDate, "9M", depositRate, dccType) depos.append(depo) depositRate = 0.008570 depo = FinLiborDeposit(settlementDate, "10M", depositRate, dccType) depos.append(depo) depositRate = 0.009130 depo = FinLiborDeposit(settlementDate, "11M", depositRate, dccType) depos.append(depo) fras = [] swaps = [] dayCountType = FinDayCountTypes.THIRTY_E_360_ISDA # dayCountType = FinDayCountTypes.ACT_360 freqType = FinFrequencyTypes.SEMI_ANNUAL swapType = FinLiborSwapTypes.PAYER swapRate = 0.0058 swap = FinLiborSwap(settlementDate, "1Y", swapType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0060 swap = FinLiborSwap(settlementDate, "2Y", swapType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0072 swap = FinLiborSwap(settlementDate, "3Y", swapType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0096 swap = FinLiborSwap(settlementDate, "4Y", swapType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0124 swap = FinLiborSwap(settlementDate, "5Y", swapType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0173 swap = FinLiborSwap(settlementDate, "7Y", swapType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0219 swap = FinLiborSwap(settlementDate, "10Y", swapType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0283 swap = FinLiborSwap(settlementDate, "30Y", swapType, swapRate, freqType, dayCountType) swaps.append(swap) numRepeats = 10 start = time.time() for _ in range(0, numRepeats): _ = FinLiborCurve(valuationDate, depos, fras, swaps, FinInterpTypes.FLAT_FORWARDS) end = time.time() elapsed1 = end - start start = time.time() for _ in range(0, numRepeats): _ = FinLiborCurve(valuationDate, depos, fras, swaps, FinInterpTypes.LINEAR_SWAP_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_FinLiborDepositsFuturesSwaps(): spotDate = FinDate(6, 6, 2018) spotDays = 0 settlementDate = spotDate.addWorkDays(spotDays) depoDCCType = FinDayCountTypes.ACT_360 depos = [] depositRate = 0.0231381 depo = FinLiborDeposit(settlementDate, "3M", depositRate, depoDCCType) depos.append(depo) depositRate = 0.027 depo = FinLiborDeposit(settlementDate, "3M", depositRate, depoDCCType) depos.append(depo) depos = [] depo = FinLiborDeposit(settlementDate, "1M", 0.0230, depoDCCType) depos.append(depo) depo = FinLiborDeposit(settlementDate, "2M", 0.0235, depoDCCType) depos.append(depo) depo = FinLiborDeposit(settlementDate, "3M", 0.0240, depoDCCType) depos.append(depo) fras = [] fraRate = futureToFRARate(97.6675, -0.00005) fraSettlementDate = spotDate.nextIMMDate() fraMaturityDate = fraSettlementDate.nextIMMDate() fra = FinLiborFRA(fraSettlementDate, fraMaturityDate, fraRate, depoDCCType) fras.append(fra) fraRate = futureToFRARate(97.5200, -0.00060) fraSettlementDate = fraMaturityDate fraMaturityDate = fraSettlementDate.nextIMMDate() fra = FinLiborFRA(fraSettlementDate, fraMaturityDate, fraRate, depoDCCType) fras.append(fra) fraRate = futureToFRARate(97.3550, -0.00146) fraSettlementDate = fraMaturityDate fraMaturityDate = fraSettlementDate.nextIMMDate() fra = FinLiborFRA(fraSettlementDate, fraMaturityDate, fraRate, depoDCCType) fras.append(fra) fraRate = futureToFRARate(97.2450, -0.00263) fraSettlementDate = fraMaturityDate fraMaturityDate = fraSettlementDate.nextIMMDate() fra = FinLiborFRA(fraSettlementDate, fraMaturityDate, fraRate, depoDCCType) fras.append(fra) fraRate = futureToFRARate(97.1450, -0.00411) fraSettlementDate = fraMaturityDate fraMaturityDate = fraSettlementDate.nextIMMDate() fra = FinLiborFRA(fraSettlementDate, fraMaturityDate, fraRate, depoDCCType) fras.append(fra) fraRate = futureToFRARate(97.0750, -0.00589) fraSettlementDate = fraSettlementDate.nextIMMDate() fraMaturityDate = fraSettlementDate.nextIMMDate() fra = FinLiborFRA(fraSettlementDate, fraMaturityDate, fraRate, depoDCCType) fras.append(fra) ########################################################################### spotDays = 2 startDate = spotDate.addWorkDays(spotDays) swaps = [] swapType = FinLiborSwapTypes.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 = FinLiborSwap(startDate, "2Y", swapType, swapRate, fixedFreqType, fixedDCCType, notional, floatSpread, floatFreqType, floatDCCType, calendarType, busDayAdjustRule) swaps.append(swap) liborCurve = FinLiborCurve(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_FinLiborDepositsFRAsSwaps(): valuationDate = FinDate(2019, 9, 18) dccType = FinDayCountTypes.THIRTY_E_360_ISDA depos = [] spotDays = 0 settlementDate = valuationDate.addWorkDays(spotDays) depositRate = 0.050 maturityDate = settlementDate.addMonths(1) depo = FinLiborDeposit(settlementDate, maturityDate, depositRate, dccType) depos.append(depo) maturityDate = settlementDate.addMonths(2) depo = FinLiborDeposit(settlementDate, maturityDate, depositRate, dccType) depos.append(depo) maturityDate = settlementDate.addMonths(3) depo = FinLiborDeposit(settlementDate, maturityDate, depositRate, dccType) depos.append(depo) maturityDate = settlementDate.addMonths(6) depo = FinLiborDeposit(settlementDate, maturityDate, depositRate, dccType) depos.append(depo) maturityDate = settlementDate.addMonths(9) depo = FinLiborDeposit(settlementDate, maturityDate, depositRate, dccType) depos.append(depo) maturityDate = settlementDate.addMonths(12) depo = FinLiborDeposit(settlementDate, maturityDate, depositRate, dccType) depos.append(depo) fras = [] # 1 x 4 FRA fraRate = 0.04 fraSettlementDate = settlementDate.addMonths(9) fraMaturityDate = settlementDate.addMonths(13) fra = FinLiborFRA(fraSettlementDate, fraMaturityDate, fraRate, dccType) fras.append(fra) # 4 x 7 FRA fraRate = 0.03 fraSettlementDate = settlementDate.addMonths(13) fraMaturityDate = settlementDate.addMonths(17) fra = FinLiborFRA(fraSettlementDate, fraMaturityDate, fraRate, dccType) fras.append(fra) # 4 x 7 FRA fraRate = 0.07 fraSettlementDate = settlementDate.addMonths(17) fraMaturityDate = settlementDate.addMonths(21) fra = FinLiborFRA(fraSettlementDate, fraMaturityDate, fraRate, dccType) fras.append(fra) swaps = [] fixedDCCType = FinDayCountTypes.ACT_365F fixedFreqType = FinFrequencyTypes.SEMI_ANNUAL swapRate = 0.05 # maturityDate = settlementDate.addMonths(24) # swap = FinLiborSwap(settlementDate, maturityDate, swapRate, fixedFreqType, # fixedDCCType) # swaps.append(swap) swapType = FinLiborSwapTypes.PAYER maturityDate = settlementDate.addMonths(36) swap = FinLiborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(48) swap = FinLiborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(60) swap = FinLiborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(72) swap = FinLiborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(84) swap = FinLiborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(96) swap = FinLiborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(108) swap = FinLiborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(120) swap = FinLiborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(132) swap = FinLiborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(144) swap = FinLiborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(180) swap = FinLiborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(240) swap = FinLiborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(300) swap = FinLiborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(360) swap = FinLiborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) liborCurve = FinLiborCurve(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_FinLiborDepositsAndSwaps(valuationDate): depoBasis = FinDayCountTypes.THIRTY_E_360_ISDA depos = [] spotDays = 2 settlementDate = valuationDate.addWorkDays(spotDays) depositRate = 0.030 maturityDate = settlementDate.addMonths(1) depo1 = FinLiborDeposit(settlementDate, maturityDate, depositRate, depoBasis) maturityDate = settlementDate.addMonths(2) depo2 = FinLiborDeposit(settlementDate, maturityDate, depositRate, depoBasis) maturityDate = settlementDate.addMonths(3) depo3 = FinLiborDeposit(settlementDate, maturityDate, depositRate, depoBasis) maturityDate = settlementDate.addMonths(6) depo4 = FinLiborDeposit(settlementDate, maturityDate, depositRate, depoBasis) maturityDate = settlementDate.addMonths(9) depo5 = FinLiborDeposit(settlementDate, maturityDate, depositRate, depoBasis) depos.append(depo1) depos.append(depo2) depos.append(depo3) depos.append(depo4) depos.append(depo5) fras = [] swaps = [] fixedBasis = FinDayCountTypes.ACT_365_ISDA fixedFreq = FinFrequencyTypes.SEMI_ANNUAL swapRate = 0.03 maturityDate = settlementDate.addMonths(12) swap1 = FinLiborSwap(settlementDate, maturityDate, swapRate, fixedFreq, fixedBasis) swaps.append(swap1) swapRate = 0.034 maturityDate = settlementDate.addMonths(24) swap2 = FinLiborSwap(settlementDate, maturityDate, swapRate, fixedFreq, fixedBasis) swaps.append(swap2) swapRate = 0.037 maturityDate = settlementDate.addMonths(36) swap3 = FinLiborSwap(settlementDate, maturityDate, swapRate, fixedFreq, fixedBasis) swaps.append(swap3) swapRate = 0.039 maturityDate = settlementDate.addMonths(48) swap4 = FinLiborSwap(settlementDate, maturityDate, swapRate, fixedFreq, fixedBasis) swaps.append(swap4) swapRate = 0.040 maturityDate = settlementDate.addMonths(60) swap5 = FinLiborSwap(settlementDate, maturityDate, swapRate, fixedFreq, fixedBasis) swaps.append(swap5) liborCurve = FinLiborCurve("USD_LIBOR", settlementDate, depos, fras, swaps) return liborCurve
def buildLiborCurve(valuationDate): settlementDate = valuationDate.addDays(2) dcType = FinDayCountTypes.ACT_360 depos = [] fras = [] swaps = [] maturityDate = settlementDate.addMonths(6) depo1 = FinLiborDeposit(settlementDate, maturityDate, -0.00251, dcType) depos.append(depo1) # Series of 1M futures startDate = settlementDate.nextIMMDate() endDate = startDate.addMonths(1) fra = FinLiborFRA(startDate, endDate, -0.0023, True, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinLiborFRA(startDate, endDate, -0.00234, True, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinLiborFRA(startDate, endDate, -0.00225, True, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinLiborFRA(startDate, endDate, -0.00226, True, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinLiborFRA(startDate, endDate, -0.00219, True, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinLiborFRA(startDate, endDate, -0.00213, True, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinLiborFRA(startDate, endDate, -0.00186, True, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinLiborFRA(startDate, endDate, -0.00189, True, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinLiborFRA(startDate, endDate, -0.00175, True, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinLiborFRA(startDate, endDate, -0.00143, True, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinLiborFRA(startDate, endDate, -0.00126, True, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinLiborFRA(startDate, endDate, -0.00126, True, dcType) fras.append(fra) fixedFreq = FinFrequencyTypes.ANNUAL dcType = FinDayCountTypes.THIRTY_360 maturityDate = settlementDate.addMonths(24) swap1 = FinLiborSwap(settlementDate, maturityDate, -0.001506, fixedFreq, dcType) swaps.append(swap1) maturityDate = settlementDate.addMonths(36) swap2 = FinLiborSwap(settlementDate, maturityDate, -0.000185, fixedFreq, dcType) swaps.append(swap2) maturityDate = settlementDate.addMonths(48) swap3 = FinLiborSwap(settlementDate, maturityDate, 0.001358, fixedFreq, dcType) swaps.append(swap3) maturityDate = settlementDate.addMonths(60) swap4 = FinLiborSwap(settlementDate, maturityDate, 0.0027652, fixedFreq, dcType) swaps.append(swap4) maturityDate = settlementDate.addMonths(72) swap5 = FinLiborSwap(settlementDate, maturityDate, 0.0041539, fixedFreq, dcType) swaps.append(swap5) maturityDate = settlementDate.addMonths(84) swap6 = FinLiborSwap(settlementDate, maturityDate, 0.0054604, fixedFreq, dcType) swaps.append(swap6) maturityDate = settlementDate.addMonths(96) swap7 = FinLiborSwap(settlementDate, maturityDate, 0.006674, fixedFreq, dcType) swaps.append(swap7) maturityDate = settlementDate.addMonths(108) swap8 = FinLiborSwap(settlementDate, maturityDate, 0.007826, fixedFreq, dcType) swaps.append(swap8) maturityDate = settlementDate.addMonths(120) swap9 = FinLiborSwap(settlementDate, maturityDate, 0.008821, fixedFreq, dcType) swaps.append(swap9) maturityDate = settlementDate.addMonths(132) swap10 = FinLiborSwap(settlementDate, maturityDate, 0.0097379, fixedFreq, dcType) swaps.append(swap10) maturityDate = settlementDate.addMonths(144) swap11 = FinLiborSwap(settlementDate, maturityDate, 0.0105406, fixedFreq, dcType) swaps.append(swap11) maturityDate = settlementDate.addMonths(180) swap12 = FinLiborSwap(settlementDate, maturityDate, 0.0123927, fixedFreq, dcType) swaps.append(swap12) maturityDate = settlementDate.addMonths(240) swap13 = FinLiborSwap(settlementDate, maturityDate, 0.0139882, fixedFreq, dcType) swaps.append(swap13) maturityDate = settlementDate.addMonths(300) swap14 = FinLiborSwap(settlementDate, maturityDate, 0.0144972, fixedFreq, dcType) swaps.append(swap14) maturityDate = settlementDate.addMonths(360) swap15 = FinLiborSwap(settlementDate, maturityDate, 0.0146081, fixedFreq, dcType) swaps.append(swap15) maturityDate = settlementDate.addMonths(420) swap16 = FinLiborSwap(settlementDate, maturityDate, 0.01461897, fixedFreq, dcType) swaps.append(swap16) maturityDate = settlementDate.addMonths(480) swap17 = FinLiborSwap(settlementDate, maturityDate, 0.014567455, fixedFreq, dcType) swaps.append(swap17) maturityDate = settlementDate.addMonths(540) swap18 = FinLiborSwap(settlementDate, maturityDate, 0.0140826, fixedFreq, dcType) swaps.append(swap18) maturityDate = settlementDate.addMonths(600) swap19 = FinLiborSwap(settlementDate, maturityDate, 0.01436822, fixedFreq, dcType) swaps.append(swap19) liborCurve = FinLiborCurve("USD", settlementDate, 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, liborCurve, None) testCases.print("SWAP VALUE:", swap._maturityDate, v) return liborCurve
def test_FinLiborDepositsAndSwaps(): valuationDate = FinDate(2019, 9, 18) depoDCCType = FinDayCountTypes.THIRTY_E_360_ISDA depos = [] spotDays = 2 settlementDate = valuationDate.addWorkDays(spotDays) depositRate = 0.050 maturityDate = settlementDate.addMonths(1) depo = FinLiborDeposit(settlementDate, maturityDate, depositRate, depoDCCType) depos.append(depo) maturityDate = settlementDate.addMonths(2) depo = FinLiborDeposit(settlementDate, maturityDate, depositRate, depoDCCType) depos.append(depo) maturityDate = settlementDate.addMonths(3) depo = FinLiborDeposit(settlementDate, maturityDate, depositRate, depoDCCType) depos.append(depo) maturityDate = settlementDate.addMonths(6) depo = FinLiborDeposit(settlementDate, maturityDate, depositRate, depoDCCType) depos.append(depo) maturityDate = settlementDate.addMonths(9) depo = FinLiborDeposit(settlementDate, maturityDate, depositRate, depoDCCType) depos.append(depo) maturityDate = settlementDate.addMonths(12) depo = FinLiborDeposit(settlementDate, maturityDate, depositRate, depoDCCType) depos.append(depo) fras = [] swaps = [] fixedDCCType = FinDayCountTypes.ACT_365_ISDA fixedFreqType = FinFrequencyTypes.SEMI_ANNUAL swapRate = 0.05 maturityDate = settlementDate.addMonths(24) swap = FinLiborSwap(settlementDate, maturityDate, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(36) swap = FinLiborSwap(settlementDate, maturityDate, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(48) swap = FinLiborSwap(settlementDate, maturityDate, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(60) swap = FinLiborSwap(settlementDate, maturityDate, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(72) swap = FinLiborSwap(settlementDate, maturityDate, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(84) swap = FinLiborSwap(settlementDate, maturityDate, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(96) swap = FinLiborSwap(settlementDate, maturityDate, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(108) swap = FinLiborSwap(settlementDate, maturityDate, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(120) swap = FinLiborSwap(settlementDate, maturityDate, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(132) swap = FinLiborSwap(settlementDate, maturityDate, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(144) swap = FinLiborSwap(settlementDate, maturityDate, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(180) swap = FinLiborSwap(settlementDate, maturityDate, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(240) swap = FinLiborSwap(settlementDate, maturityDate, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(300) swap = FinLiborSwap(settlementDate, maturityDate, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(360) swap = FinLiborSwap(settlementDate, maturityDate, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) liborCurve = FinLiborCurve("USD_LIBOR", settlementDate, 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(deposit._maturityDate) testCases.print(str(deposit._maturityDate), df)