def buildIborCurve(valuation_date):
    depoDCCType = DayCountTypes.THIRTY_E_360_ISDA
    depos = []
    deposit_rate = 0.050

    depo0 = FinIborDeposit(
        valuation_date,
        "1D",
        deposit_rate,
        depoDCCType)

    spotDays = 2
    settlement_date = valuation_date.addWeekDays(spotDays)

    maturity_date = settlement_date.addMonths(1)
    depo1 = FinIborDeposit(settlement_date,
                           maturity_date,
                           deposit_rate,
                           depoDCCType)

    maturity_date = settlement_date.addMonths(3)
    depo2 = FinIborDeposit(
        settlement_date,
        maturity_date,
        deposit_rate,
        depoDCCType)

    maturity_date = settlement_date.addMonths(6)
    depo3 = FinIborDeposit(
        settlement_date,
        maturity_date,
        deposit_rate,
        depoDCCType)

    maturity_date = settlement_date.addMonths(9)
    depo4 = FinIborDeposit(
        settlement_date,
        maturity_date,
        deposit_rate,
        depoDCCType)

    maturity_date = settlement_date.addMonths(12)
    depo5 = FinIborDeposit(
        settlement_date,
        maturity_date,
        deposit_rate,
        depoDCCType)

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

    fras = []
    fixedDCCType = DayCountTypes.ACT_365F
    fixedFreqType = FrequencyTypes.SEMI_ANNUAL

    swaps = []

    swap_rate = 0.05
    maturity_date = settlement_date.addMonths(24)
    swap1 = FinIborSwap(
        settlement_date,
        maturity_date,
        FinSwapTypes.PAY,
        swap_rate,
        fixedFreqType,
        fixedDCCType)

    #    print(swap1._fixed_leg._payment_dates)

    swaps.append(swap1)

    maturity_date = settlement_date.addMonths(36)
    swap2 = FinIborSwap(
        settlement_date,
        maturity_date,
        FinSwapTypes.PAY,
        swap_rate,
        fixedFreqType,
        fixedDCCType)
    swaps.append(swap2)

    #    print(swap2._fixed_leg._payment_dates)

    maturity_date = settlement_date.addMonths(48)
    swap3 = FinIborSwap(
        settlement_date,
        maturity_date,
        FinSwapTypes.PAY,
        swap_rate,
        fixedFreqType,
        fixedDCCType)
    swaps.append(swap3)

    #    print(swap3._fixed_leg._payment_dates)

    maturity_date = settlement_date.addMonths(60)
    swap4 = FinIborSwap(
        settlement_date,
        maturity_date,
        FinSwapTypes.PAY,
        swap_rate,
        fixedFreqType,
        fixedDCCType)
    swaps.append(swap4)

    #    print(swap4._fixed_leg._payment_dates)

    maturity_date = settlement_date.addMonths(72)
    swap5 = FinIborSwap(
        settlement_date,
        maturity_date,
        FinSwapTypes.PAY,
        swap_rate,
        fixedFreqType,
        fixedDCCType)
    swaps.append(swap5)

    #    print(swap5._fixed_leg._payment_dates)

    maturity_date = settlement_date.addMonths(84)
    swap6 = FinIborSwap(
        settlement_date,
        maturity_date,
        FinSwapTypes.PAY,
        swap_rate,
        fixedFreqType,
        fixedDCCType)
    swaps.append(swap6)

    #    print(swap6._fixed_leg._payment_dates)

    maturity_date = settlement_date.addMonths(96)
    swap7 = FinIborSwap(
        settlement_date,
        maturity_date,
        FinSwapTypes.PAY,
        swap_rate,
        fixedFreqType,
        fixedDCCType)
    swaps.append(swap7)

    #    print(swap7._fixed_leg._payment_dates)

    maturity_date = settlement_date.addMonths(108)
    swap8 = FinIborSwap(
        settlement_date,
        maturity_date,
        FinSwapTypes.PAY,
        swap_rate,
        fixedFreqType,
        fixedDCCType)
    swaps.append(swap8)

    #    print(swap8._fixed_leg._payment_dates)

    maturity_date = settlement_date.addMonths(120)
    swap9 = FinIborSwap(
        settlement_date,
        maturity_date,
        FinSwapTypes.PAY,
        swap_rate,
        fixedFreqType,
        fixedDCCType)
    swaps.append(swap9)

    #    print(swap9._fixed_leg._payment_dates)

    libor_curve = IborSingleCurve(valuation_date,
                                  depos,
                                  fras,
                                  swaps)

    if 1 == 0:
        import numpy as np
        num_steps = 40
        dt = 10 / num_steps
        times = np.linspace(0.0, 10.0, num_steps + 1)

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

    return libor_curve
