예제 #1
0
def test_FinFXVanillaOptionBloombergExample():

    # Example Bloomberg Pricing at
    # https://stackoverflow.com/questions/48778712/fx-vanilla-call-price-in-quantlib-doesnt-match-bloomberg

    valuationDate = 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 = valuationDate.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 = FinIborSingleCurve(valuationDate, depos, fras, swaps)

    depos = []
    fras = []
    swaps = []
    depo = FinIborDeposit(settlementDate, maturityDate, forDepoRate,
                          FinDayCountTypes.ACT_360, notional, calendarType)
    depos.append(depo)
    forDiscountCurve = FinIborSingleCurve(valuationDate, depos, fras, swaps)

    model = FinFXModelBlackScholes(volatility)

    callOption = FinFXVanillaOption(expiryDate, strikeFXRate, currencyPair,
                                    FinOptionTypes.EUROPEAN_CALL, notional,
                                    notionalCurrency, 2)

    value = callOption.value(valuationDate, spotFXRate, domDiscountCurve,
                             forDiscountCurve, model)

    delta = callOption.delta(valuationDate, spotFXRate, domDiscountCurve,
                             forDiscountCurve, model)

    testCases.header("value", "delta")
    testCases.print(value, delta)
예제 #2
0
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)
예제 #3
0
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)
예제 #4
0
def test_FinIborDepositsAndSwaps(valuationDate):

    depoBasis = FinDayCountTypes.THIRTY_E_360_ISDA
    depos = []

    spotDays = 0
    settlementDate = valuationDate.addWeekDays(spotDays)
    depositRate = 0.05

    depo1 = FinIborDeposit(settlementDate, "1M", depositRate, depoBasis)
    depo2 = FinIborDeposit(settlementDate, "3M", depositRate, depoBasis)
    depo3 = FinIborDeposit(settlementDate, "6M", depositRate, depoBasis)

    depos.append(depo1)
    depos.append(depo2)
    depos.append(depo3)

    fras = []

    swaps = []
    fixedBasis = FinDayCountTypes.ACT_365F
    fixedFreq = FinFrequencyTypes.SEMI_ANNUAL
    fixedLegType = FinSwapTypes.PAY

    swapRate = 0.05
    swap1 = FinIborSwap(settlementDate, "1Y", fixedLegType, swapRate,
                        fixedFreq, fixedBasis)
    swap2 = FinIborSwap(settlementDate, "3Y", fixedLegType, swapRate,
                        fixedFreq, fixedBasis)
    swap3 = FinIborSwap(settlementDate, "5Y", fixedLegType, swapRate,
                        fixedFreq, fixedBasis)

    swaps.append(swap1)
    swaps.append(swap2)
    swaps.append(swap3)

    liborCurve = FinIborSingleCurve(valuationDate, depos, fras, swaps)

    return liborCurve
