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

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

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

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

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

    accrual = FinDayCountTypes.THIRTY_E_360
    freq = FinFrequencyTypes.SEMI_ANNUAL

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

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

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

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

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

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

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

    if 1 == 0:
        plt.figure()

        years = np.linspace(0, 50, 500)
        dates = settlementDate.addYears(years)
        fwds = liborCurve.fwd(dates)
        plt.plot(years, fwds, label="Fwd Rate")
        plt.title(interpType)
        plt.xlabel("Years")
        plt.legend()

        years = np.linspace(0, 50, 500)
        dates = settlementDate.addYears(years)
        fwds = liborCurve.zeroRate(dates)
        plt.plot(years, fwds, label="Zero Rate")
        plt.title(interpType)
        plt.xlabel("Years")
        plt.ylabel("Rate")
        plt.legend()
def test_derivativePricingExample():

    valuationDate = FinDate(10, 11, 2011)

    dccType = FinDayCountTypes.ACT_360
    depos = []

    # We do the O/N rate which settles on trade date
    spotDays = 0
    settlementDate = valuationDate.addWeekDays(spotDays)

    depositRate = 0.001410
    depo = FinIborDeposit(settlementDate, "ON", depositRate, dccType)
    depos.append(depo)

    spotDays = 1
    settlementDate = valuationDate.addWeekDays(spotDays)

    depositRate = 0.001410
    depo = FinIborDeposit(settlementDate, "TN", depositRate, dccType)
    depos.append(depo)

    spotDays = 2
    settlementDate = valuationDate.addWeekDays(spotDays)

    depositRate = 0.001910
    depo = FinIborDeposit(settlementDate, "1W", depositRate, dccType)
    depos.append(depo)

    depositRate = 0.002090
    depo = FinIborDeposit(settlementDate, "2W", depositRate, dccType)
    depos.append(depo)

    depositRate = 0.002490
    depo = FinIborDeposit(settlementDate, "1M", depositRate, dccType)
    depos.append(depo)

    depositRate = 0.003450
    depo = FinIborDeposit(settlementDate, "2M", depositRate, dccType)
    depos.append(depo)

    depositRate = 0.004570
    depo = FinIborDeposit(settlementDate, "3M", depositRate, dccType)
    depos.append(depo)

    depositRate = 0.005230
    depo = FinIborDeposit(settlementDate, "4M", depositRate, dccType)
    depos.append(depo)

    depositRate = 0.005860
    depo = FinIborDeposit(settlementDate, "5M", depositRate, dccType)
    depos.append(depo)

    depositRate = 0.006540
    depo = FinIborDeposit(settlementDate, "6M", depositRate, dccType)
    depos.append(depo)

    depositRate = 0.007080
    depo = FinIborDeposit(settlementDate, "7M", depositRate, dccType)
    depos.append(depo)

    depositRate = 0.007540
    depo = FinIborDeposit(settlementDate, "8M", depositRate, dccType)
    depos.append(depo)

    depositRate = 0.008080
    depo = FinIborDeposit(settlementDate, "9M", depositRate, dccType)
    depos.append(depo)

    depositRate = 0.008570
    depo = FinIborDeposit(settlementDate, "10M", depositRate, dccType)
    depos.append(depo)

    depositRate = 0.009130
    depo = FinIborDeposit(settlementDate, "11M", depositRate, dccType)
    depos.append(depo)

    fras = []

    swaps = []
    dayCountType = FinDayCountTypes.THIRTY_E_360_ISDA
    #    dayCountType = FinDayCountTypes.ACT_360
    freqType = FinFrequencyTypes.SEMI_ANNUAL
    fixedLegType = FinSwapTypes.PAY

    swapRate = 0.0058
    swap = FinIborSwapOLD(settlementDate, "1Y", fixedLegType, swapRate,
                          freqType, dayCountType)
    swaps.append(swap)

    swapRate = 0.0060
    swap = FinIborSwapOLD(settlementDate, "2Y", fixedLegType, swapRate,
                          freqType, dayCountType)
    swaps.append(swap)

    swapRate = 0.0072
    swap = FinIborSwapOLD(settlementDate, "3Y", fixedLegType, swapRate,
                          freqType, dayCountType)
    swaps.append(swap)

    swapRate = 0.0096
    swap = FinIborSwapOLD(settlementDate, "4Y", fixedLegType, swapRate,
                          freqType, dayCountType)
    swaps.append(swap)

    swapRate = 0.0124
    swap = FinIborSwapOLD(settlementDate, "5Y", fixedLegType, swapRate,
                          freqType, dayCountType)
    swaps.append(swap)

    swapRate = 0.0173
    swap = FinIborSwapOLD(settlementDate, "7Y", fixedLegType, swapRate,
                          freqType, dayCountType)
    swaps.append(swap)

    swapRate = 0.0219
    swap = FinIborSwapOLD(settlementDate, "10Y", fixedLegType, swapRate,
                          freqType, dayCountType)
    swaps.append(swap)

    swapRate = 0.0283
    swap = FinIborSwapOLD(settlementDate, "30Y", fixedLegType, swapRate,
                          freqType, dayCountType)
    swaps.append(swap)

    numRepeats = 10
    start = time.time()

    for _ in range(0, numRepeats):
        _ = FinIborSingleCurveOLD(valuationDate, depos, fras, swaps,
                                  FinInterpTypes.FLAT_FWD_RATES)

    end = time.time()
    elapsed1 = end - start

    start = time.time()

    for _ in range(0, numRepeats):
        _ = FinIborSingleCurveOLD(valuationDate, depos, fras, swaps,
                                  FinInterpTypes.FLAT_FWD_RATES)

    end = time.time()
    elapsed2 = end - start

    testCases.header("METHOD", "TIME")
    testCases.print("NON-LINEAR SOLVER BOOTSTRAP", elapsed1 / numRepeats)
    testCases.print("LINEAR SWAP BOOTSTRAP", elapsed2 / numRepeats)
