def test_FinFXForward(): # https://stackoverflow.com/questions/48778712 # /fx-vanilla-call-price-in-quantlib-doesnt-match-bloomberg valueDate = FinDate(13, 2, 2018) expiryDate = valueDate.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 = valueDate.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 = FinIborCurve(settlementDate, depos, fras, swaps) depos = [] fras = [] swaps = [] depositRate = ccy2InterestRate depo = FinIborDeposit(settlementDate, maturityDate, depositRate, FinDayCountTypes.ACT_360, notional, calendarType) depos.append(depo) domDiscountCurve = FinIborCurve(settlementDate, 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(valueDate, spotFXRate, domDiscountCurve, forDiscountCurve) fwdFXRate = fxForward.forward(valueDate, spotFXRate, domDiscountCurve, forDiscountCurve) testCases.print(spotFXRate, fwdFXRate, fwdValue)
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 = FinIborCurve(settlementDate, 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 = FinIborCurve(settlementDate, 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 buildIborCurve(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 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, 0.0502, fixedFreq, dcType) swaps.append(swap1) maturityDate = settlementDate.addMonths(24) swap2 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, 0.0502, fixedFreq, dcType) swaps.append(swap2) maturityDate = settlementDate.addMonths(36) swap3 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, 0.0501, fixedFreq, dcType) swaps.append(swap3) maturityDate = settlementDate.addMonths(48) swap4 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, 0.0502, fixedFreq, dcType) swaps.append(swap4) maturityDate = settlementDate.addMonths(60) swap5 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, 0.0501, fixedFreq, dcType) swaps.append(swap5) liborCurve = FinIborCurve(settlementDate, depos, fras, swaps) return liborCurve
def test_FinIborDepositsAndSwaps(valuationDate): depoBasis = FinDayCountTypes.THIRTY_E_360_ISDA depos = [] spotDays = 0 settlementDate = valuationDate.addWeekDays(spotDays) depositRate = 0.05 depo1 = FinIborDeposit(settlementDate, "1M", depositRate, depoBasis) depo2 = FinIborDeposit(settlementDate, "3M", depositRate, depoBasis) depo3 = FinIborDeposit(settlementDate, "6M", depositRate, depoBasis) depos.append(depo1) depos.append(depo2) depos.append(depo3) fras = [] swaps = [] fixedBasis = FinDayCountTypes.ACT_365F fixedFreq = FinFrequencyTypes.SEMI_ANNUAL swapType = FinSwapTypes.PAYER swapRate = 0.05 swap1 = FinIborSwap(settlementDate, "1Y", swapType, swapRate, fixedFreq, fixedBasis) swap2 = FinIborSwap(settlementDate, "3Y", swapType, swapRate, fixedFreq, fixedBasis) swap3 = FinIborSwap(settlementDate, "5Y", swapType, swapRate, fixedFreq, fixedBasis) swaps.append(swap1) swaps.append(swap2) swaps.append(swap3) liborCurve = FinIborCurve(settlementDate, depos, fras, swaps) return liborCurve
def buildIborCurve(valuationDate): settlementDate = valuationDate.addDays(2) dcType = FinDayCountTypes.ACT_360 depos = [] fras = [] swaps = [] maturityDate = settlementDate.addMonths(1) depo1 = FinIborDeposit(settlementDate, maturityDate, -0.00251, dcType) depos.append(depo1) # Series of 1M futures startDate = settlementDate.nextIMMDate() endDate = startDate.addMonths(1) fra = FinIborFRA(startDate, endDate, -0.0023, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinIborFRA(startDate, endDate, -0.00234, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinIborFRA(startDate, endDate, -0.00225, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinIborFRA(startDate, endDate, -0.00226, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinIborFRA(startDate, endDate, -0.00219, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinIborFRA(startDate, endDate, -0.00213, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinIborFRA(startDate, endDate, -0.00186, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinIborFRA(startDate, endDate, -0.00189, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinIborFRA(startDate, endDate, -0.00175, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinIborFRA(startDate, endDate, -0.00143, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinIborFRA(startDate, endDate, -0.00126, dcType) fras.append(fra) startDate = startDate.addMonths(1) endDate = startDate.addMonths(1) fra = FinIborFRA(startDate, endDate, -0.00126, dcType) fras.append(fra) ########################################################################### ########################################################################### ########################################################################### ########################################################################### fixedFreq = FinFrequencyTypes.ANNUAL dcType = FinDayCountTypes.THIRTY_E_360 swapType = FinSwapTypes.PAYER ##################################################### maturityDate = settlementDate.addMonths(24) swap1 = FinIborSwap(settlementDate, maturityDate, swapType, -0.001506, fixedFreq, dcType) swaps.append(swap1) ##################################################### maturityDate = settlementDate.addMonths(36) swap2 = FinIborSwap(settlementDate, maturityDate, swapType, -0.000185, fixedFreq, dcType) swaps.append(swap2) ##################################################### maturityDate = settlementDate.addMonths(48) swap3 = FinIborSwap(settlementDate, maturityDate, swapType, 0.001358, fixedFreq, dcType) swaps.append(swap3) ##################################################### maturityDate = settlementDate.addMonths(60) swap4 = FinIborSwap(settlementDate, maturityDate, swapType, 0.0027652, fixedFreq, dcType) swaps.append(swap4) maturityDate = settlementDate.addMonths(72) swap5 = FinIborSwap(settlementDate, maturityDate, swapType, 0.0041539, fixedFreq, dcType) swaps.append(swap5) maturityDate = settlementDate.addMonths(84) swap6 = FinIborSwap(settlementDate, maturityDate, swapType, 0.0054604, fixedFreq, dcType) swaps.append(swap6) maturityDate = settlementDate.addMonths(96) swap7 = FinIborSwap(settlementDate, maturityDate, swapType, 0.006674, fixedFreq, dcType) swaps.append(swap7) maturityDate = settlementDate.addMonths(108) swap8 = FinIborSwap(settlementDate, maturityDate, swapType, 0.007826, fixedFreq, dcType) swaps.append(swap8) maturityDate = settlementDate.addMonths(120) swap9 = FinIborSwap(settlementDate, maturityDate, swapType, 0.008821, fixedFreq, dcType) swaps.append(swap9) maturityDate = settlementDate.addMonths(132) swap10 = FinIborSwap(settlementDate, maturityDate, swapType, 0.0097379, fixedFreq, dcType) swaps.append(swap10) maturityDate = settlementDate.addMonths(144) swap11 = FinIborSwap(settlementDate, maturityDate, swapType, 0.0105406, fixedFreq, dcType) swaps.append(swap11) maturityDate = settlementDate.addMonths(180) swap12 = FinIborSwap(settlementDate, maturityDate, swapType, 0.0123927, fixedFreq, dcType) swaps.append(swap12) maturityDate = settlementDate.addMonths(240) swap13 = FinIborSwap(settlementDate, maturityDate, swapType, 0.0139882, fixedFreq, dcType) swaps.append(swap13) maturityDate = settlementDate.addMonths(300) swap14 = FinIborSwap(settlementDate, maturityDate, swapType, 0.0144972, fixedFreq, dcType) swaps.append(swap14) maturityDate = settlementDate.addMonths(360) swap15 = FinIborSwap(settlementDate, maturityDate, swapType, 0.0146081, fixedFreq, dcType) swaps.append(swap15) maturityDate = settlementDate.addMonths(420) swap16 = FinIborSwap(settlementDate, maturityDate, swapType, 0.01461897, fixedFreq, dcType) swaps.append(swap16) maturityDate = settlementDate.addMonths(480) swap17 = FinIborSwap(settlementDate, maturityDate, swapType, 0.014567455, fixedFreq, dcType) swaps.append(swap17) maturityDate = settlementDate.addMonths(540) swap18 = FinIborSwap(settlementDate, maturityDate, swapType, 0.0140826, fixedFreq, dcType) swaps.append(swap18) maturityDate = settlementDate.addMonths(600) swap19 = FinIborSwap(settlementDate, maturityDate, swapType, 0.01436822, fixedFreq, dcType) swaps.append(swap19) liborCurve = FinIborCurve(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 buildFullIssuerCurve(valuationDate): dcType = FinDayCountTypes.ACT_360 depos = [] irBump = 0.0 m = 1.0 # 0.00000000000 spotDays = 0 settlementDate = valuationDate.addDays(spotDays) maturityDate = settlementDate.addMonths(1) depo1 = FinIborDeposit(settlementDate, maturityDate, m * 0.0016, dcType) maturityDate = settlementDate.addMonths(2) depo2 = FinIborDeposit(settlementDate, maturityDate, m * 0.0020, dcType) maturityDate = settlementDate.addMonths(3) depo3 = FinIborDeposit(settlementDate, maturityDate, m * 0.0024, dcType) maturityDate = settlementDate.addMonths(6) depo4 = FinIborDeposit(settlementDate, maturityDate, m * 0.0033, dcType) maturityDate = settlementDate.addMonths(12) depo5 = FinIborDeposit(settlementDate, maturityDate, m * 0.0056, dcType) depos.append(depo1) depos.append(depo2) depos.append(depo3) depos.append(depo4) depos.append(depo5) fras = [] spotDays = 2 settlementDate = valuationDate.addDays(spotDays) swaps = [] dcType = FinDayCountTypes.THIRTY_E_360_ISDA fixedFreq = FinFrequencyTypes.SEMI_ANNUAL maturityDate = settlementDate.addMonths(24) swap1 = FinIborSwap( settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.0044 + irBump, fixedFreq, dcType) swaps.append(swap1) maturityDate = settlementDate.addMonths(36) swap2 = FinIborSwap( settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.0078 + irBump, fixedFreq, dcType) swaps.append(swap2) maturityDate = settlementDate.addMonths(48) swap3 = FinIborSwap( settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.0119 + irBump, fixedFreq, dcType) swaps.append(swap3) maturityDate = settlementDate.addMonths(60) swap4 = FinIborSwap( settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.0158 + irBump, fixedFreq, dcType) swaps.append(swap4) maturityDate = settlementDate.addMonths(72) swap5 = FinIborSwap( settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.0192 + irBump, fixedFreq, dcType) swaps.append(swap5) maturityDate = settlementDate.addMonths(84) swap6 = FinIborSwap( settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.0219 + irBump, fixedFreq, dcType) swaps.append(swap6) maturityDate = settlementDate.addMonths(96) swap7 = FinIborSwap( settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.0242 + irBump, fixedFreq, dcType) swaps.append(swap7) maturityDate = settlementDate.addMonths(108) swap8 = FinIborSwap( settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.0261 + irBump, fixedFreq, dcType) swaps.append(swap8) maturityDate = settlementDate.addMonths(120) swap9 = FinIborSwap( settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.0276 + irBump, fixedFreq, dcType) swaps.append(swap9) liborCurve = FinIborCurve(valuationDate, depos, fras, swaps) cdsMarketContracts = [] cdsCoupon = 0.005743 maturityDate = valuationDate.nextCDSDate(6) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) cdsCoupon = 0.007497 maturityDate = valuationDate.nextCDSDate(12) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) cdsCoupon = 0.011132 maturityDate = valuationDate.nextCDSDate(24) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) cdsCoupon = 0.013932 maturityDate = valuationDate.nextCDSDate(36) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) cdsCoupon = 0.015764 maturityDate = valuationDate.nextCDSDate(48) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) cdsCoupon = 0.017366 maturityDate = valuationDate.nextCDSDate(60) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) cdsCoupon = 0.020928 maturityDate = valuationDate.nextCDSDate(84) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) cdsCoupon = 0.022835 maturityDate = valuationDate.nextCDSDate(120) cds = FinCDS(valuationDate, maturityDate, cdsCoupon) cdsMarketContracts.append(cds) recoveryRate = 0.40 issuerCurve = FinCDSCurve(valuationDate, cdsMarketContracts, liborCurve, recoveryRate) return liborCurve, issuerCurve
def testFinIborCashSettledSwaption(): testCases.header("LABEL", "VALUE") valuationDate = FinDate(1, 1, 2020) settlementDate = FinDate(1, 1, 2020) depoDCCType = FinDayCountTypes.THIRTY_E_360_ISDA depos = [] depo = FinIborDeposit(settlementDate, "1W", 0.0023, depoDCCType) depos.append(depo) depo = FinIborDeposit(settlementDate, "1M", 0.0023, depoDCCType) depos.append(depo) depo = FinIborDeposit(settlementDate, "3M", 0.0023, depoDCCType) depos.append(depo) depo = FinIborDeposit(settlementDate, "6M", 0.0023, depoDCCType) depos.append(depo) # No convexity correction provided so I omit interest rate futures settlementDate = FinDate(2, 1, 2020) swaps = [] accType = FinDayCountTypes.ACT_365F fixedFreqType = FinFrequencyTypes.SEMI_ANNUAL swapType = FinSwapTypes.PAYER swap = FinIborSwap(settlementDate, "3Y", swapType, 0.00790, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "4Y", swapType, 0.01200, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "5Y", swapType, 0.01570, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "6Y", swapType, 0.01865, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "7Y", swapType, 0.02160, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "8Y", swapType, 0.02350, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "9Y", swapType, 0.02540, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "10Y", swapType, 0.0273, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "15Y", swapType, 0.0297, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "20Y", swapType, 0.0316, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "25Y", swapType, 0.0335, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "30Y", swapType, 0.0354, fixedFreqType, accType) swaps.append(swap) liborCurve = FinIborCurve(valuationDate, depos, [], swaps, FinInterpTypes.LINEAR_ZERO_RATES) exerciseDate = settlementDate.addTenor("5Y") swapMaturityDate = exerciseDate.addTenor("5Y") swapFixedCoupon = 0.040852 swapFixedFrequencyType = FinFrequencyTypes.SEMI_ANNUAL swapFixedDayCountType = FinDayCountTypes.THIRTY_E_360_ISDA swapFloatFrequencyType = FinFrequencyTypes.QUARTERLY swapFloatDayCountType = FinDayCountTypes.ACT_360 swapNotional = 1000000 swaptionType = FinSwapTypes.PAYER swaption = FinIborSwaption(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 test_FinIborSwaptionQLExample(): # valuationDate = FinDate(28, 2, 2014) settlementDate = FinDate(4, 3, 2014) depoDCCType = FinDayCountTypes.THIRTY_E_360_ISDA depos = [] depo = FinIborDeposit(settlementDate, "1W", 0.0023, depoDCCType) depos.append(depo) depo = FinIborDeposit(settlementDate, "1M", 0.0023, depoDCCType) depos.append(depo) depo = FinIborDeposit(settlementDate, "3M", 0.0023, depoDCCType) depos.append(depo) depo = FinIborDeposit(settlementDate, "6M", 0.0023, depoDCCType) depos.append(depo) # No convexity correction provided so I omit interest rate futures swaps = [] accType = FinDayCountTypes.ACT_365F fixedFreqType = FinFrequencyTypes.SEMI_ANNUAL swapType = FinSwapTypes.PAYER swap = FinIborSwap(settlementDate, "3Y", swapType, 0.00790, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "4Y", swapType, 0.01200, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "5Y", swapType, 0.01570, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "6Y", swapType, 0.01865, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "7Y", swapType, 0.02160, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "8Y", swapType, 0.02350, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "9Y", swapType, 0.02540, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "10Y", swapType, 0.0273, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "15Y", swapType, 0.0297, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "20Y", swapType, 0.0316, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "25Y", swapType, 0.0335, fixedFreqType, accType) swaps.append(swap) swap = FinIborSwap(settlementDate, "30Y", swapType, 0.0354, fixedFreqType, accType) swaps.append(swap) liborCurve = FinIborCurve(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 = FinSwapTypes.PAYER swaption = FinIborSwaption(settlementDate, exerciseDate, swapMaturityDate, swaptionType, swapFixedCoupon, swapFixedFrequencyType, swapFixedDayCountType, swapNotional, swapFloatFrequencyType, swapFloatDayCountType) testCases.header("MODEL", "VALUE") model = FinModelBlack(0.1533) v = swaption.value(settlementDate, liborCurve, model) testCases.print(model.__class__, v) model = FinModelBlackShifted(0.1533, -0.008) v = swaption.value(settlementDate, liborCurve, model) testCases.print(model.__class__, v) model = FinModelSABR(0.132, 0.5, 0.5, 0.5) v = swaption.value(settlementDate, liborCurve, model) testCases.print(model.__class__, v) model = FinModelSABRShifted(0.352, 0.5, 0.15, 0.15, -0.005) v = swaption.value(settlementDate, liborCurve, model) testCases.print(model.__class__, v) model = FinModelRatesHW(0.010000000, 0.00000000001) v = swaption.value(settlementDate, liborCurve, model) testCases.print(model.__class__, v)
def test_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.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 swapType = FinSwapTypes.PAYER swaps = [] swap = FinIborSwap(settlementDate, "2Y", swapType, (2.77417 + 2.77844) / 200, freq, accrual, notional) swaps.append(swap) swap = FinIborSwap(settlementDate, "3Y", swapType, (2.86098 + 2.86582) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwap(settlementDate, "4Y", swapType, (2.90240 + 2.90620) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwap(settlementDate, "5Y", swapType, (2.92944 + 2.92906) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwap(settlementDate, "6Y", swapType, (2.94001 + 2.94499) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwap(settlementDate, "7Y", swapType, (2.95352 + 2.95998) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwap(settlementDate, "8Y", swapType, (2.96830 + 2.97400) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwap(settlementDate, "9Y", swapType, (2.98403 + 2.98817) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwap(settlementDate, "10Y", swapType, (2.99716 + 3.00394) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwap(settlementDate, "11Y", swapType, (3.01344 + 3.01596) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwap(settlementDate, "12Y", swapType, (3.02276 + 3.02684) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwap(settlementDate, "15Y", swapType, (3.04092 + 3.04508) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwap(settlementDate, "20Y", swapType, (3.04417 + 3.05183) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwap(settlementDate, "25Y", swapType, (3.03219 + 3.03621) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwap(settlementDate, "30Y", swapType, (3.01030 + 3.01370) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwap(settlementDate, "40Y", swapType, (2.96946 + 2.97354) / 200, freq, accrual) swaps.append(swap) swap = FinIborSwap(settlementDate, "50Y", swapType, (2.91552 + 2.93748) / 200, freq, accrual) swaps.append(swap) liborCurve = FinIborCurve(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_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 = FinIborDeposit(settlementDate, maturityDate, domDepoRate, FinDayCountTypes.ACT_360, notional, calendarType) depos.append(depo) domDiscountCurve = FinIborCurve(settlementDate, depos, fras, swaps) depos = [] fras = [] swaps = [] depo = FinIborDeposit(settlementDate, maturityDate, forDepoRate, FinDayCountTypes.ACT_360, notional, calendarType) depos.append(depo) forDiscountCurve = FinIborCurve(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_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 = FinIborSwap(curveDate, maturityDate, FinSwapTypes.PAYER, fixedCoupon, fixedFreq, fixedDCC) swaps.append(swap) libor_curve = FinIborCurve(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 test_FinIborDepositsFuturesSwaps(): spotDate = FinDate(6, 6, 2018) spotDays = 0 settlementDate = spotDate.addWeekDays(spotDays) depoDCCType = FinDayCountTypes.ACT_360 depos = [] depositRate = 0.0231381 depo = FinIborDeposit(settlementDate, "3M", depositRate, depoDCCType) depos.append(depo) depositRate = 0.027 depo = FinIborDeposit(settlementDate, "3M", depositRate, depoDCCType) depos.append(depo) depos = [] depo = FinIborDeposit(settlementDate, "1M", 0.0230, depoDCCType) depos.append(depo) depo = FinIborDeposit(settlementDate, "2M", 0.0235, depoDCCType) depos.append(depo) depo = FinIborDeposit(settlementDate, "3M", 0.0240, depoDCCType) depos.append(depo) fras = [] fraRate = futureToFRARate(97.6675, -0.00005) fraSettlementDate = spotDate.nextIMMDate() fraMaturityDate = fraSettlementDate.nextIMMDate() fra = FinIborFRA(fraSettlementDate, fraMaturityDate, fraRate, depoDCCType) fras.append(fra) fraRate = futureToFRARate(97.5200, -0.00060) fraSettlementDate = fraMaturityDate fraMaturityDate = fraSettlementDate.nextIMMDate() fra = FinIborFRA(fraSettlementDate, fraMaturityDate, fraRate, depoDCCType) fras.append(fra) fraRate = futureToFRARate(97.3550, -0.00146) fraSettlementDate = fraMaturityDate fraMaturityDate = fraSettlementDate.nextIMMDate() fra = FinIborFRA(fraSettlementDate, fraMaturityDate, fraRate, depoDCCType) fras.append(fra) fraRate = futureToFRARate(97.2450, -0.00263) fraSettlementDate = fraMaturityDate fraMaturityDate = fraSettlementDate.nextIMMDate() fra = FinIborFRA(fraSettlementDate, fraMaturityDate, fraRate, depoDCCType) fras.append(fra) fraRate = futureToFRARate(97.1450, -0.00411) fraSettlementDate = fraMaturityDate fraMaturityDate = fraSettlementDate.nextIMMDate() fra = FinIborFRA(fraSettlementDate, fraMaturityDate, fraRate, depoDCCType) fras.append(fra) fraRate = futureToFRARate(97.0750, -0.00589) fraSettlementDate = fraSettlementDate.nextIMMDate() fraMaturityDate = fraSettlementDate.nextIMMDate() fra = FinIborFRA(fraSettlementDate, fraMaturityDate, fraRate, depoDCCType) fras.append(fra) ########################################################################### spotDays = 2 startDate = spotDate.addWeekDays(spotDays) swaps = [] swapType = FinSwapTypes.PAYER fixedDCCType = FinDayCountTypes.THIRTY_E_360 fixedFreqType = FinFrequencyTypes.SEMI_ANNUAL floatFreqType = FinFrequencyTypes.QUARTERLY notional = 1000000 floatSpread = 0.0 floatDCCType = FinDayCountTypes.ACT_360 calendarType = FinCalendarTypes.US busDayAdjustRule = FinBusDayAdjustTypes.PRECEDING swapRate = 0.02776305 swap = FinIborSwap(startDate, "2Y", swapType, swapRate, fixedFreqType, fixedDCCType, notional, floatSpread, floatFreqType, floatDCCType, calendarType, busDayAdjustRule) swaps.append(swap) liborCurve = FinIborCurve(spotDate, depos, fras, swaps) times = np.linspace(0.0, 2.0, 25) dates = spotDate.addYears(times) zeroRates = liborCurve.zeroRate(dates) fwdRates = liborCurve.fwd(dates) if PLOT_GRAPHS: plt.figure(figsize=(8, 6)) plt.plot(times, zeroRates * 100, label="zero rates") plt.plot(times, fwdRates * 100, label="fwd rates") plt.xlabel("Times") plt.ylabel("CC forward rates") plt.legend() print("==============================================================") for fra in fras: print(fra) print("==============================================================") endDate = spotDate df = liborCurve.df(endDate) print(endDate, df) endDate = settlementDate df = liborCurve.df(endDate) print(endDate, df) endDate = FinDate(20, 6, 2018) df = liborCurve.df(endDate) print(endDate, df) for depo in depos: endDate = depo._maturityDate df = liborCurve.df(endDate) print(endDate, df) for fra in fras: endDate = fra._maturityDate df = liborCurve.df(endDate) print(endDate, df) for swap in swaps: endDate = swap._maturityDate df = liborCurve.df(endDate) print(endDate, df) swap.printFixedLegPV(spotDate) swap.printFloatLegPV(spotDate)
def test_FinIborDepositsFRAsSwaps(): valuationDate = FinDate(2019, 9, 18) dccType = FinDayCountTypes.THIRTY_E_360_ISDA depos = [] spotDays = 0 settlementDate = valuationDate.addWeekDays(spotDays) depositRate = 0.050 maturityDate = settlementDate.addMonths(1) depo = FinIborDeposit(settlementDate, maturityDate, depositRate, dccType) depos.append(depo) maturityDate = settlementDate.addMonths(2) depo = FinIborDeposit(settlementDate, maturityDate, depositRate, dccType) depos.append(depo) maturityDate = settlementDate.addMonths(3) depo = FinIborDeposit(settlementDate, maturityDate, depositRate, dccType) depos.append(depo) maturityDate = settlementDate.addMonths(6) depo = FinIborDeposit(settlementDate, maturityDate, depositRate, dccType) depos.append(depo) maturityDate = settlementDate.addMonths(9) depo = FinIborDeposit(settlementDate, maturityDate, depositRate, dccType) depos.append(depo) maturityDate = settlementDate.addMonths(12) depo = FinIborDeposit(settlementDate, maturityDate, depositRate, dccType) depos.append(depo) fras = [] # 1 x 4 FRA fraRate = 0.04 fraSettlementDate = settlementDate.addMonths(9) fraMaturityDate = settlementDate.addMonths(13) fra = FinIborFRA(fraSettlementDate, fraMaturityDate, fraRate, dccType) fras.append(fra) # 4 x 7 FRA fraRate = 0.03 fraSettlementDate = settlementDate.addMonths(13) fraMaturityDate = settlementDate.addMonths(17) fra = FinIborFRA(fraSettlementDate, fraMaturityDate, fraRate, dccType) fras.append(fra) # 4 x 7 FRA fraRate = 0.07 fraSettlementDate = settlementDate.addMonths(17) fraMaturityDate = settlementDate.addMonths(21) fra = FinIborFRA(fraSettlementDate, fraMaturityDate, fraRate, dccType) fras.append(fra) swaps = [] fixedDCCType = FinDayCountTypes.ACT_365F fixedFreqType = FinFrequencyTypes.SEMI_ANNUAL swapRate = 0.05 # maturityDate = settlementDate.addMonths(24) # swap = FinIborSwap(settlementDate, maturityDate, swapRate, fixedFreqType, # fixedDCCType) # swaps.append(swap) swapType = FinSwapTypes.PAYER maturityDate = settlementDate.addMonths(36) swap = FinIborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(48) swap = FinIborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(60) swap = FinIborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(72) swap = FinIborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(84) swap = FinIborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(96) swap = FinIborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(108) swap = FinIborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(120) swap = FinIborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(132) swap = FinIborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(144) swap = FinIborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(180) swap = FinIborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(240) swap = FinIborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(300) swap = FinIborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) maturityDate = settlementDate.addMonths(360) swap = FinIborSwap(settlementDate, maturityDate, swapType, swapRate, fixedFreqType, fixedDCCType) swaps.append(swap) liborCurve = FinIborCurve(valuationDate, depos, fras, swaps) df = liborCurve.df(settlementDate) testCases.header("SETTLEMENT DATE", "DF") testCases.print(str(settlementDate), df) testCases.header("DATE", "DF") for deposit in depos: df = liborCurve.df(deposit._maturityDate) testCases.print(str(deposit._maturityDate), df) for swap in swaps: df = liborCurve.df(swap._maturityDate) testCases.print(str(swap._maturityDate), df)
def buildIborCurve(valueDate): depoDCCType = FinDayCountTypes.THIRTY_E_360_ISDA depos = [] payFixed = FinSwapTypes.PAYER spotDays = 2 settlementDate = valueDate.addWeekDays(spotDays) depositRate = 0.050 maturityDate = settlementDate.addMonths(1) depo1 = FinIborDeposit(settlementDate, maturityDate, depositRate, depoDCCType) maturityDate = settlementDate.addMonths(3) depo2 = FinIborDeposit(settlementDate, maturityDate, depositRate, depoDCCType) maturityDate = settlementDate.addMonths(6) depo3 = FinIborDeposit(settlementDate, maturityDate, depositRate, depoDCCType) maturityDate = settlementDate.addMonths(9) depo4 = FinIborDeposit(settlementDate, maturityDate, depositRate, depoDCCType) maturityDate = settlementDate.addMonths(12) depo5 = FinIborDeposit(settlementDate, maturityDate, depositRate, depoDCCType) depos.append(depo1) depos.append(depo2) depos.append(depo3) depos.append(depo4) depos.append(depo5) fras = [] fixedDCCType = FinDayCountTypes.ACT_365F fixedFreqType = FinFrequencyTypes.SEMI_ANNUAL swaps = [] swapRate = 0.05 maturityDate = settlementDate.addMonths(24) swap1 = FinIborSwap(settlementDate, maturityDate, swapRate, payFixed, fixedFreqType, fixedDCCType) swaps.append(swap1) maturityDate = settlementDate.addMonths(36) swap2 = FinIborSwap(settlementDate, maturityDate, swapRate, payFixed, fixedFreqType, fixedDCCType) swaps.append(swap2) maturityDate = settlementDate.addMonths(48) swap3 = FinIborSwap(settlementDate, maturityDate, swapRate, payFixed, fixedFreqType, fixedDCCType) swaps.append(swap3) maturityDate = settlementDate.addMonths(60) swap4 = FinIborSwap(settlementDate, maturityDate, swapRate, payFixed, fixedFreqType, fixedDCCType) swaps.append(swap4) maturityDate = settlementDate.addMonths(72) swap5 = FinIborSwap(settlementDate, maturityDate, swapRate, payFixed, fixedFreqType, fixedDCCType) swaps.append(swap5) maturityDate = settlementDate.addMonths(84) swap6 = FinIborSwap(settlementDate, maturityDate, swapRate, payFixed, fixedFreqType, fixedDCCType) swaps.append(swap6) maturityDate = settlementDate.addMonths(96) swap7 = FinIborSwap(settlementDate, maturityDate, swapRate, payFixed, fixedFreqType, fixedDCCType) swaps.append(swap7) maturityDate = settlementDate.addMonths(108) swap8 = FinIborSwap(settlementDate, maturityDate, swapRate, payFixed, fixedFreqType, fixedDCCType) swaps.append(swap8) maturityDate = settlementDate.addMonths(120) swap9 = FinIborSwap(settlementDate, maturityDate, swapRate, payFixed, fixedFreqType, fixedDCCType) swaps.append(swap9) liborCurve = FinIborCurve(settlementDate, depos, fras, swaps) if 1 == 0: import numpy as np numSteps = 40 dt = 10 / numSteps times = np.linspace(0.0, 10.0, numSteps + 1) df0 = 1.0 for t in times[1:]: df1 = liborCurve.df(t) fwd = (df0 / df1 - 1.0) / dt print(t, df1, fwd) df0 = df1 return liborCurve
def buildFullIssuerCurve2(mktSpreadBump, irBump): # https://www.markit.com/markit.jsp?jsppage=pv.jsp # YIELD CURVE 20 August 2020 SNAP AT 1600 m = 1.0 settlementDate = FinDate(24, 8, 2020) dcType = FinDayCountTypes.ACT_360 depos = [] maturityDate = settlementDate.addMonths(1) depo1 = FinIborDeposit(settlementDate, maturityDate, m * 0.001709, dcType) maturityDate = settlementDate.addMonths(2) depo2 = FinIborDeposit(settlementDate, maturityDate, m * 0.002123, dcType) maturityDate = settlementDate.addMonths(3) depo3 = FinIborDeposit(settlementDate, maturityDate, m * 0.002469, dcType) maturityDate = settlementDate.addMonths(6) depo4 = FinIborDeposit(settlementDate, maturityDate, m * 0.003045, dcType) maturityDate = settlementDate.addMonths(12) depo5 = FinIborDeposit(settlementDate, maturityDate, m * 0.004449, dcType) depos.append(depo1) depos.append(depo2) depos.append(depo3) depos.append(depo4) depos.append(depo5) swaps = [] dcType = FinDayCountTypes.THIRTY_E_360_ISDA fixedFreq = FinFrequencyTypes.SEMI_ANNUAL maturityDate = settlementDate.addMonths(24) swap1 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.002155 + irBump, fixedFreq, dcType) swaps.append(swap1) maturityDate = settlementDate.addMonths(36) swap2 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.002305 + irBump, fixedFreq, dcType) swaps.append(swap2) maturityDate = settlementDate.addMonths(48) swap3 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.002665 + irBump, fixedFreq, dcType) swaps.append(swap3) maturityDate = settlementDate.addMonths(60) swap4 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.003290 + irBump, fixedFreq, dcType) swaps.append(swap4) liborCurve = FinIborCurve(settlementDate, depos, [], swaps) cdsCoupon = 0.01 + mktSpreadBump cdsMarketContracts = [] effectiveDate = FinDate(21, 8, 2020) cds = FinCDS(effectiveDate, "6M", cdsCoupon) cdsMarketContracts.append(cds) cds = FinCDS(effectiveDate, "1Y", cdsCoupon) cdsMarketContracts.append(cds) cds = FinCDS(effectiveDate, "2Y", cdsCoupon) cdsMarketContracts.append(cds) cds = FinCDS(effectiveDate, "3Y", cdsCoupon) cdsMarketContracts.append(cds) cds = FinCDS(effectiveDate, "4Y", cdsCoupon) cdsMarketContracts.append(cds) cds = FinCDS(effectiveDate, "5Y", cdsCoupon) cdsMarketContracts.append(cds) cds = FinCDS(effectiveDate, "7Y", cdsCoupon) cdsMarketContracts.append(cds) cds = FinCDS(effectiveDate, "10Y", cdsCoupon) cdsMarketContracts.append(cds) recoveryRate = 0.40 issuerCurve = FinCDSCurve(settlementDate, cdsMarketContracts, liborCurve, recoveryRate) testCases.header("DATE", "DISCOUNT_FACTOR", "SURV_PROB") years = np.linspace(0.0, 10.0, 20) dates = settlementDate.addYears(years) for dt in dates: df = liborCurve.df(dt) q = issuerCurve.survProb(dt) testCases.print("%16s" % dt, "%12.8f" % df, "%12.8f" % q) return liborCurve, issuerCurve
def buildFullIssuerCurve1(mktSpreadBump, irBump): # https://www.markit.com/markit.jsp?jsppage=pv.jsp # YIELD CURVE 8-AUG-2019 SNAP AT 1600 tradeDate = FinDate(2019, 8, 9) valuationDate = tradeDate.addDays(1) dcType = FinDayCountTypes.ACT_360 depos = [] m = 1.0 # 0.00000000000 spotDays = 2 settlementDate = valuationDate.addDays(spotDays) maturityDate = settlementDate.addMonths(1) depo1 = FinIborDeposit(settlementDate, maturityDate, m * 0.022009, dcType) maturityDate = settlementDate.addMonths(2) depo2 = FinIborDeposit(settlementDate, maturityDate, m * 0.022138, dcType) maturityDate = settlementDate.addMonths(3) depo3 = FinIborDeposit(settlementDate, maturityDate, m * 0.021810, dcType) maturityDate = settlementDate.addMonths(6) depo4 = FinIborDeposit(settlementDate, maturityDate, m * 0.020503, dcType) maturityDate = settlementDate.addMonths(12) depo5 = FinIborDeposit(settlementDate, maturityDate, m * 0.019930, dcType) depos.append(depo1) depos.append(depo2) depos.append(depo3) depos.append(depo4) depos.append(depo5) fras = [] swaps = [] dcType = FinDayCountTypes.THIRTY_E_360_ISDA fixedFreq = FinFrequencyTypes.SEMI_ANNUAL maturityDate = settlementDate.addMonths(24) swap1 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.015910 + irBump, fixedFreq, dcType) swaps.append(swap1) maturityDate = settlementDate.addMonths(36) swap2 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.014990 + irBump, fixedFreq, dcType) swaps.append(swap2) maturityDate = settlementDate.addMonths(48) swap3 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.014725 + irBump, fixedFreq, dcType) swaps.append(swap3) maturityDate = settlementDate.addMonths(60) swap4 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.014640 + irBump, fixedFreq, dcType) swaps.append(swap4) maturityDate = settlementDate.addMonths(72) swap5 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.014800 + irBump, fixedFreq, dcType) swaps.append(swap5) maturityDate = settlementDate.addMonths(84) swap6 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.014995 + irBump, fixedFreq, dcType) swaps.append(swap6) maturityDate = settlementDate.addMonths(96) swap7 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.015180 + irBump, fixedFreq, dcType) swaps.append(swap7) maturityDate = settlementDate.addMonths(108) swap8 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.015610 + irBump, fixedFreq, dcType) swaps.append(swap8) maturityDate = settlementDate.addMonths(120) swap9 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.015880 + irBump, fixedFreq, dcType) swaps.append(swap9) maturityDate = settlementDate.addMonths(144) swap10 = FinIborSwap(settlementDate, maturityDate, FinSwapTypes.PAYER, m * 0.016430 + irBump, fixedFreq, dcType) swaps.append(swap10) liborCurve = FinIborCurve(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) recoveryRate = 0.40 issuerCurve = FinCDSCurve(settlementDate, cdsMarketContracts, liborCurve, recoveryRate) return liborCurve, issuerCurve
def test_FinBondEmbeddedOptionMATLAB(): # https://fr.mathworks.com/help/fininst/optembndbybk.html # I FIND THAT THE PRICE CONVERGES TO 102.365 WHICH IS CLOSE TO 102.382 # FOUND BY MATLAB ALTHOUGH THEY DO NOT EXAMINE THE ASYMPTOTIC PRICE # WHICH MIGHT BE A BETTER MATCH - ALSO THEY DO NOT USE A REALISTIC VOL settlementDate = FinDate(1, 1, 2007) ########################################################################### swapType = FinSwapTypes.PAYER dcType = FinDayCountTypes.THIRTY_E_360 fixedFreq = FinFrequencyTypes.ANNUAL swap1 = FinIborSwap(settlementDate, "1Y", swapType, 0.0350, fixedFreq, dcType) swap2 = FinIborSwap(settlementDate, "2Y", swapType, 0.0400, fixedFreq, dcType) swap3 = FinIborSwap(settlementDate, "3Y", swapType, 0.0450, fixedFreq, dcType) swaps = [swap1, swap2, swap3] discountCurve = FinIborCurve(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("TIME", "NumTimeSteps", "BondWithOption", "BondPure") timeSteps = range(100, 200, 10) # 1000, 10) values = [] for numTimeSteps in timeSteps: model = FinModelRatesBK(sigma, a, numTimeSteps) start = time.time() v = puttableBond.value(settlementDate, discountCurve, model) end = time.time() period = end - start testCases.print(period, numTimeSteps, v['bondwithoption'], v['bondpure']) values.append(v['bondwithoption']) if plotGraphs: plt.figure() plt.plot(timeSteps, values)
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 swapType = FinSwapTypes.PAYER swapRate = 0.0058 swap = FinIborSwap(settlementDate, "1Y", swapType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0060 swap = FinIborSwap(settlementDate, "2Y", swapType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0072 swap = FinIborSwap(settlementDate, "3Y", swapType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0096 swap = FinIborSwap(settlementDate, "4Y", swapType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0124 swap = FinIborSwap(settlementDate, "5Y", swapType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0173 swap = FinIborSwap(settlementDate, "7Y", swapType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0219 swap = FinIborSwap(settlementDate, "10Y", swapType, swapRate, freqType, dayCountType) swaps.append(swap) swapRate = 0.0283 swap = FinIborSwap(settlementDate, "30Y", swapType, swapRate, freqType, dayCountType) swaps.append(swap) numRepeats = 10 start = time.time() for _ in range(0, numRepeats): _ = FinIborCurve(valuationDate, depos, fras, swaps, FinInterpTypes.FLAT_FORWARDS) end = time.time() elapsed1 = end - start start = time.time() for _ in range(0, numRepeats): _ = FinIborCurve(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)