예제 #5
0
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)
예제 #6
0
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))
예제 #7
0
def test_FinOISDepositsFuturesSwaps():

    spotDate = FinDate(6, 6, 2018)
    spotDays = 0
    settleDt = spotDate.addWeekDays(spotDays)
    depoDCCType = FinDayCountTypes.THIRTY_E_360_ISDA

    depo = FinIborDeposit(settleDt, "1D", 1.712 / 100.0, depoDCCType)
    depos = [depo]

    fras = []

    fraRate = futureToFRARate(97.6675, -0.00005)
    frasettleDt = spotDate.nextIMMDate()
    fraMaturityDate = frasettleDt.nextIMMDate()
    fra = FinIborFRA(frasettleDt, fraMaturityDate, fraRate, depoDCCType)
    fras.append(fra)

    fraRate = futureToFRARate(97.5200, -0.00060)
    frasettleDt = fraMaturityDate
    fraMaturityDate = frasettleDt.nextIMMDate()
    fra = FinIborFRA(frasettleDt, fraMaturityDate, fraRate, depoDCCType)
    fras.append(fra)

    fraRate = futureToFRARate(97.3550, -0.00146)
    frasettleDt = fraMaturityDate
    fraMaturityDate = frasettleDt.nextIMMDate()
    fra = FinIborFRA(frasettleDt, fraMaturityDate, fraRate, depoDCCType)
    fras.append(fra)

    fraRate = futureToFRARate(97.2450, -0.00263)
    frasettleDt = fraMaturityDate
    fraMaturityDate = frasettleDt.nextIMMDate()
    fra = FinIborFRA(frasettleDt, fraMaturityDate, fraRate, depoDCCType)
    fras.append(fra)

    fraRate = futureToFRARate(97.1450, -0.00411)
    frasettleDt = fraMaturityDate
    fraMaturityDate = frasettleDt.nextIMMDate()
    fra = FinIborFRA(frasettleDt, fraMaturityDate, fraRate, depoDCCType)
    fras.append(fra)

    fraRate = futureToFRARate(97.0750, -0.00589)
    frasettleDt = frasettleDt.nextIMMDate()
    fraMaturityDate = frasettleDt.nextIMMDate()
    fra = FinIborFRA(frasettleDt, 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
    floatSpread = 0.0
    floatDCCType = FinDayCountTypes.ACT_360
    calendarType = FinCalendarTypes.US
    busDayAdjustRule = FinBusDayAdjustTypes.PRECEDING

    swapRate = 0.02776305
    paymentLag = 1

    swap = FinOIS(startDate, "2Y", fixedLegType, swapRate, fixedFreqType,
                  fixedDCCType, notional, paymentLag, floatSpread,
                  floatFreqType, floatDCCType, calendarType, busDayAdjustRule)

    swaps.append(swap)

    liborCurve = FinOISCurve(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 = settleDt
        df = liborCurve.df(endDate)
        print(endDate, df)

        endDate = FinDate(20, 6, 2018)
        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_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 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
    fixedLegType = FinSwapTypes.PAY
    interpType = FinInterpTypes.FLAT_FWD_RATES

    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, True)

    principal = 0.0
    testCases.banner("======================================================")
    testCases.banner("SINGLE CURVE VALUATION")
    testCases.header("LABEL", "VALUE")
    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.banner("======================================================")
    testCases.banner("SINGLE CURVE VALUATION TO SWAP SETTLEMENT DATE")
    testCases.header("LABEL", "VALUE")
    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))
    testCases.banner("======================================================")

    #    swaps[0].printFixedLegPV()
    #    swaps[0].printFloatLegPV()

    oisCurve = buildOIS(valuationDate)
    #    print(oisCurve)

    liborDualCurve = FinIborDualCurveOLD(valuationDate, oisCurve, depos, fras,
                                         swaps, FinInterpTypes.FLAT_FWD_RATES,
                                         True)
    #    print(liborDualCurve)

    # The valuation of 53714.55 is very close to the spreadsheet value 53713.96

    testCases.header("VALUATION TO TODAY DATE", " PV")
    testCases.print(
        "VALUE:", swaps[0].value(valuationDate, oisCurve, liborDualCurve,
                                 None))
    testCases.print("FIXED:", swaps[0].fixedLegValue(valuationDate, oisCurve))
    testCases.print(
        "FLOAT:", swaps[0].floatLegValue(valuationDate, oisCurve, liborCurve,
                                         None))

    testCases.header("VALUATION TO SWAP SETTLEMENT DATE", " PV")
    testCases.print(
        "VALUE:", swaps[0].value(settlementDate, oisCurve, liborDualCurve,
                                 None))
    testCases.print("FIXED:", swaps[0].fixedLegValue(settlementDate, oisCurve))
    testCases.print(
        "FLOAT:", swaps[0].floatLegValue(
            settlementDate,
            oisCurve,
            liborDualCurve,
            None,
        ))

    #    swaps[0].printFixedLegPV()
    #    swaps[0].printFloatLegPV()

    PLOT = False
    if PLOT is True:

        years = np.linspace(0, 5, 21)
        dates = settlementDate.addYears(years)

        singleCurveFwds = liborCurve.fwd(dates)
        plt.plot(years, singleCurveFwds, label="Single Libor Curve")

        oisCurveFwds = oisCurve.fwd(dates)
        plt.plot(years, oisCurveFwds, label="OIS Curve")

        indexCurveFwds = liborDualCurve.fwd(dates)
        plt.plot(years, indexCurveFwds, label="Libor Index Curve")

        plt.legend()
