def test_FinIborFuture():

    todayDate = FinDate(5, 5, 2020)

    testCases.header("VALUES")

    for i in range(1, 12):
        fut = FinIborFuture(todayDate, i, "3M")
        testCases.print(fut)

        fra = fut.toFRA(0.020, 0.0)
        testCases.print(fra)
Beispiel #2
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))
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()
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()