Exemple #2
0
def test_FinIborDepositsFuturesSwaps():

    spotDate = Date(6, 6, 2018)
    spotDays = 0
    settlement_date = spotDate.addWeekDays(spotDays)
    depoDCCType = DayCountTypes.ACT_360
    depos = []
    deposit_rate = 0.0231381
    depo = FinIborDeposit(settlement_date, "3M", deposit_rate, depoDCCType)
    depos.append(depo)

    deposit_rate = 0.027
    depo = FinIborDeposit(settlement_date, "3M", deposit_rate, depoDCCType)
    depos.append(depo)

    depos = []
    depo = FinIborDeposit(settlement_date, "1M", 0.0230, depoDCCType)
    depos.append(depo)
    depo = FinIborDeposit(settlement_date, "2M", 0.0235, depoDCCType)
    depos.append(depo)
    depo = FinIborDeposit(settlement_date, "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
    start_date = spotDate.addWeekDays(spotDays)

    swaps = []
    fixed_legType = FinSwapTypes.PAY
    fixedDCCType = DayCountTypes.THIRTY_E_360
    fixedFreqType = FrequencyTypes.SEMI_ANNUAL
    floatFreqType = FrequencyTypes.QUARTERLY
    notional = 1000000
    principal = 0.0
    floatSpread = 0.0
    floatDCCType = DayCountTypes.ACT_360
    calendar_type = CalendarTypes.UNITED_STATES
    busDayAdjustRule = BusDayAdjustTypes.PRECEDING

    swap_rate = 0.02776305

    swap = FinIborSwap(start_date, "2Y", fixed_legType, swap_rate,
                       fixedFreqType, fixedDCCType, notional, floatSpread,
                       floatFreqType, floatDCCType, calendar_type,
                       busDayAdjustRule)

    swaps.append(swap)

    libor_curve = IborSingleCurve(spotDate, depos, fras, swaps)

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

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

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

        end_date = spotDate
        df = libor_curve.df(end_date)
        print(end_date, df)

        end_date = settlement_date
        df = libor_curve.df(end_date)
        print(end_date, df)

        end_date = Date(20, 6, 2018)
        df = libor_curve.df(end_date)
        print(end_date, df)

        for depo in depos:
            end_date = depo._maturity_date
            df = libor_curve.df(end_date)
            print(end_date, df)

        for fra in fras:
            end_date = fra._maturity_date
            df = libor_curve.df(end_date)
            print(end_date, df)

        for swap in swaps:
            end_date = swap._maturity_date
            df = libor_curve.df(end_date)
            print(end_date, df)

        swap.printFixedLegPV(spotDate)
        swap.printFloatLegPV(spotDate)
Exemple #3
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

    valuation_date = Date(24, 8, 2020)
    settlement_date = Date(24, 8, 2020)
    dcType = DayCountTypes.ACT_360
    depos = []

    maturity_date = settlement_date.addMonths(1)
    depo1 = FinIborDeposit(settlement_date, maturity_date, m * 0.001709,
                           dcType)

    maturity_date = settlement_date.addMonths(2)
    depo2 = FinIborDeposit(settlement_date, maturity_date, m * 0.002123,
                           dcType)

    maturity_date = settlement_date.addMonths(3)
    depo3 = FinIborDeposit(settlement_date, maturity_date, m * 0.002469,
                           dcType)

    maturity_date = settlement_date.addMonths(6)
    depo4 = FinIborDeposit(settlement_date, maturity_date, m * 0.003045,
                           dcType)

    maturity_date = settlement_date.addMonths(12)
    depo5 = FinIborDeposit(settlement_date, maturity_date, m * 0.004449,
                           dcType)

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

    swaps = []
    dcType = DayCountTypes.THIRTY_E_360_ISDA
    fixedFreq = FrequencyTypes.SEMI_ANNUAL

    maturity_date = settlement_date.addMonths(24)
    swap1 = FinIborSwap(settlement_date, maturity_date, FinSwapTypes.PAY,
                        m * 0.002155 + irBump, fixedFreq, dcType)
    swaps.append(swap1)

    maturity_date = settlement_date.addMonths(36)
    swap2 = FinIborSwap(settlement_date, maturity_date, FinSwapTypes.PAY,
                        m * 0.002305 + irBump, fixedFreq, dcType)
    swaps.append(swap2)

    maturity_date = settlement_date.addMonths(48)
    swap3 = FinIborSwap(settlement_date, maturity_date, FinSwapTypes.PAY,
                        m * 0.002665 + irBump, fixedFreq, dcType)
    swaps.append(swap3)

    maturity_date = settlement_date.addMonths(60)
    swap4 = FinIborSwap(settlement_date, maturity_date, FinSwapTypes.PAY,
                        m * 0.003290 + irBump, fixedFreq, dcType)
    swaps.append(swap4)

    libor_curve = IborSingleCurve(valuation_date, depos, [], swaps)

    cdsCoupon = 0.01 + mktSpreadBump

    cdsMarketContracts = []
    effective_date = Date(21, 8, 2020)
    cds = FinCDS(effective_date, "6M", cdsCoupon)
    cdsMarketContracts.append(cds)

    cds = FinCDS(effective_date, "1Y", cdsCoupon)
    cdsMarketContracts.append(cds)

    cds = FinCDS(effective_date, "2Y", cdsCoupon)
    cdsMarketContracts.append(cds)

    cds = FinCDS(effective_date, "3Y", cdsCoupon)
    cdsMarketContracts.append(cds)

    cds = FinCDS(effective_date, "4Y", cdsCoupon)
    cdsMarketContracts.append(cds)

    cds = FinCDS(effective_date, "5Y", cdsCoupon)
    cdsMarketContracts.append(cds)

    cds = FinCDS(effective_date, "7Y", cdsCoupon)
    cdsMarketContracts.append(cds)

    cds = FinCDS(effective_date, "10Y", cdsCoupon)
    cdsMarketContracts.append(cds)

    recovery_rate = 0.40

    issuer_curve = FinCDSCurve(settlement_date, cdsMarketContracts,
                               libor_curve, recovery_rate)

    testCases.header("DATE", "DISCOUNT_FACTOR", "SURV_PROB")
    years = np.linspace(0.0, 10.0, 20)
    dates = settlement_date.addYears(years)
    for dt in dates:
        df = libor_curve.df(dt)
        q = issuer_curve.survProb(dt)
        testCases.print("%16s" % dt, "%12.8f" % df, "%12.8f" % q)

    return libor_curve, issuer_curve
Exemple #4
0
def test_FinIborDepositsFRAsSwaps():

    valuation_date = Date(18, 9, 2019)

    dccType = DayCountTypes.THIRTY_E_360_ISDA
    depos = []

    spotDays = 0
    settlement_date = valuation_date.addWeekDays(spotDays)

    deposit_rate = 0.050
    maturity_date = settlement_date.addMonths(1)
    depo = FinIborDeposit(settlement_date, maturity_date, deposit_rate,
                          dccType)
    depos.append(depo)

    maturity_date = settlement_date.addMonths(2)
    depo = FinIborDeposit(settlement_date, maturity_date, deposit_rate,
                          dccType)
    depos.append(depo)

    maturity_date = settlement_date.addMonths(3)
    depo = FinIborDeposit(settlement_date, maturity_date, deposit_rate,
                          dccType)
    depos.append(depo)

    maturity_date = settlement_date.addMonths(6)
    depo = FinIborDeposit(settlement_date, maturity_date, deposit_rate,
                          dccType)
    depos.append(depo)

    maturity_date = settlement_date.addMonths(9)
    depo = FinIborDeposit(settlement_date, maturity_date, deposit_rate,
                          dccType)
    depos.append(depo)

    maturity_date = settlement_date.addMonths(12)
    depo = FinIborDeposit(settlement_date, maturity_date, deposit_rate,
                          dccType)
    depos.append(depo)

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

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

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

    swaps = []
    fixedDCCType = DayCountTypes.ACT_365F
    fixedFreqType = FrequencyTypes.SEMI_ANNUAL

    swap_rate = 0.05
    #    maturity_date = settlement_date.addMonths(24)
    #    swap = FinIborSwap(settlement_date, maturity_date, swap_rate, fixedFreqType,
    #                        fixedDCCType)
    #    swaps.append(swap)

    fixed_legType = FinSwapTypes.PAY
    maturity_date = settlement_date.addMonths(36)
    swap = FinIborSwap(settlement_date, maturity_date, fixed_legType,
                       swap_rate, fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settlement_date.addMonths(48)
    swap = FinIborSwap(settlement_date, maturity_date, fixed_legType,
                       swap_rate, fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settlement_date.addMonths(60)
    swap = FinIborSwap(settlement_date, maturity_date, fixed_legType,
                       swap_rate, fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settlement_date.addMonths(72)
    swap = FinIborSwap(settlement_date, maturity_date, fixed_legType,
                       swap_rate, fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settlement_date.addMonths(84)
    swap = FinIborSwap(settlement_date, maturity_date, fixed_legType,
                       swap_rate, fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settlement_date.addMonths(96)
    swap = FinIborSwap(settlement_date, maturity_date, fixed_legType,
                       swap_rate, fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settlement_date.addMonths(108)
    swap = FinIborSwap(settlement_date, maturity_date, fixed_legType,
                       swap_rate, fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settlement_date.addMonths(120)
    swap = FinIborSwap(settlement_date, maturity_date, fixed_legType,
                       swap_rate, fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settlement_date.addMonths(132)
    swap = FinIborSwap(settlement_date, maturity_date, fixed_legType,
                       swap_rate, fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settlement_date.addMonths(144)
    swap = FinIborSwap(settlement_date, maturity_date, fixed_legType,
                       swap_rate, fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settlement_date.addMonths(180)
    swap = FinIborSwap(settlement_date, maturity_date, fixed_legType,
                       swap_rate, fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settlement_date.addMonths(240)
    swap = FinIborSwap(settlement_date, maturity_date, fixed_legType,
                       swap_rate, fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settlement_date.addMonths(300)
    swap = FinIborSwap(settlement_date, maturity_date, fixed_legType,
                       swap_rate, fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settlement_date.addMonths(360)
    swap = FinIborSwap(settlement_date, maturity_date, fixed_legType,
                       swap_rate, fixedFreqType, fixedDCCType)
    swaps.append(swap)

    libor_curve = IborSingleCurve(valuation_date, depos, fras, swaps)

    df = libor_curve.df(settlement_date)

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

    for deposit in depos:
        df = libor_curve.df(deposit._maturity_date)
        testCases.print(str(deposit._maturity_date), df)

    for swap in swaps:
        df = libor_curve.df(swap._maturity_date)
        testCases.print(str(swap._maturity_date), df)