예제 #10
0
def test_FinIborSwaptionQLExample():

    valuationDate = FinDate(4, 3, 2014)
    settlementDate = FinDate(4, 3, 2014)

    depoDCCType = FinDayCountTypes.THIRTY_E_360_ISDA
    depos = []
    depo = FinIborDeposit(settlementDate, "1W", 0.0023, depoDCCType)
    depos.append(depo)
    depo = FinIborDeposit(settlementDate, "1M", 0.0023, depoDCCType)
    depos.append(depo)
    depo = FinIborDeposit(settlementDate, "3M", 0.0023, depoDCCType)
    depos.append(depo)
    depo = FinIborDeposit(settlementDate, "6M", 0.0023, depoDCCType)
    depos.append(depo)

    # No convexity correction provided so I omit interest rate futures

    swaps = []
    accType = FinDayCountTypes.ACT_365F
    fixedFreqType = FinFrequencyTypes.SEMI_ANNUAL
    fixedLegType = FinSwapTypes.PAY

    swap = FinIborSwap(settlementDate, "3Y", fixedLegType, 0.00790,
                       fixedFreqType, accType)
    swaps.append(swap)
    swap = FinIborSwap(settlementDate, "4Y", fixedLegType, 0.01200,
                       fixedFreqType, accType)
    swaps.append(swap)
    swap = FinIborSwap(settlementDate, "5Y", fixedLegType, 0.01570,
                       fixedFreqType, accType)
    swaps.append(swap)
    swap = FinIborSwap(settlementDate, "6Y", fixedLegType, 0.01865,
                       fixedFreqType, accType)
    swaps.append(swap)
    swap = FinIborSwap(settlementDate, "7Y", fixedLegType, 0.02160,
                       fixedFreqType, accType)
    swaps.append(swap)
    swap = FinIborSwap(settlementDate, "8Y", fixedLegType, 0.02350,
                       fixedFreqType, accType)
    swaps.append(swap)
    swap = FinIborSwap(settlementDate, "9Y", fixedLegType, 0.02540,
                       fixedFreqType, accType)
    swaps.append(swap)
    swap = FinIborSwap(settlementDate, "10Y", fixedLegType, 0.0273,
                       fixedFreqType, accType)
    swaps.append(swap)
    swap = FinIborSwap(settlementDate, "15Y", fixedLegType, 0.0297,
                       fixedFreqType, accType)
    swaps.append(swap)
    swap = FinIborSwap(settlementDate, "20Y", fixedLegType, 0.0316,
                       fixedFreqType, accType)
    swaps.append(swap)
    swap = FinIborSwap(settlementDate, "25Y", fixedLegType, 0.0335,
                       fixedFreqType, accType)
    swaps.append(swap)
    swap = FinIborSwap(settlementDate, "30Y", fixedLegType, 0.0354,
                       fixedFreqType, accType)
    swaps.append(swap)

    liborCurve = FinIborSingleCurve(valuationDate, depos, [], swaps,
                                    FinInterpTypes.LINEAR_ZERO_RATES)

    exerciseDate = settlementDate.addTenor("5Y")
    swapMaturityDate = exerciseDate.addTenor("5Y")
    swapFixedCoupon = 0.040852
    swapFixedFrequencyType = FinFrequencyTypes.SEMI_ANNUAL
    swapFixedDayCountType = FinDayCountTypes.THIRTY_E_360_ISDA
    swapFloatFrequencyType = FinFrequencyTypes.QUARTERLY
    swapFloatDayCountType = FinDayCountTypes.ACT_360
    swapNotional = 1000000
    swaptionType = FinSwapTypes.PAY

    swaption = FinIborSwaption(settlementDate, exerciseDate, swapMaturityDate,
                               swaptionType, swapFixedCoupon,
                               swapFixedFrequencyType, swapFixedDayCountType,
                               swapNotional, swapFloatFrequencyType,
                               swapFloatDayCountType)

    testCases.header("MODEL", "VALUE")

    model = FinModelBlack(0.1533)
    v = swaption.value(settlementDate, liborCurve, model)
    testCases.print(model.__class__, v)

    model = FinModelBlackShifted(0.1533, -0.008)
    v = swaption.value(settlementDate, liborCurve, model)
    testCases.print(model.__class__, v)

    model = FinModelSABR(0.132, 0.5, 0.5, 0.5)
    v = swaption.value(settlementDate, liborCurve, model)
    testCases.print(model.__class__, v)

    model = FinModelSABRShifted(0.352, 0.5, 0.15, 0.15, -0.005)
    v = swaption.value(settlementDate, liborCurve, model)
    testCases.print(model.__class__, v)

    model = FinModelRatesHW(0.010000000, 0.00000000001)
    v = swaption.value(settlementDate, liborCurve, model)
    testCases.print(model.__class__, v)