def test_FinIborDepositsFuturesSwaps():

    spotDate = FinDate(6, 6, 2018)
    spotDays = 0
    settlementDate = spotDate.addWeekDays(spotDays)
    depoDCCType = FinDayCountTypes.ACT_360
    depos = []
    depositRate = 0.0231381
    depo = FinIborDeposit(settlementDate, "3M", depositRate, depoDCCType)
    depos.append(depo)

    depositRate = 0.027
    depo = FinIborDeposit(settlementDate, "3M", depositRate, depoDCCType)
    depos.append(depo)

    depos = []
    depo = FinIborDeposit(settlementDate, "1M", 0.0230, depoDCCType)
    depos.append(depo)
    depo = FinIborDeposit(settlementDate, "2M", 0.0235, depoDCCType)
    depos.append(depo)
    depo = FinIborDeposit(settlementDate, "3M", 0.0240, depoDCCType)
    depos.append(depo)

    fras = []

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

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

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

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

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

    fraRate = futureToFRARate(97.0750, -0.00589)
    fraSettlementDate = fraSettlementDate.nextIMMDate()
    fraMaturityDate = fraSettlementDate.nextIMMDate()
    fra = FinIborFRA(fraSettlementDate, fraMaturityDate, fraRate, depoDCCType)
    fras.append(fra)

    ###########################################################################

    spotDays = 2
    startDate = spotDate.addWeekDays(spotDays)

    swaps = []
    fixedLegType = FinSwapTypes.PAY
    fixedDCCType = FinDayCountTypes.THIRTY_E_360
    fixedFreqType = FinFrequencyTypes.SEMI_ANNUAL
    floatFreqType = FinFrequencyTypes.QUARTERLY
    notional = 1000000
    principal = 0.0
    floatSpread = 0.0
    floatDCCType = FinDayCountTypes.ACT_360
    calendarType = FinCalendarTypes.US
    busDayAdjustRule = FinBusDayAdjustTypes.PRECEDING

    swapRate = 0.02776305

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

    swaps.append(swap)

    liborCurve = FinIborSingleCurveOLD(spotDate, depos, fras, swaps)

    times = np.linspace(0.0, 2.0, 25)
    dates = spotDate.addYears(times)
    zeroRates = liborCurve.zeroRate(dates)
    fwdRates = liborCurve.fwd(dates)

    if PLOT_GRAPHS:
        plt.figure(figsize=(8, 6))
        plt.plot(times, zeroRates * 100, label="zero rates")
        plt.plot(times, fwdRates * 100, label="fwd rates")
        plt.xlabel("Times")
        plt.ylabel("CC forward rates")
        plt.legend()

        print("==============================================================")
        for fra in fras:
            print(fra)
        print("==============================================================")

        endDate = spotDate
        df = liborCurve.df(endDate)
        print(endDate, df)

        endDate = settlementDate
        df = liborCurve.df(endDate)
        print(endDate, df)

        endDate = FinDate(20, 6, 2018)
        df = liborCurve.df(endDate)
        print(endDate, df)

        for depo in depos:
            endDate = depo._maturityDate
            df = liborCurve.df(endDate)
            print(endDate, df)

        for fra in fras:
            endDate = fra._maturityDate
            df = liborCurve.df(endDate)
            print(endDate, df)

        for swap in swaps:
            endDate = swap._maturityDate
            df = liborCurve.df(endDate)
            print(endDate, df)

        swap.printFixedLegPV(spotDate)
        swap.printFloatLegPV(spotDate)
