def test_FinFXForward(): # https://stackoverflow.com/questions/48778712 # /fx-vanilla-call-price-in-quantlib-doesnt-match-bloomberg valuationDate = FinDate(13, 2, 2018) expiryDate = valuationDate.addMonths(12) # Forward is on EURUSD which is expressed as number of USD per EUR # ccy1 = EUR and ccy2 = USD forName = "EUR" domName = "USD" currencyPair = forName + domName # Always ccy1ccy2 spotFXRate = 1.300 # USD per EUR strikeFXRate = 1.365 # USD per EUR ccy1InterestRate = 0.02 # USD Rates ccy2InterestRate = 0.05 # EUR rates ########################################################################### spotDays = 0 settlementDate = valuationDate.addWeekDays(spotDays) maturityDate = settlementDate.addMonths(12) notional = 100.0 calendarType = FinCalendarTypes.TARGET depos = [] fras = [] swaps = [] depositRate = ccy1InterestRate depo = FinIborDeposit(settlementDate, maturityDate, depositRate, FinDayCountTypes.ACT_360, notional, calendarType) depos.append(depo) forDiscountCurve = FinIborSingleCurve(valuationDate, depos, fras, swaps) depos = [] fras = [] swaps = [] depositRate = ccy2InterestRate depo = FinIborDeposit(settlementDate, maturityDate, depositRate, FinDayCountTypes.ACT_360, notional, calendarType) depos.append(depo) domDiscountCurve = FinIborSingleCurve(valuationDate, depos, fras, swaps) notional = 100.0 notionalCurrency = forName fxForward = FinFXForward(expiryDate, strikeFXRate, currencyPair, notional, notionalCurrency) testCases.header("SPOT FX", "FX FWD", "VALUE_BS") fwdValue = fxForward.value(valuationDate, spotFXRate, domDiscountCurve, forDiscountCurve) fwdFXRate = fxForward.forward(valuationDate, spotFXRate, domDiscountCurve, forDiscountCurve) testCases.print(spotFXRate, fwdFXRate, fwdValue)
def test_FinFXVanillaOptionBloombergExample(): # Example Bloomberg Pricing at # https://stackoverflow.com/questions/48778712/fx-vanilla-call-price-in-quantlib-doesnt-match-bloomberg valueDate = FinDate(13, 2, 2018) expiryDate = FinDate(15, 2, 2019) # In BS the FX rate is the price in domestic of one unit of foreign # In case of EURUSD = 1.3 the domestic currency is USD and foreign is EUR # DOM = USD , FOR = EUR forName = "EUR" domName = "USD" forDepoRate = 0.05 # EUR domDepoRate = 0.02 # USD currencyPair = forName + domName # Always FORDOM spotFXRate = 1.30 strikeFXRate = 1.3650 volatility = 0.20 spotDays = 0 settlementDate = valueDate.addWeekDays(spotDays) maturityDate = settlementDate.addMonths(12) notional = 1000000.0 notionalCurrency = "EUR" calendarType = FinCalendarTypes.TARGET depos = [] fras = [] swaps = [] depo = FinLiborDeposit(settlementDate, maturityDate, domDepoRate, FinDayCountTypes.ACT_360, notional, calendarType) depos.append(depo) domDiscountCurve = FinLiborCurve(settlementDate, depos, fras, swaps) depos = [] fras = [] swaps = [] depo = FinLiborDeposit(settlementDate, maturityDate, forDepoRate, FinDayCountTypes.ACT_360, notional, calendarType) depos.append(depo) forDiscountCurve = FinLiborCurve(settlementDate, depos, fras, swaps) model = FinFXModelBlackScholes(volatility) callOption = FinFXVanillaOption(expiryDate, strikeFXRate, currencyPair, FinOptionTypes.EUROPEAN_CALL, notional, notionalCurrency, 2) value = callOption.value(valueDate, spotFXRate, domDiscountCurve, forDiscountCurve, model) delta = callOption.delta(valueDate, spotFXRate, domDiscountCurve, forDiscountCurve, model) testCases.header("value", "delta") testCases.print(value, delta)
def test_FinIborDepositsOnly(): # I have used the following useful blog post by Ioannis Rigopoulos for this # https://blog.deriscope.com/index.php/en/yield-curve-excel-quantlib-deposit valuationDate = FinDate(2018, 2, 23) spotDays = 0 settlementDate = valuationDate.addWeekDays(spotDays) depoDCCType = FinDayCountTypes.ACT_360 notional = 100.0 calendarType = FinCalendarTypes.TARGET depos = [] # 1 month depositRate = 0.04 maturityDate = settlementDate.addMonths(1) depo = FinIborDeposit(settlementDate, maturityDate, depositRate, depoDCCType, notional, calendarType) depos.append(depo) # 2 months depositRate = 0.04 maturityDate = settlementDate.addMonths(2) depo = FinIborDeposit(settlementDate, maturityDate, depositRate, depoDCCType, notional, calendarType) depos.append(depo) # 6 months depositRate = 0.04 maturityDate = settlementDate.addMonths(6) depo = FinIborDeposit(settlementDate, maturityDate, depositRate, depoDCCType, notional, calendarType) depos.append(depo) # 1 year depositRate = 0.04 maturityDate = settlementDate.addMonths(12) depo = FinIborDeposit(settlementDate, maturityDate, depositRate, depoDCCType, notional, calendarType) depos.append(depo) fras = [] swaps = [] 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", depo._maturityDate, v)
def test_FinIborFRAsOnly(): # TO DO FIX THIS valuationDate = FinDate(2018, 2, 23) spotDays = 0 settlementDate = valuationDate.addWeekDays(spotDays) depoDCCType = FinDayCountTypes.ACT_360 notional = 100.0 payFixed = True calendarType = FinCalendarTypes.TARGET fras = [] # 1 x 4 FRA fraRate = 0.04 fraSettlementDate = settlementDate.addMonths(1) fraMaturityDate = settlementDate.addMonths(4) fra = FinIborFRA(fraSettlementDate, fraMaturityDate, fraRate, depoDCCType, notional, payFixed, calendarType) fras.append(fra) # 4 x 7 FRA fraRate = 0.08 fraSettlementDate = settlementDate.addMonths(4) fraMaturityDate = settlementDate.addMonths(7) fra = FinIborFRA(fraSettlementDate, fraMaturityDate, fraRate, depoDCCType, notional, payFixed, calendarType) fras.append(fra) depos = [] swaps = [] liborCurve = FinIborSingleCurve(valuationDate, depos, fras, swaps) testCases.header("DATE", "MATDATE", "VALUE") ''' Check calibration ''' for fra in fras: v = fra.value(settlementDate, liborCurve) testCases.print("FRA:", fra._maturityDate, v)
def test_FinBondEmbeddedOptionQUANTLIB(): # Based on example at the nice blog on Quantlib at # http://gouthamanbalaraman.com/blog/callable-bond-quantlib-python.html # I get a price of 68.97 for 1000 time steps which is higher than the # 68.38 found in blog article. But this is for 40 grid points. # Note also that a basis point vol of 0.120 is 12% which is VERY HIGH! valuationDate = FinDate(16, 8, 2016) settlementDate = valuationDate.addWeekDays(3) ########################################################################### discountCurve = FinDiscountCurveFlat(valuationDate, 0.035, FinFrequencyTypes.SEMI_ANNUAL) ########################################################################### issueDate = FinDate(15, 9, 2010) maturityDate = FinDate(15, 9, 2022) coupon = 0.025 freqType = FinFrequencyTypes.QUARTERLY accrualType = FinDayCountTypes.ACT_ACT_ICMA bond = FinBond(issueDate, maturityDate, coupon, freqType, accrualType) ########################################################################### # Set up the call and put times and prices ########################################################################### nextCallDate = FinDate(15, 9, 2016) callDates = [nextCallDate] callPrices = [100.0] for i in range(1, 24): nextCallDate = nextCallDate.addMonths(3) callDates.append(nextCallDate) callPrices.append(100.0) putDates = [] putPrices = [] # the value used in blog of 12% bp vol is unrealistic sigma = 0.12 # basis point volatility a = 0.03 puttableBond = FinBondEmbeddedOption(issueDate, maturityDate, coupon, freqType, accrualType, callDates, callPrices, putDates, putPrices) testCases.header("BOND PRICE", "PRICE") v = bond.cleanPriceFromDiscountCurve(settlementDate, discountCurve) testCases.print("Bond Pure Price:", v) testCases.header("TIME", "NumTimeSteps", "BondWithOption", "BondPure") timeSteps = range(100, 1000, 100) values = [] for numTimeSteps in timeSteps: model = FinModelRatesHW(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.title("Puttable Bond Price Convergence") plt.plot(timeSteps, values)
def test_FinIborCapFloor(): todayDate = FinDate(20, 6, 2019) valuationDate = todayDate startDate = todayDate.addWeekDays(2) maturityDate = startDate.addTenor("1Y") liborCurve = test_FinIborDepositsAndSwaps(todayDate) # The capfloor has begun # lastFixing = 0.028 ########################################################################## # COMPARISON OF MODELS ########################################################################## strikes = np.linspace(0.02, 0.08, 5) testCases.header("LABEL", "STRIKE", "BLK", "BLK_SHFTD", "SABR", "SABR_SHFTD", "HW", "BACH") model1 = FinModelBlack(0.20) model2 = FinModelBlackShifted(0.25, 0.0) model3 = FinModelSABR(0.013, 0.5, 0.5, 0.5) model4 = FinModelSABRShifted(0.013, 0.5, 0.5, 0.5, -0.008) model5 = FinModelRatesHW(0.30, 0.01) model6 = FinModelBachelier(0.01) for k in strikes: capFloorType = FinCapFloorTypes.CAP capfloor = FinIborCapFloor(startDate, maturityDate, capFloorType, k) cvalue1 = capfloor.value(valuationDate, liborCurve, model1) cvalue2 = capfloor.value(valuationDate, liborCurve, model2) cvalue3 = capfloor.value(valuationDate, liborCurve, model3) cvalue4 = capfloor.value(valuationDate, liborCurve, model4) cvalue5 = capfloor.value(valuationDate, liborCurve, model5) cvalue6 = capfloor.value(valuationDate, liborCurve, model6) testCases.print("CAP", k, cvalue1, cvalue2, cvalue3, cvalue4, cvalue5, cvalue6) testCases.header("LABEL", "STRIKE", "BLK", "BLK_SHFTD", "SABR", "SABR_SHFTD", "HW", "BACH") for k in strikes: capFloorType = FinCapFloorTypes.FLOOR capfloor = FinIborCapFloor(startDate, maturityDate, capFloorType, k) fvalue1 = capfloor.value(valuationDate, liborCurve, model1) fvalue2 = capfloor.value(valuationDate, liborCurve, model2) fvalue3 = capfloor.value(valuationDate, liborCurve, model3) fvalue4 = capfloor.value(valuationDate, liborCurve, model4) fvalue5 = capfloor.value(valuationDate, liborCurve, model5) fvalue6 = capfloor.value(valuationDate, liborCurve, model6) testCases.print("FLR", k, fvalue1, fvalue2, fvalue3, fvalue4, fvalue5, fvalue6) ############################################################################### # PUT CALL CHECK ############################################################################### testCases.header("LABEL", "STRIKE", "BLK", "BLK_SHFTD", "SABR", "SABR SHFTD", "HW", "BACH") for k in strikes: capFloorType = FinCapFloorTypes.CAP capfloor = FinIborCapFloor(startDate, maturityDate, capFloorType, k) cvalue1 = capfloor.value(valuationDate, liborCurve, model1) cvalue2 = capfloor.value(valuationDate, liborCurve, model2) cvalue3 = capfloor.value(valuationDate, liborCurve, model3) cvalue4 = capfloor.value(valuationDate, liborCurve, model4) cvalue5 = capfloor.value(valuationDate, liborCurve, model5) cvalue6 = capfloor.value(valuationDate, liborCurve, model6) capFloorType = FinCapFloorTypes.FLOOR capfloor = FinIborCapFloor(startDate, maturityDate, capFloorType, k) fvalue1 = capfloor.value(valuationDate, liborCurve, model1) fvalue2 = capfloor.value(valuationDate, liborCurve, model2) fvalue3 = capfloor.value(valuationDate, liborCurve, model3) fvalue4 = capfloor.value(valuationDate, liborCurve, model4) fvalue5 = capfloor.value(valuationDate, liborCurve, model5) fvalue6 = capfloor.value(valuationDate, liborCurve, model6) pcvalue1 = cvalue1 - fvalue1 pcvalue2 = cvalue2 - fvalue2 pcvalue3 = cvalue3 - fvalue3 pcvalue4 = cvalue4 - fvalue4 pcvalue5 = cvalue5 - fvalue5 pcvalue6 = cvalue6 - fvalue6 testCases.print("PUT_CALL", k, pcvalue1, pcvalue2, pcvalue3, pcvalue4, pcvalue5, pcvalue6)
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 = FinIborSwapOLD(settlementDate, "2Y", fixedLegType, (2.77417 + 2.77844) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "3Y", fixedLegType, (2.86098 + 2.86582) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "4Y", fixedLegType, (2.90240 + 2.90620) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "5Y", fixedLegType, (2.92944 + 2.92906) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "6Y", fixedLegType, (2.94001 + 2.94499) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "7Y", fixedLegType, (2.95352 + 2.95998) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "8Y", fixedLegType, (2.96830 + 2.97400) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "9Y", fixedLegType, (2.98403 + 2.98817) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "10Y", fixedLegType, (2.99716 + 3.00394) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "11Y", fixedLegType, (3.01344 + 3.01596) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "12Y", fixedLegType, (3.02276 + 3.02684) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "15Y", fixedLegType, (3.04092 + 3.04508) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "20Y", fixedLegType, (3.04417 + 3.05183) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "25Y", fixedLegType, (3.03219 + 3.03621) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "30Y", fixedLegType, (3.01030 + 3.01370) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "40Y", fixedLegType, (2.96946 + 2.97354) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "50Y", fixedLegType, (2.91552 + 2.93748) / 200, freq, accrual) swaps.append(swap) liborCurve = FinIborSingleCurveOLD(valuationDate, depos, fras, swaps, interpType) # 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)) testCases.print("FIXED:", swaps[0].fixedLegValue(valuationDate, liborCurve)) testCases.print( "FLOAT:", swaps[0].floatLegValue(valuationDate, liborCurve, liborCurve, None)) testCases.header("VALUATION TO SWAP SETTLEMENT DATE", " PV") testCases.print( "VALUE:", swaps[0].value(settlementDate, liborCurve, liborCurve, None)) testCases.print("FIXED:", swaps[0].fixedLegValue(settlementDate, liborCurve)) testCases.print( "FLOAT:", swaps[0].floatLegValue(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 = FinIborSwapOLD(settlementDate, "1Y", fixedLegType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0060 swap = FinIborSwapOLD(settlementDate, "2Y", fixedLegType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0072 swap = FinIborSwapOLD(settlementDate, "3Y", fixedLegType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0096 swap = FinIborSwapOLD(settlementDate, "4Y", fixedLegType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0124 swap = FinIborSwapOLD(settlementDate, "5Y", fixedLegType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0173 swap = FinIborSwapOLD(settlementDate, "7Y", fixedLegType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0219 swap = FinIborSwapOLD(settlementDate, "10Y", fixedLegType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0283 swap = FinIborSwapOLD(settlementDate, "30Y", fixedLegType, swapRate, freqType, dayCountType) swaps.append(swap) numRepeats = 10 start = time.time() for _ in range(0, numRepeats): _ = FinIborSingleCurveOLD(valuationDate, depos, fras, swaps, FinInterpTypes.FLAT_FWD_RATES) end = time.time() elapsed1 = end - start start = time.time() for _ in range(0, numRepeats): _ = FinIborSingleCurveOLD(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.US busDayAdjustRule = FinBusDayAdjustTypes.PRECEDING swapRate = 0.02776305 swap = FinIborSwapOLD(startDate, "2Y", fixedLegType, swapRate, fixedFreqType, fixedDCCType, notional, floatSpread, floatFreqType, floatDCCType, calendarType, busDayAdjustRule) swaps.append(swap) liborCurve = FinIborSingleCurveOLD(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 = FinIborSwapOLD(settlementDate, maturityDate, swapRate, fixedFreqType, # fixedDCCType) # swaps.append(swap) fixedLegType = FinSwapTypes.PAY maturityDate = settlementDate.addMonths(36) swap = FinIborSwapOLD(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(48) swap = FinIborSwapOLD(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(60) swap = FinIborSwapOLD(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(72) swap = FinIborSwapOLD(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(84) swap = FinIborSwapOLD(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(96) swap = FinIborSwapOLD(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(108) swap = FinIborSwapOLD(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(120) swap = FinIborSwapOLD(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(132) swap = FinIborSwapOLD(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(144) swap = FinIborSwapOLD(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(180) swap = FinIborSwapOLD(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(240) swap = FinIborSwapOLD(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(300) swap = FinIborSwapOLD(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(360) swap = FinIborSwapOLD(settlementDate, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) liborCurve = FinIborSingleCurveOLD(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_FinOISDepositsFRAsSwaps(): valuationDate = FinDate(2019, 9, 18) dccType = FinDayCountTypes.THIRTY_E_360_ISDA depos = [] spotDays = 0 settleDt = valuationDate.addWeekDays(spotDays) depoDCCType = FinDayCountTypes.ACT_360 notional = 100.0 calendarType = FinCalendarTypes.TARGET depos = [] # 1 month depositRate = 0.04 maturityDate = settleDt.addMonths(1) depo = FinIborDeposit(settleDt, maturityDate, depositRate, depoDCCType, notional, calendarType) depos.append(depo) fras = [] # 1 x 4 FRA fraRate = 0.04 frasettleDt = settleDt.addMonths(9) fraMaturityDate = settleDt.addMonths(13) fra = FinIborFRA(frasettleDt, fraMaturityDate, fraRate, dccType) fras.append(fra) # 4 x 7 FRA fraRate = 0.03 frasettleDt = settleDt.addMonths(13) fraMaturityDate = settleDt.addMonths(17) fra = FinIborFRA(frasettleDt, fraMaturityDate, fraRate, dccType) fras.append(fra) # 4 x 7 FRA fraRate = 0.07 frasettleDt = settleDt.addMonths(17) fraMaturityDate = settleDt.addMonths(21) fra = FinIborFRA(frasettleDt, fraMaturityDate, fraRate, dccType) fras.append(fra) swaps = [] fixedDCCType = FinDayCountTypes.ACT_365F fixedFreqType = FinFrequencyTypes.SEMI_ANNUAL swapRate = 0.05 # maturityDate = settleDt.addMonths(24) # swap = FinIborSwap(settleDt, maturityDate, swapRate, fixedFreqType, # fixedDCCType) # swaps.append(swap) fixedLegType = FinfixedLegTypes.PAY maturityDate = settleDt.addMonths(36) swap = FinOIS(settleDt, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settleDt.addMonths(48) swap = FinOIS(settleDt, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settleDt.addMonths(60) swap = FinOIS(settleDt, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settleDt.addMonths(72) swap = FinOIS(settleDt, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settleDt.addMonths(84) swap = FinOIS(settleDt, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settleDt.addMonths(96) swap = FinOIS(settleDt, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settleDt.addMonths(108) swap = FinOIS(settleDt, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settleDt.addMonths(120) swap = FinOIS(settleDt, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settleDt.addMonths(132) swap = FinOIS(settleDt, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settleDt.addMonths(144) swap = FinOIS(settleDt, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settleDt.addMonths(180) swap = FinOIS(settleDt, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settleDt.addMonths(240) swap = FinOIS(settleDt, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settleDt.addMonths(300) swap = FinOIS(settleDt, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settleDt.addMonths(360) swap = FinOIS(settleDt, maturityDate, fixedLegType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) liborCurve = FinOISCurve(valuationDate, depos, fras, swaps) df = liborCurve.df(settleDt) testCases.header("SETTLEMENT DATE", "DF") testCases.print(str(settleDt), 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_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 settleDt = valuationDate.addWeekDays(spotDays) accrual = FinDayCountTypes.THIRTY_E_360 depo = FinIborDeposit(settleDt, "1D", 1.712 / 100.0, accrual) depos = [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 settleDt = valuationDate.addWeekDays(spotDays) payRec = FinSwapTypes.PAY lag = 1 # Not used swaps = [] swap = FinOIS(settleDt, "2Y", payRec, (2.77417 + 2.77844) / 200, freq, accrual) swaps.append(swap) swap = FinOIS(settleDt, "3Y", payRec, (2.86098 + 2.86582) / 200, freq, accrual) swaps.append(swap) swap = FinOIS(settleDt, "4Y", payRec, (2.90240 + 2.90620) / 200, freq, accrual) swaps.append(swap) swap = FinOIS(settleDt, "5Y", payRec, (2.92944 + 2.92906) / 200, freq, accrual) swaps.append(swap) swap = FinOIS(settleDt, "6Y", payRec, (2.94001 + 2.94499) / 200, freq, accrual) swaps.append(swap) swap = FinOIS(settleDt, "7Y", payRec, (2.95352 + 2.95998) / 200, freq, accrual) swaps.append(swap) swap = FinOIS(settleDt, "8Y", payRec, (2.96830 + 2.97400) / 200, freq, accrual) swaps.append(swap) swap = FinOIS(settleDt, "9Y", payRec, (2.98403 + 2.98817) / 200, freq, accrual) swaps.append(swap) swap = FinOIS(settleDt, "10Y", payRec, (2.99716 + 3.00394) / 200, freq, accrual) swaps.append(swap) swap = FinOIS(settleDt, "11Y", payRec, (3.01344 + 3.01596) / 200, freq, accrual) swaps.append(swap) swap = FinOIS(settleDt, "12Y", payRec, (3.02276 + 3.02684) / 200, freq, accrual) swaps.append(swap) swap = FinOIS(settleDt, "15Y", payRec, (3.04092 + 3.04508) / 200, freq, accrual) swaps.append(swap) swap = FinOIS(settleDt, "20Y", payRec, (3.04417 + 3.05183) / 200, freq, accrual) swaps.append(swap) swap = FinOIS(settleDt, "25Y", payRec, (3.03219 + 3.03621) / 200, freq, accrual) swaps.append(swap) swap = FinOIS(settleDt, "30Y", payRec, (3.01030 + 3.01370) / 200, freq, accrual) swaps.append(swap) swap = FinOIS(settleDt, "40Y", payRec, (2.96946 + 2.97354) / 200, freq, accrual) swaps.append(swap) swap = FinOIS(settleDt, "50Y", payRec, (2.91552 + 2.93748) / 200, freq, accrual) swaps.append(swap) oisCurve = FinOISCurve(valuationDate, depos, fras, swaps) # swaps[0]._fixedLeg.printValuation() # swaps[0]._floatLeg.printValuation() # 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, oisCurve, None)) testCases.print("FIXED:", -swaps[0]._fixedLeg.value(valuationDate, oisCurve)) testCases.print("FLOAT:", swaps[0]._floatLeg.value(valuationDate, oisCurve, None)) testCases.header("VALUATION TO SWAP SETTLEMENT DATE", " PV") testCases.print("VALUE:", swaps[0].value(settleDt, oisCurve, None)) testCases.print("FIXED:", -swaps[0]._fixedLeg.value(settleDt, oisCurve)) testCases.print("FLOAT:", swaps[0]._floatLeg.value(settleDt, oisCurve, None))
def test_derivativePricingExample(): valuationDate = FinDate(10, 11, 2011) # We do the O/N rate which settles on trade date spotDays = 0 settleDt = valuationDate.addWeekDays(spotDays) fras = [] swaps = [] dayCountType = FinDayCountTypes.THIRTY_E_360_ISDA # dayCountType = FinDayCountTypes.ACT_360 freqType = FinFrequencyTypes.SEMI_ANNUAL fixedLegType = FinfixedLegTypes.PAY swapRate = 0.0058 swap = FinOIS(settleDt, "1Y", fixedLegType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0060 swap = FinOIS(settleDt, "2Y", fixedLegType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0072 swap = FinOIS(settleDt, "3Y", fixedLegType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0096 swap = FinOIS(settleDt, "4Y", fixedLegType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0124 swap = FinOIS(settleDt, "5Y", fixedLegType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0173 swap = FinOIS(settleDt, "7Y", fixedLegType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0219 swap = FinOIS(settleDt, "10Y", fixedLegType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0283 swap = FinOIS(settleDt, "30Y", fixedLegType, swapRate, freqType, dayCountType) swaps.append(swap) numRepeats = 10 start = time.time() for _ in range(0, numRepeats): _ = FinOISCurve(valuationDate, fras, swaps, FinInterpTypes.FLAT_FWD_RATES) end = time.time() elapsed1 = end - start start = time.time() for _ in range(0, numRepeats): _ = FinOISCurve(valuationDate, 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_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 = FinIborSwapOLD(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 = FinIborSwapOLD(settlementDate, "2Y", fixedLegType, -0.1525 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "3Y", fixedLegType, -0.0185 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "4Y", fixedLegType, 0.1315 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "5Y", fixedLegType, 0.2745 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "6Y", fixedLegType, 0.4135 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "7Y", fixedLegType, 0.5439 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "8Y", fixedLegType, 0.6652 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "9Y", fixedLegType, 0.7784 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "10Y", fixedLegType, 0.8799 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "11Y", fixedLegType, 0.9715 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "12Y", fixedLegType, 1.0517 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "15Y", fixedLegType, 1.2369 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "20Y", fixedLegType, 1.3965 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "25Y", fixedLegType, 1.4472 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "30Y", fixedLegType, 1.4585 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "35Y", fixedLegType, 1.4595 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "40Y", fixedLegType, 1.4535 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwapOLD(settlementDate, "45Y", fixedLegType, 1.4410 / 100.0, fixedFreqType, fixedDCCType) swaps.append(swap) swap = FinIborSwapOLD(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 testFinIborSwaptionModels(): ########################################################################## # COMPARISON OF MODELS ########################################################################## valuationDate = FinDate(2011, 1, 1) liborCurve = test_FinIborDepositsAndSwaps(valuationDate) exerciseDate = FinDate(2012, 1, 1) swapMaturityDate = FinDate(2017, 1, 1) swapFixedFrequencyType = FinFrequencyTypes.SEMI_ANNUAL swapFixedDayCountType = FinDayCountTypes.ACT_365F strikes = np.linspace(0.02, 0.08, 5) testCases.header("LAB", "STRIKE", "BLK", "BLK_SHFT", "SABR", "SABR_SHFT", "HW", "BK") model1 = FinModelBlack(0.00001) model2 = FinModelBlackShifted(0.00001, 0.0) model3 = FinModelSABR(0.013, 0.5, 0.5, 0.5) model4 = FinModelSABRShifted(0.013, 0.5, 0.5, 0.5, -0.008) model5 = FinModelRatesHW(0.00001, 0.00001) model6 = FinModelRatesBK(0.01, 0.01) settlementDate = valuationDate.addWeekDays(2) for k in strikes: swaptionType = FinSwapTypes.PAY swaption = FinIborSwaption(settlementDate, exerciseDate, swapMaturityDate, swaptionType, k, swapFixedFrequencyType, swapFixedDayCountType) swap1 = swaption.value(valuationDate, liborCurve, model1) swap2 = swaption.value(valuationDate, liborCurve, model2) swap3 = swaption.value(valuationDate, liborCurve, model3) swap4 = swaption.value(valuationDate, liborCurve, model4) swap5 = swaption.value(valuationDate, liborCurve, model5) swap6 = swaption.value(valuationDate, liborCurve, model6) testCases.print("PAY", k, swap1, swap2, swap3, swap4, swap5, swap6) testCases.header("LABEL", "STRIKE", "BLK", "BLK_SHFTD", "SABR", "SABR_SHFTD", "HW", "BK") for k in strikes: swaptionType = FinSwapTypes.RECEIVE swaption = FinIborSwaption(settlementDate, exerciseDate, swapMaturityDate, swaptionType, k, swapFixedFrequencyType, swapFixedDayCountType) swap1 = swaption.value(valuationDate, liborCurve, model1) swap2 = swaption.value(valuationDate, liborCurve, model2) swap3 = swaption.value(valuationDate, liborCurve, model3) swap4 = swaption.value(valuationDate, liborCurve, model4) swap5 = swaption.value(valuationDate, liborCurve, model5) swap6 = swaption.value(valuationDate, liborCurve, model6) testCases.print("REC", k, swap1, swap2, swap3, swap4, swap5, swap6)