예제 #11
0
def buildIborCurve(valuationDate):

    depoDCCType = FinDayCountTypes.THIRTY_E_360_ISDA
    depos = []

    payFixed = FinSwapTypes.PAY

    spotDays = 2
    settlementDate = valuationDate.addWeekDays(spotDays)

    depositRate = 0.050
    maturityDate = settlementDate.addMonths(1)
    depo1 = FinIborDeposit(
        settlementDate,
        maturityDate,
        depositRate,
        depoDCCType)

    maturityDate = settlementDate.addMonths(3)
    depo2 = FinIborDeposit(
        settlementDate,
        maturityDate,
        depositRate,
        depoDCCType)

    maturityDate = settlementDate.addMonths(6)
    depo3 = FinIborDeposit(
        settlementDate,
        maturityDate,
        depositRate,
        depoDCCType)

    maturityDate = settlementDate.addMonths(9)
    depo4 = FinIborDeposit(
        settlementDate,
        maturityDate,
        depositRate,
        depoDCCType)

    maturityDate = settlementDate.addMonths(12)
    depo5 = FinIborDeposit(
        settlementDate,
        maturityDate,
        depositRate,
        depoDCCType)

    depos.append(depo1)
    depos.append(depo2)
    depos.append(depo3)
    depos.append(depo4)
    depos.append(depo5)

    fras = []
    fixedDCCType = FinDayCountTypes.ACT_365F
    fixedFreqType = FinFrequencyTypes.SEMI_ANNUAL

    swaps = []

    swapRate = 0.05
    maturityDate = settlementDate.addMonths(24)
    swap1 = FinIborSwap(
        settlementDate,
        maturityDate,
        swapRate,
        payFixed,
        fixedFreqType,
        fixedDCCType)
    swaps.append(swap1)

    maturityDate = settlementDate.addMonths(36)
    swap2 = FinIborSwap(
        settlementDate,
        maturityDate,
        swapRate,
        payFixed,
        fixedFreqType,
        fixedDCCType)
    swaps.append(swap2)

    maturityDate = settlementDate.addMonths(48)
    swap3 = FinIborSwap(
        settlementDate,
        maturityDate,
        swapRate,
        payFixed,
        fixedFreqType,
        fixedDCCType)
    swaps.append(swap3)

    maturityDate = settlementDate.addMonths(60)
    swap4 = FinIborSwap(
        settlementDate,
        maturityDate,
        swapRate,
        payFixed,
        fixedFreqType,
        fixedDCCType)
    swaps.append(swap4)

    maturityDate = settlementDate.addMonths(72)
    swap5 = FinIborSwap(
        settlementDate,
        maturityDate,
        swapRate,
        payFixed,
        fixedFreqType,
        fixedDCCType)
    swaps.append(swap5)

    maturityDate = settlementDate.addMonths(84)
    swap6 = FinIborSwap(
        settlementDate,
        maturityDate,
        swapRate,
        payFixed,
        fixedFreqType,
        fixedDCCType)
    swaps.append(swap6)

    maturityDate = settlementDate.addMonths(96)
    swap7 = FinIborSwap(
        settlementDate,
        maturityDate,
        swapRate,
        payFixed,
        fixedFreqType,
        fixedDCCType)
    swaps.append(swap7)

    maturityDate = settlementDate.addMonths(108)
    swap8 = FinIborSwap(
        settlementDate,
        maturityDate,
        swapRate,
        payFixed,
        fixedFreqType,
        fixedDCCType)
    swaps.append(swap8)

    maturityDate = settlementDate.addMonths(120)
    swap9 = FinIborSwap(
        settlementDate,
        maturityDate,
        swapRate,
        payFixed,
        fixedFreqType,
        fixedDCCType)
    swaps.append(swap9)

    liborCurve = FinIborSingleCurve(valuationDate,
                                    depos,
                                    fras,
                                    swaps)
    
    if 1 == 0:
        import numpy as np
        numSteps = 40
        dt = 10 / numSteps
        times = np.linspace(0.0, 10.0, numSteps + 1)

        df0 = 1.0
        for t in times[1:]:
            df1 = liborCurve.df(t)
            fwd = (df0 / df1 - 1.0) / dt
            print(t, df1, fwd)
            df0 = df1

    return liborCurve