def test_FinIborDepositsFRAsSwaps():

    valuationDate = FinDate(18, 9, 2019)

    dccType = FinDayCountTypes.THIRTY_E_360_ISDA
    depos = []

    spotDays = 0
    settlementDate = valuationDate.addWeekDays(spotDays)

    depositRate = 0.050
    maturityDate = settlementDate.addMonths(1)
    depo = FinIborDeposit(settlementDate, maturityDate, depositRate, dccType)
    depos.append(depo)

    maturityDate = settlementDate.addMonths(2)
    depo = FinIborDeposit(settlementDate, maturityDate, depositRate, dccType)
    depos.append(depo)

    maturityDate = settlementDate.addMonths(3)
    depo = FinIborDeposit(settlementDate, maturityDate, depositRate, dccType)
    depos.append(depo)

    maturityDate = settlementDate.addMonths(6)
    depo = FinIborDeposit(settlementDate, maturityDate, depositRate, dccType)
    depos.append(depo)

    maturityDate = settlementDate.addMonths(9)
    depo = FinIborDeposit(settlementDate, maturityDate, depositRate, dccType)
    depos.append(depo)

    maturityDate = settlementDate.addMonths(12)
    depo = FinIborDeposit(settlementDate, maturityDate, depositRate, dccType)
    depos.append(depo)

    fras = []
    # 1 x 4 FRA
    fraRate = 0.04
    fraSettlementDate = settlementDate.addMonths(9)
    fraMaturityDate = settlementDate.addMonths(13)
    fra = FinIborFRA(fraSettlementDate, fraMaturityDate, fraRate, dccType)
    fras.append(fra)

    # 4 x 7 FRA
    fraRate = 0.03
    fraSettlementDate = settlementDate.addMonths(13)
    fraMaturityDate = settlementDate.addMonths(17)
    fra = FinIborFRA(fraSettlementDate, fraMaturityDate, fraRate, dccType)
    fras.append(fra)

    # 4 x 7 FRA
    fraRate = 0.07
    fraSettlementDate = settlementDate.addMonths(17)
    fraMaturityDate = settlementDate.addMonths(21)
    fra = FinIborFRA(fraSettlementDate, fraMaturityDate, fraRate, dccType)
    fras.append(fra)

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

    swapRate = 0.05
    #    maturityDate = settlementDate.addMonths(24)
    #    swap = FinIborSwapOLD(settlementDate, maturityDate, swapRate, fixedFreqType,
    #                        fixedDCCType)
    #    swaps.append(swap)

    fixedLegType = FinSwapTypes.PAY
    maturityDate = settlementDate.addMonths(36)
    swap = FinIborSwapOLD(settlementDate, maturityDate, fixedLegType, swapRate,
                          fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturityDate = settlementDate.addMonths(48)
    swap = FinIborSwapOLD(settlementDate, maturityDate, fixedLegType, swapRate,
                          fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturityDate = settlementDate.addMonths(60)
    swap = FinIborSwapOLD(settlementDate, maturityDate, fixedLegType, swapRate,
                          fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturityDate = settlementDate.addMonths(72)
    swap = FinIborSwapOLD(settlementDate, maturityDate, fixedLegType, swapRate,
                          fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturityDate = settlementDate.addMonths(84)
    swap = FinIborSwapOLD(settlementDate, maturityDate, fixedLegType, swapRate,
                          fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturityDate = settlementDate.addMonths(96)
    swap = FinIborSwapOLD(settlementDate, maturityDate, fixedLegType, swapRate,
                          fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturityDate = settlementDate.addMonths(108)
    swap = FinIborSwapOLD(settlementDate, maturityDate, fixedLegType, swapRate,
                          fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturityDate = settlementDate.addMonths(120)
    swap = FinIborSwapOLD(settlementDate, maturityDate, fixedLegType, swapRate,
                          fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturityDate = settlementDate.addMonths(132)
    swap = FinIborSwapOLD(settlementDate, maturityDate, fixedLegType, swapRate,
                          fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturityDate = settlementDate.addMonths(144)
    swap = FinIborSwapOLD(settlementDate, maturityDate, fixedLegType, swapRate,
                          fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturityDate = settlementDate.addMonths(180)
    swap = FinIborSwapOLD(settlementDate, maturityDate, fixedLegType, swapRate,
                          fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturityDate = settlementDate.addMonths(240)
    swap = FinIborSwapOLD(settlementDate, maturityDate, fixedLegType, swapRate,
                          fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturityDate = settlementDate.addMonths(300)
    swap = FinIborSwapOLD(settlementDate, maturityDate, fixedLegType, swapRate,
                          fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturityDate = settlementDate.addMonths(360)
    swap = FinIborSwapOLD(settlementDate, maturityDate, fixedLegType, swapRate,
                          fixedFreqType, fixedDCCType)
    swaps.append(swap)

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

    df = liborCurve.df(settlementDate)

    testCases.header("SETTLEMENT DATE", "DF")
    testCases.print(str(settlementDate), df)
    testCases.header("DATE", "DF")

    for deposit in depos:
        df = liborCurve.df(deposit._maturityDate)
        testCases.print(str(deposit._maturityDate), df)

    for swap in swaps:
        df = liborCurve.df(swap._maturityDate)
        testCases.print(str(swap._maturityDate), df)
Exemple #5
0
def test_swapValuationExample():
    
    # Example from
    # https://blog.deriscope.com/index.php/en/excel-interest-rate-swap-price-dual-bootstrapping-curve
    
    vBloomberg = 388147

    valuation_date = Date(30, 11, 2018)

    start_date = Date(27, 12, 2017)
    maturity_date = Date(27, 12, 2067)
    notional = 10 * ONE_MILLION
    fixed_legType = FinSwapTypes.RECEIVE
    
    fixedRate = 0.0150
    fixedDCCType = DayCountTypes.THIRTY_360_BOND
    fixedFreqType = FrequencyTypes.ANNUAL
    
    floatSpread = 0.0
    floatDCCType = DayCountTypes.ACT_360
    floatFreqType = FrequencyTypes.SEMI_ANNUAL

    offMarketSwap = FinIborSwapOLD(start_date, maturity_date, fixed_legType,
                                fixedRate, fixedFreqType, fixedDCCType,
                                notional,
                                floatSpread, floatFreqType, floatDCCType)
    
    interp_type = FinInterpTypes.LINEAR_ZERO_RATES
    
    depoDCCType = DayCountTypes.ACT_360
    depos = []
    
    ###########################################################################
    # MARKET
    ###########################################################################
    
    spotDays = 0
    settlement_date = valuation_date.addWeekDays(spotDays)
    depo = FinIborDeposit(settlement_date, "6M", -0.2510/100.0, depoDCCType); depos.append(depo)
    
    fras = []
    fraDCCType = DayCountTypes.ACT_360
    
    fra = FinIborFRA(settlement_date.addTenor("1M"), "6M", -0.2450/100.0, fraDCCType); fras.append(fra)
    fra = FinIborFRA(settlement_date.addTenor("2M"), "6M", -0.2435/100.0, fraDCCType); fras.append(fra)
    fra = FinIborFRA(settlement_date.addTenor("3M"), "6M", -0.2400/100.0, fraDCCType); fras.append(fra)
    fra = FinIborFRA(settlement_date.addTenor("4M"), "6M", -0.2360/100.0, fraDCCType); fras.append(fra)
    fra = FinIborFRA(settlement_date.addTenor("5M"), "6M", -0.2285/100.0, fraDCCType); fras.append(fra)
    fra = FinIborFRA(settlement_date.addTenor("6M"), "6M", -0.2230/100.0, fraDCCType); fras.append(fra)
    fra = FinIborFRA(settlement_date.addTenor("7M"), "6M", -0.2110/100.0, fraDCCType); fras.append(fra)
    fra = FinIborFRA(settlement_date.addTenor("8M"), "6M", -0.1990/100.0, fraDCCType); fras.append(fra)
    fra = FinIborFRA(settlement_date.addTenor("9M"), "6M", -0.1850/100.0, fraDCCType); fras.append(fra)
    fra = FinIborFRA(settlement_date.addTenor("10M"), "6M", -0.1680/100.0, fraDCCType); fras.append(fra)
    fra = FinIborFRA(settlement_date.addTenor("11M"), "6M", -0.1510/100.0, fraDCCType); fras.append(fra)
    fra = FinIborFRA(settlement_date.addTenor("12M"), "6M", -0.1360/100.0, fraDCCType); fras.append(fra)
    
    swaps = []
    fixed_legType = FinSwapTypes.PAY
    fixedDCCType = DayCountTypes.THIRTY_360_BOND
    fixedFreqType = FrequencyTypes.ANNUAL
    
    swap = FinIborSwapOLD(settlement_date, "2Y", fixed_legType, -0.1525/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinIborSwapOLD(settlement_date, "3Y", fixed_legType, -0.0185/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinIborSwapOLD(settlement_date, "4Y", fixed_legType, 0.1315/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinIborSwapOLD(settlement_date, "5Y", fixed_legType, 0.2745/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinIborSwapOLD(settlement_date, "6Y", fixed_legType, 0.4135/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinIborSwapOLD(settlement_date, "7Y", fixed_legType, 0.5439/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinIborSwapOLD(settlement_date, "8Y", fixed_legType, 0.6652/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinIborSwapOLD(settlement_date, "9Y", fixed_legType, 0.7784/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinIborSwapOLD(settlement_date, "10Y", fixed_legType, 0.8799/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinIborSwapOLD(settlement_date, "11Y", fixed_legType, 0.9715/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinIborSwapOLD(settlement_date, "12Y", fixed_legType, 1.0517/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinIborSwapOLD(settlement_date, "15Y", fixed_legType, 1.2369/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinIborSwapOLD(settlement_date, "20Y", fixed_legType, 1.3965/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinIborSwapOLD(settlement_date, "25Y", fixed_legType, 1.4472/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinIborSwapOLD(settlement_date, "30Y", fixed_legType, 1.4585/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinIborSwapOLD(settlement_date, "35Y", fixed_legType, 1.4595/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinIborSwapOLD(settlement_date, "40Y", fixed_legType, 1.4535/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinIborSwapOLD(settlement_date, "45Y", fixed_legType, 1.4410/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinIborSwapOLD(settlement_date, "50Y", fixed_legType, 1.4335/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    
    iborDepos = depos.copy()
    iborFras = fras.copy()
    iborSwaps = swaps.copy()
    
    iborCurve = FinIborSingleCurve(valuation_date, iborDepos, iborFras, iborSwaps, interp_type)
    v1 = offMarketSwap.value(valuation_date, 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 = DayCountTypes.ACT_360
    depos = []
    
    spotDays = 0
    settlement_date = valuation_date.addWeekDays(spotDays)
    depo = FinIborDeposit(settlement_date, "1D", -0.3490/100.0, depoDCCType); depos.append(depo)
    
    fras = []
    
    swaps = []
    fixed_legType = FinSwapTypes.PAY
    fixedDCCType = DayCountTypes.ACT_365F
    fixedFreqType = FrequencyTypes.ANNUAL
    
    # Standard OIS with standard annual terms
    swap = FinOIS(settlement_date, "2W", fixed_legType, -0.3600/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinOIS(settlement_date, "1M", fixed_legType, -0.3560/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinOIS(settlement_date, "2M", fixed_legType, -0.3570/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinOIS(settlement_date, "3M", fixed_legType, -0.3580/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinOIS(settlement_date, "4M", fixed_legType, -0.3575/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinOIS(settlement_date, "5M", fixed_legType, -0.3578/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinOIS(settlement_date, "6M", fixed_legType, -0.3580/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinOIS(settlement_date, "7M", fixed_legType, -0.3600/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinOIS(settlement_date, "8M", fixed_legType, -0.3575/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinOIS(settlement_date, "9M", fixed_legType, -0.3569/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinOIS(settlement_date, "10M", fixed_legType, -0.3553/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinOIS(settlement_date, "11M", fixed_legType, -0.3534/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinOIS(settlement_date, "12M", fixed_legType, -0.3496/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinOIS(settlement_date, "18M", fixed_legType, -0.3173/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    
    swap = FinOIS(settlement_date, "2Y", fixed_legType, -0.2671/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinOIS(settlement_date, "30M", fixed_legType, -0.2070/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinOIS(settlement_date, "3Y", fixed_legType, -0.1410/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinOIS(settlement_date, "4Y", fixed_legType, -0.0060/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinOIS(settlement_date, "5Y", fixed_legType, 0.1285/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinOIS(settlement_date, "6Y", fixed_legType, 0.2590/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinOIS(settlement_date, "7Y", fixed_legType, 0.3830/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinOIS(settlement_date, "8Y", fixed_legType, 0.5020/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinOIS(settlement_date, "9Y", fixed_legType, 0.6140/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinOIS(settlement_date, "10Y", fixed_legType, 0.7160/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinOIS(settlement_date, "11Y", fixed_legType, 0.8070/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinOIS(settlement_date, "12Y", fixed_legType, 0.8890/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinOIS(settlement_date, "15Y", fixed_legType, 1.0790/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinOIS(settlement_date, "20Y", fixed_legType, 1.2460/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinOIS(settlement_date, "25Y", fixed_legType, 1.3055/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinOIS(settlement_date, "30Y", fixed_legType, 1.3270/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinOIS(settlement_date, "35Y", fixed_legType, 1.3315/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinOIS(settlement_date, "40Y", fixed_legType, 1.3300/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    swap = FinOIS(settlement_date, "50Y", fixed_legType, 1.3270/100.0, fixedFreqType, fixedDCCType); swaps.append(swap)
    
    oisDepos = depos.copy()
    oisFras = fras.copy()
    oisSwaps = swaps.copy()
    
#    oisCurveFF = FinOISCurve(valuation_date, oisDepos, oisFras, oisSwaps, interp_type)
    
    iborDualCurve = FinIborDualCurve(valuation_date, oisCurveFF, iborDepos, iborFras, iborSwaps, interp_type)
Exemple #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

    """
    valuation_date = Date(6, 6, 2018)

    # We do the O/N rate which settles on trade date
    spotDays = 0
    settlement_date = valuation_date.addWeekDays(spotDays)
    depoDCCType = DayCountTypes.ACT_360
    depos = []
    deposit_rate = 0.0231381
    maturity_date = settlement_date.addMonths(3)
    depo = FinIborDeposit(settlement_date, maturity_date, deposit_rate,
                           depoDCCType)
    depos.append(depo)

    futs = []
    fut = FinIborFuture(valuation_date, 1); futs.append(fut)
    fut = FinIborFuture(valuation_date, 2); futs.append(fut)
    fut = FinIborFuture(valuation_date, 3); futs.append(fut)
    fut = FinIborFuture(valuation_date, 4); futs.append(fut)
    fut = FinIborFuture(valuation_date, 5); futs.append(fut)
    fut = FinIborFuture(valuation_date, 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 = DayCountTypes.THIRTY_E_360
    freq = FrequencyTypes.SEMI_ANNUAL

    spotDays = 2
    settlement_date = valuation_date.addWeekDays(spotDays)
    notional = ONE_MILLION
    fixed_legType = FinSwapTypes.PAY
    interp_type = FinInterpTypes.FLAT_FWD_RATES

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

    libor_curve = FinIborSingleCurveOLD(valuation_date, depos, fras, swaps, interp_type, True)

    principal = 0.0
    testCases.banner("======================================================")
    testCases.banner("SINGLE CURVE VALUATION")
    testCases.header("LABEL", "VALUE")
    testCases.print("VALUE:", swaps[0].value(valuation_date, libor_curve, libor_curve, None))
    testCases.print("FIXED:", swaps[0].fixed_legValue(valuation_date, libor_curve))
    testCases.print("FLOAT:", swaps[0].floatLegValue(valuation_date, libor_curve, libor_curve, None))

    testCases.banner("======================================================")
    testCases.banner("SINGLE CURVE VALUATION TO SWAP SETTLEMENT DATE")
    testCases.header("LABEL", "VALUE")
    testCases.print("VALUE:", swaps[0].value(settlement_date, libor_curve, libor_curve, None))
    testCases.print("FIXED:", swaps[0].fixed_legValue(settlement_date, libor_curve))
    testCases.print("FLOAT:", swaps[0].floatLegValue(settlement_date, libor_curve, libor_curve, None))
    testCases.banner("======================================================")

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

    oisCurve = buildOIS(valuation_date)
#    print(oisCurve)

    liborDualCurve = FinIborDualCurveOLD(valuation_date, 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(valuation_date, oisCurve, liborDualCurve, None))
    testCases.print("FIXED:", swaps[0].fixed_legValue(valuation_date, oisCurve))
    testCases.print("FLOAT:", swaps[0].floatLegValue(valuation_date, oisCurve, libor_curve, None))

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

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

    PLOT = False
    if PLOT is True:

        years = np.linspace(0, 5, 21)
        dates = settlement_date.addYears(years)
    
        singleCurveFwds = libor_curve.fwd(dates)
        plt.plot(years, singleCurveFwds, label="Single Libor Curve")
 
        oisCurveFwds = oisCurve.fwd(dates)    
        plt.plot(years, oisCurveFwds, label="OIS Curve")

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