예제 #12
0
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)
예제 #13
0
def test_bloombergPricingExample(interpType):

    ''' This is an example of a replication of a BBG example from
    https://github.com/vilen22/curve-building/blob/master/Bloomberg%20Curve%20Building%20Replication.xlsx

    '''
    valuationDate = FinDate(6, 6, 2018)

    # We do the O/N rate which settles on trade date
    spotDays = 0
    settlementDate = valuationDate.addWeekDays(spotDays)
    depoDCCType = FinDayCountTypes.ACT_360
    depos = []
    depositRate = 0.0231381
    maturityDate = settlementDate.addMonths(3)
    depo = FinIborDeposit(settlementDate, maturityDate, depositRate,
                           depoDCCType)
    depos.append(depo)

    futs = []
    fut = FinIborFuture(valuationDate, 1); futs.append(fut)
    fut = FinIborFuture(valuationDate, 2); futs.append(fut)
    fut = FinIborFuture(valuationDate, 3); futs.append(fut)
    fut = FinIborFuture(valuationDate, 4); futs.append(fut)
    fut = FinIborFuture(valuationDate, 5); futs.append(fut)
    fut = FinIborFuture(valuationDate, 6); futs.append(fut)

    fras = [None]*6
    fras[0] = futs[0].toFRA(97.6675, -0.00005)
    fras[1] = futs[1].toFRA(97.5200, -0.00060)
    fras[2] = futs[2].toFRA(97.3550, -0.00146)
    fras[3] = futs[3].toFRA(97.2450, -0.00263)
    fras[4] = futs[4].toFRA(97.1450, -0.00411)
    fras[5] = futs[5].toFRA(97.0750, -0.00589)

    accrual = FinDayCountTypes.THIRTY_E_360
    freq = FinFrequencyTypes.SEMI_ANNUAL

    spotDays = 2
    settlementDate = valuationDate.addWeekDays(spotDays)
    notional = ONE_MILLION
    fixedLegType = FinSwapTypes.PAY

    swaps = []
    swap = FinIborSwap(settlementDate, "2Y", fixedLegType, (2.77417+2.77844)/200, freq, accrual); swaps.append(swap)
    swap = FinIborSwap(settlementDate, "3Y", fixedLegType, (2.86098+2.86582)/200, freq, accrual); swaps.append(swap)
    swap = FinIborSwap(settlementDate, "4Y", fixedLegType, (2.90240+2.90620)/200, freq, accrual); swaps.append(swap)
    swap = FinIborSwap(settlementDate, "5Y", fixedLegType, (2.92944+2.92906)/200, freq, accrual); swaps.append(swap)
    swap = FinIborSwap(settlementDate, "6Y", fixedLegType, (2.94001+2.94499)/200, freq, accrual); swaps.append(swap)
    swap = FinIborSwap(settlementDate, "7Y", fixedLegType, (2.95352+2.95998)/200, freq, accrual); swaps.append(swap)
    swap = FinIborSwap(settlementDate, "8Y", fixedLegType, (2.96830+2.97400)/200, freq, accrual); swaps.append(swap)
    swap = FinIborSwap(settlementDate, "9Y", fixedLegType, (2.98403+2.98817)/200, freq, accrual); swaps.append(swap)
    swap = FinIborSwap(settlementDate, "10Y", fixedLegType, (2.99716+3.00394)/200, freq, accrual); swaps.append(swap)
    swap = FinIborSwap(settlementDate, "11Y", fixedLegType, (3.01344+3.01596)/200, freq, accrual); swaps.append(swap)
    swap = FinIborSwap(settlementDate, "12Y", fixedLegType, (3.02276+3.02684)/200, freq, accrual); swaps.append(swap)
    swap = FinIborSwap(settlementDate, "15Y", fixedLegType, (3.04092+3.04508)/200, freq, accrual); swaps.append(swap)
    swap = FinIborSwap(settlementDate, "20Y", fixedLegType, (3.04417+3.05183)/200, freq, accrual); swaps.append(swap)
    swap = FinIborSwap(settlementDate, "25Y", fixedLegType, (3.03219+3.03621)/200, freq, accrual); swaps.append(swap)
    swap = FinIborSwap(settlementDate, "30Y", fixedLegType, (3.01030+3.01370)/200, freq, accrual); swaps.append(swap)
    swap = FinIborSwap(settlementDate, "40Y", fixedLegType, (2.96946+2.97354)/200, freq, accrual); swaps.append(swap)
    swap = FinIborSwap(settlementDate, "50Y", fixedLegType, (2.91552+2.93748)/200, freq, accrual); swaps.append(swap)

    liborCurve = FinIborSingleCurve(valuationDate, depos, fras, swaps, interpType)

    # The valuation of 53714.55 is very close to the spreadsheet value 53713.96
    principal = 0.0

    # Pay fixed so make fixed leg value negative
    testCases.header("VALUATION TO TODAY DATE"," PV")
    testCases.print("VALUE:", swaps[0].value(valuationDate, liborCurve, liborCurve, None))
    testCases.print("FIXED:", -swaps[0]._fixedLeg.value(valuationDate, liborCurve))
    testCases.print("FLOAT:", swaps[0]._floatLeg.value(valuationDate, liborCurve, liborCurve, None))

    # Pay fixed so make fixed leg value negative
    testCases.header("VALUATION TO SWAP SETTLEMENT DATE"," PV")
    testCases.print("VALUE:", swaps[0].value(settlementDate, liborCurve, liborCurve, None))
    testCases.print("FIXED:", -swaps[0]._fixedLeg.value(settlementDate, liborCurve))
    testCases.print("FLOAT:", swaps[0]._floatLeg.value(settlementDate, liborCurve, liborCurve, None))

    # swaps[0].printFixedLegPV()
    # swaps[0].printFloatLegPV()

    if 1==0:
        plt.figure()
    
        years = np.linspace(0, 50, 500)    
        dates = settlementDate.addYears(years)
        fwds = liborCurve.fwd(dates)
        plt.plot(years, fwds, label = "Fwd Rate")
        plt.title(interpType)
        plt.xlabel("Years")
        plt.legend()
    
        years = np.linspace(0, 50, 500)    
        dates = settlementDate.addYears(years)
        fwds = liborCurve.zeroRate(dates)
        plt.plot(years, fwds, label = "Zero Rate")
        plt.title(interpType)
        plt.xlabel("Years")
        plt.ylabel("Rate")
        plt.legend()
예제 #14
0
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
예제 #15
0
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
예제 #16
0
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
예제 #17
0
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
예제 #18
0
def testFinIborCashSettledSwaption():

    testCases.header("LABEL", "VALUE")

    valuationDate = FinDate(1, 1, 2020)
    settlementDate = FinDate(1, 1, 2020)

    depoDCCType = FinDayCountTypes.THIRTY_E_360_ISDA
    depos = []
    depo = FinIborDeposit(settlementDate, "1W", 0.0023, depoDCCType)
    depos.append(depo)
    depo = FinIborDeposit(settlementDate, "1M", 0.0023, depoDCCType)
    depos.append(depo)
    depo = FinIborDeposit(settlementDate, "3M", 0.0023, depoDCCType)
    depos.append(depo)
    depo = FinIborDeposit(settlementDate, "6M", 0.0023, depoDCCType)
    depos.append(depo)

    # No convexity correction provided so I omit interest rate futures

    settlementDate = FinDate(2, 1, 2020)

    swaps = []
    accType = FinDayCountTypes.ACT_365F
    fixedFreqType = FinFrequencyTypes.SEMI_ANNUAL
    fixedLegType = FinSwapTypes.PAY

    swap = FinIborSwap(settlementDate, "3Y", fixedLegType, 0.00790,
                       fixedFreqType, accType)
    swaps.append(swap)
    swap = FinIborSwap(settlementDate, "4Y", fixedLegType, 0.01200,
                       fixedFreqType, accType)
    swaps.append(swap)
    swap = FinIborSwap(settlementDate, "5Y", fixedLegType, 0.01570,
                       fixedFreqType, accType)
    swaps.append(swap)
    swap = FinIborSwap(settlementDate, "6Y", fixedLegType, 0.01865,
                       fixedFreqType, accType)
    swaps.append(swap)
    swap = FinIborSwap(settlementDate, "7Y", fixedLegType, 0.02160,
                       fixedFreqType, accType)
    swaps.append(swap)
    swap = FinIborSwap(settlementDate, "8Y", fixedLegType, 0.02350,
                       fixedFreqType, accType)
    swaps.append(swap)
    swap = FinIborSwap(settlementDate, "9Y", fixedLegType, 0.02540,
                       fixedFreqType, accType)
    swaps.append(swap)
    swap = FinIborSwap(settlementDate, "10Y", fixedLegType, 0.0273,
                       fixedFreqType, accType)
    swaps.append(swap)
    swap = FinIborSwap(settlementDate, "15Y", fixedLegType, 0.0297,
                       fixedFreqType, accType)
    swaps.append(swap)
    swap = FinIborSwap(settlementDate, "20Y", fixedLegType, 0.0316,
                       fixedFreqType, accType)
    swaps.append(swap)
    swap = FinIborSwap(settlementDate, "25Y", fixedLegType, 0.0335,
                       fixedFreqType, accType)
    swaps.append(swap)
    swap = FinIborSwap(settlementDate, "30Y", fixedLegType, 0.0354,
                       fixedFreqType, accType)
    swaps.append(swap)

    liborCurve = FinIborSingleCurve(valuationDate, depos, [], swaps,
                                    FinInterpTypes.LINEAR_ZERO_RATES)

    exerciseDate = settlementDate.addTenor("5Y")
    swapMaturityDate = exerciseDate.addTenor("5Y")
    swapFixedCoupon = 0.040852
    swapFixedFrequencyType = FinFrequencyTypes.SEMI_ANNUAL
    swapFixedDayCountType = FinDayCountTypes.THIRTY_E_360_ISDA
    swapFloatFrequencyType = FinFrequencyTypes.QUARTERLY
    swapFloatDayCountType = FinDayCountTypes.ACT_360
    swapNotional = 1000000
    fixedLegType = FinSwapTypes.PAY

    swaption = FinIborSwaption(settlementDate, exerciseDate, swapMaturityDate,
                               fixedLegType, swapFixedCoupon,
                               swapFixedFrequencyType, swapFixedDayCountType,
                               swapNotional, swapFloatFrequencyType,
                               swapFloatDayCountType)

    model = FinModelBlack(0.1533)
    v = swaption.value(settlementDate, liborCurve, model)
    testCases.print("Swaption No-Arb Value:", v)

    fwdSwapRate1 = liborCurve.swapRate(exerciseDate, swapMaturityDate,
                                       swapFixedFrequencyType,
                                       swapFixedDayCountType)

    testCases.print("Curve Fwd Swap Rate:", fwdSwapRate1)

    fwdSwap = FinIborSwap(exerciseDate, swapMaturityDate, fixedLegType,
                          swapFixedCoupon, swapFixedFrequencyType,
                          swapFixedDayCountType)

    fwdSwapRate2 = fwdSwap.swapRate(settlementDate, liborCurve)
    testCases.print("Fwd Swap Swap Rate:", fwdSwapRate2)

    model = FinModelBlack(0.1533)

    v = swaption.cashSettledValue(valuationDate, liborCurve, fwdSwapRate2,
                                  model)

    testCases.print("Swaption Cash Settled Value:", v)