Example #1
0
def test_FinFXForward():

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

    valuation_date = Date(13, 2, 2018)
    expiry_date = valuation_date.add_months(12)
    # Forward is on EURUSD which is expressed as number of USD per EUR
    # ccy1 = EUR and ccy2 = USD
    forName = "EUR"
    domName = "USD"
    currency_pair = forName + domName  # Always ccy1ccy2
    spot_fx_rate = 1.300  # USD per EUR
    strike_fx_rate = 1.365  # USD per EUR
    ccy1InterestRate = 0.02  # USD Rates
    ccy2InterestRate = 0.05  # EUR rates

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

    spot_days = 0
    settlement_date = valuation_date.add_weekdays(spot_days)
    maturity_date = settlement_date.add_months(12)
    notional = 100.0
    calendar_type = CalendarTypes.TARGET

    depos = []
    fras = []
    swaps = []
    deposit_rate = ccy1InterestRate
    depo = IborDeposit(settlement_date, maturity_date, deposit_rate,
                       DayCountTypes.ACT_360, notional, calendar_type)
    depos.append(depo)
    for_discount_curve = IborSingleCurve(valuation_date, depos, fras, swaps)

    depos = []
    fras = []
    swaps = []
    deposit_rate = ccy2InterestRate
    depo = IborDeposit(settlement_date, maturity_date, deposit_rate,
                       DayCountTypes.ACT_360, notional, calendar_type)
    depos.append(depo)
    dom_discount_curve = IborSingleCurve(valuation_date, depos, fras, swaps)

    notional = 100.0
    notional_currency = forName

    fxForward = FXForward(expiry_date, strike_fx_rate, currency_pair, notional,
                          notional_currency)

    testCases.header("SPOT FX", "FX FWD", "VALUE_BS")

    fwdValue = fxForward.value(valuation_date, spot_fx_rate,
                               dom_discount_curve, for_discount_curve)

    fwdFXRate = fxForward.forward(valuation_date, spot_fx_rate,
                                  dom_discount_curve, for_discount_curve)

    testCases.print(spot_fx_rate, fwdFXRate, fwdValue)
def test_FinFXVanillaOptionBloombergExample():

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

    valuation_date = Date(13, 2, 2018)
    expiry_date = Date(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

    currency_pair = forName + domName  # Always FORDOM
    spot_fx_rate = 1.30
    strike_fx_rate = 1.3650
    volatility = 0.20

    spot_days = 0
    settlement_date = valuation_date.add_weekdays(spot_days)
    maturity_date = settlement_date.add_months(12)
    notional = 1000000.0
    notional_currency = "EUR"
    calendar_type = CalendarTypes.TARGET

    depos = []
    fras = []
    swaps = []
    depo = IborDeposit(settlement_date, maturity_date, domDepoRate,
                       DayCountTypes.ACT_360, notional, calendar_type)
    depos.append(depo)
    dom_discount_curve = IborSingleCurve(valuation_date, depos, fras, swaps)

    depos = []
    fras = []
    swaps = []
    depo = IborDeposit(settlement_date, maturity_date, forDepoRate,
                       DayCountTypes.ACT_360, notional, calendar_type)
    depos.append(depo)
    for_discount_curve = IborSingleCurve(valuation_date, depos, fras, swaps)

    model = BlackScholes(volatility)

    call_option = FXVanillaOption(expiry_date, strike_fx_rate, currency_pair,
                                  OptionTypes.EUROPEAN_CALL, notional,
                                  notional_currency, 2)

    value = call_option.value(valuation_date, spot_fx_rate, dom_discount_curve,
                              for_discount_curve, model)

    delta = call_option.delta(valuation_date, spot_fx_rate, dom_discount_curve,
                              for_discount_curve, model)

    testCases.header("value", "delta")
    testCases.print(value, delta)
Example #3
0
def test_ibor_depositsOnly():

    # 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

    valuation_date = Date(23, 2, 2018)

    spot_days = 0
    settlement_date = valuation_date.add_weekdays(spot_days)

    depoDCCType = DayCountTypes.ACT_360
    notional = 100.0
    calendar_type = CalendarTypes.TARGET
    depos = []

    # 1 month
    deposit_rate = 0.04
    maturity_date = settlement_date.add_months(1)
    depo = IborDeposit(settlement_date, maturity_date, deposit_rate,
                       depoDCCType, notional, calendar_type)
    depos.append(depo)

    # 2 months
    deposit_rate = 0.04
    maturity_date = settlement_date.add_months(2)
    depo = IborDeposit(settlement_date, maturity_date, deposit_rate,
                       depoDCCType, notional, calendar_type)
    depos.append(depo)

    # 6 months
    deposit_rate = 0.04
    maturity_date = settlement_date.add_months(6)
    depo = IborDeposit(settlement_date, maturity_date, deposit_rate,
                       depoDCCType, notional, calendar_type)
    depos.append(depo)

    # 1 year
    deposit_rate = 0.04
    maturity_date = settlement_date.add_months(12)
    depo = IborDeposit(settlement_date, maturity_date, deposit_rate,
                       depoDCCType, notional, calendar_type)
    depos.append(depo)

    fras = []
    swaps = []

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

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

    """ Check calibration """
    for depo in depos:
        v = depo.value(settlement_date, libor_curve)
        testCases.print("DEPO", depo._maturity_date, v)
def test_ibor_depositsAndSwaps(valuation_date):

    depoBasis = DayCountTypes.THIRTY_E_360_ISDA
    depos = []

    spot_days = 0
    settlement_date = valuation_date.add_weekdays(spot_days)
    deposit_rate = 0.05

    depo1 = IborDeposit(settlement_date, "1M", deposit_rate, depoBasis)
    depo2 = IborDeposit(settlement_date, "3M", deposit_rate, depoBasis)
    depo3 = IborDeposit(settlement_date, "6M", deposit_rate, depoBasis)

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

    fras = []

    swaps = []
    fixedBasis = DayCountTypes.ACT_365F
    fixedFreq = FrequencyTypes.SEMI_ANNUAL
    fixed_leg_type = SwapTypes.PAY

    swap_rate = 0.05
    swap1 = IborSwap(settlement_date, "1Y", fixed_leg_type, swap_rate,
                     fixedFreq, fixedBasis)
    swap2 = IborSwap(settlement_date, "3Y", fixed_leg_type, swap_rate,
                     fixedFreq, fixedBasis)
    swap3 = IborSwap(settlement_date, "5Y", fixed_leg_type, swap_rate,
                     fixedFreq, fixedBasis)

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

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

    return libor_curve
Example #5
0
def test_FinOISDepositsFRAsSwaps():

    valuation_date = Date(18, 9, 2019)

    dccType = DayCountTypes.THIRTY_E_360_ISDA
    depos = []

    spot_days = 0
    settleDt = valuation_date.add_weekdays(spot_days)

    depoDCCType = DayCountTypes.ACT_360
    notional = 100.0
    calendar_type = CalendarTypes.TARGET
    depos = []

    # 1 month
    deposit_rate = 0.04
    maturity_date = settleDt.add_months(1)
    depo = IborDeposit(settleDt, maturity_date, deposit_rate, depoDCCType,
                       notional, calendar_type)
    depos.append(depo)

    fras = []
    # 1 x 4 FRA
    fraRate = 0.04
    frasettleDt = settleDt.add_months(9)
    fraMaturityDate = settleDt.add_months(13)
    fra = IborFRA(frasettleDt, fraMaturityDate, fraRate, dccType)
    fras.append(fra)

    # 4 x 7 FRA
    fraRate = 0.03
    frasettleDt = settleDt.add_months(13)
    fraMaturityDate = settleDt.add_months(17)
    fra = IborFRA(frasettleDt, fraMaturityDate, fraRate, dccType)
    fras.append(fra)

    # 4 x 7 FRA
    fraRate = 0.07
    frasettleDt = settleDt.add_months(17)
    fraMaturityDate = settleDt.add_months(21)
    fra = IborFRA(frasettleDt, fraMaturityDate, fraRate, dccType)
    fras.append(fra)

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

    swap_rate = 0.05
    #    maturity_date = settleDt.add_months(24)
    #    swap = IborSwap(settleDt, maturity_date, swap_rate, fixedFreqType,
    #                        fixedDCCType)
    #    swaps.append(swap)

    fixed_leg_type = Finfixed_leg_types.PAY
    maturity_date = settleDt.add_months(36)
    swap = OIS(settleDt, maturity_date, fixed_leg_type, swap_rate,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settleDt.add_months(48)
    swap = OIS(settleDt, maturity_date, fixed_leg_type, swap_rate,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settleDt.add_months(60)
    swap = OIS(settleDt, maturity_date, fixed_leg_type, swap_rate,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settleDt.add_months(72)
    swap = OIS(settleDt, maturity_date, fixed_leg_type, swap_rate,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settleDt.add_months(84)
    swap = OIS(settleDt, maturity_date, fixed_leg_type, swap_rate,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settleDt.add_months(96)
    swap = OIS(settleDt, maturity_date, fixed_leg_type, swap_rate,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settleDt.add_months(108)
    swap = OIS(settleDt, maturity_date, fixed_leg_type, swap_rate,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settleDt.add_months(120)
    swap = OIS(settleDt, maturity_date, fixed_leg_type, swap_rate,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settleDt.add_months(132)
    swap = OIS(settleDt, maturity_date, fixed_leg_type, swap_rate,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settleDt.add_months(144)
    swap = OIS(settleDt, maturity_date, fixed_leg_type, swap_rate,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settleDt.add_months(180)
    swap = OIS(settleDt, maturity_date, fixed_leg_type, swap_rate,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settleDt.add_months(240)
    swap = OIS(settleDt, maturity_date, fixed_leg_type, swap_rate,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settleDt.add_months(300)
    swap = OIS(settleDt, maturity_date, fixed_leg_type, swap_rate,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settleDt.add_months(360)
    swap = OIS(settleDt, maturity_date, fixed_leg_type, swap_rate,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)

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

    df = libor_curve.df(settleDt)

    testCases.header("SETTLEMENT DATE", "DF")
    testCases.print(str(settleDt), 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)
Example #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
    spot_days = 0
    settleDt = valuation_date.add_weekdays(spot_days)
    accrual = DayCountTypes.THIRTY_E_360

    depo = IborDeposit(settleDt, "1D", 1.712 / 100.0, accrual)
    depos = [depo]

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

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

    accrual = DayCountTypes.THIRTY_E_360
    freq = FrequencyTypes.SEMI_ANNUAL
    spot_days = 2
    settleDt = valuation_date.add_weekdays(spot_days)
    payRec = SwapTypes.PAY
    lag = 1  # Not used

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

    oisCurve = OISCurve(valuation_date, depos, fras, swaps)

    #    swaps[0]._fixed_leg.print_valuation()
    #    swaps[0]._floatLeg.print_valuation()

    # 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(valuation_date, oisCurve, None))
    testCases.print("FIXED:",
                    -swaps[0]._fixed_leg.value(valuation_date, oisCurve))
    testCases.print("FLOAT:", swaps[0]._floatLeg.value(valuation_date,
                                                       oisCurve, None))

    testCases.header("VALUATION TO SWAP SETTLEMENT DATE", " PV")
    testCases.print("VALUE:", swaps[0].value(settleDt, oisCurve, None))
    testCases.print("FIXED:", -swaps[0]._fixed_leg.value(settleDt, oisCurve))
    testCases.print("FLOAT:", swaps[0]._floatLeg.value(settleDt, oisCurve,
                                                       None))
Example #7
0
def test_FinOISDepositsFuturesSwaps():

    spot_date = Date(6, 6, 2018)
    spot_days = 0
    settleDt = spot_date.add_weekdays(spot_days)
    depoDCCType = DayCountTypes.THIRTY_E_360_ISDA

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

    fras = []

    fraRate = futureToFRARate(97.6675, -0.00005)
    frasettleDt = spot_date.next_imm_date()
    fraMaturityDate = frasettleDt.next_imm_date()
    fra = IborFRA(frasettleDt, fraMaturityDate, fraRate, depoDCCType)
    fras.append(fra)

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

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

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

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

    fraRate = futureToFRARate(97.0750, -0.00589)
    frasettleDt = frasettleDt.next_imm_date()
    fraMaturityDate = frasettleDt.next_imm_date()
    fra = IborFRA(frasettleDt, fraMaturityDate, fraRate, depoDCCType)
    fras.append(fra)

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

    spot_days = 2
    start_date = spot_date.add_weekdays(spot_days)

    swaps = []
    fixed_leg_type = SwapTypes.PAY
    fixedDCCType = DayCountTypes.THIRTY_E_360
    fixedFreqType = FrequencyTypes.SEMI_ANNUAL
    floatFreqType = FrequencyTypes.QUARTERLY
    notional = 1000000
    float_spread = 0.0
    floatDCCType = DayCountTypes.ACT_360
    calendar_type = CalendarTypes.US
    busDayAdjustRule = BusDayAdjustTypes.PRECEDING

    swap_rate = 0.02776305
    payment_lag = 1

    swap = OIS(start_date, "2Y", fixed_leg_type, swap_rate, fixedFreqType,
               fixedDCCType, notional, payment_lag, float_spread,
               floatFreqType, floatDCCType, calendar_type, busDayAdjustRule)

    swaps.append(swap)

    libor_curve = OISCurve(spot_date, depos, fras, swaps)

    times = np.linspace(0.0, 2.0, 25)
    dates = spot_date.add_years(times)
    zero_rates = libor_curve.zero_rate(dates)
    fwd_rates = libor_curve.fwd(dates)

    if PLOT_GRAPHS:
        plt.figure(figsize=(8, 6))
        plt.plot(times, zero_rates * 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 = spot_date
        df = libor_curve.df(end_date)
        print(end_date, df)

        end_date = settleDt
        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 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.print_fixed_leg_pv(spot_date)
        swap.print_float_leg_pv(spot_date)
Example #8
0
def build_Ibor_Curve(valuation_date):

    depoDCCType = DayCountTypes.THIRTY_E_360_ISDA
    depos = []

    payFixed = SwapTypes.PAY

    spot_days = 2
    settlement_date = valuation_date.add_weekdays(spot_days)

    deposit_rate = 0.050
    maturity_date = settlement_date.add_months(1)
    depo1 = IborDeposit(
        settlement_date,
        maturity_date,
        deposit_rate,
        depoDCCType)

    maturity_date = settlement_date.add_months(3)
    depo2 = IborDeposit(
        settlement_date,
        maturity_date,
        deposit_rate,
        depoDCCType)

    maturity_date = settlement_date.add_months(6)
    depo3 = IborDeposit(
        settlement_date,
        maturity_date,
        deposit_rate,
        depoDCCType)

    maturity_date = settlement_date.add_months(9)
    depo4 = IborDeposit(
        settlement_date,
        maturity_date,
        deposit_rate,
        depoDCCType)

    maturity_date = settlement_date.add_months(12)
    depo5 = IborDeposit(
        settlement_date,
        maturity_date,
        deposit_rate,
        depoDCCType)

    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.add_months(24)
    swap1 = IborSwap(
        settlement_date,
        maturity_date,
        swap_rate,
        payFixed,
        fixedFreqType,
        fixedDCCType)
    swaps.append(swap1)

    maturity_date = settlement_date.add_months(36)
    swap2 = IborSwap(
        settlement_date,
        maturity_date,
        swap_rate,
        payFixed,
        fixedFreqType,
        fixedDCCType)
    swaps.append(swap2)

    maturity_date = settlement_date.add_months(48)
    swap3 = IborSwap(
        settlement_date,
        maturity_date,
        swap_rate,
        payFixed,
        fixedFreqType,
        fixedDCCType)
    swaps.append(swap3)

    maturity_date = settlement_date.add_months(60)
    swap4 = IborSwap(
        settlement_date,
        maturity_date,
        swap_rate,
        payFixed,
        fixedFreqType,
        fixedDCCType)
    swaps.append(swap4)

    maturity_date = settlement_date.add_months(72)
    swap5 = IborSwap(
        settlement_date,
        maturity_date,
        swap_rate,
        payFixed,
        fixedFreqType,
        fixedDCCType)
    swaps.append(swap5)

    maturity_date = settlement_date.add_months(84)
    swap6 = IborSwap(
        settlement_date,
        maturity_date,
        swap_rate,
        payFixed,
        fixedFreqType,
        fixedDCCType)
    swaps.append(swap6)

    maturity_date = settlement_date.add_months(96)
    swap7 = IborSwap(
        settlement_date,
        maturity_date,
        swap_rate,
        payFixed,
        fixedFreqType,
        fixedDCCType)
    swaps.append(swap7)

    maturity_date = settlement_date.add_months(108)
    swap8 = IborSwap(
        settlement_date,
        maturity_date,
        swap_rate,
        payFixed,
        fixedFreqType,
        fixedDCCType)
    swaps.append(swap8)

    maturity_date = settlement_date.add_months(120)
    swap9 = IborSwap(
        settlement_date,
        maturity_date,
        swap_rate,
        payFixed,
        fixedFreqType,
        fixedDCCType)
    swaps.append(swap9)

    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
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
    spot_days = 0
    settlement_date = valuation_date.add_weekdays(spot_days)
    depoDCCType = DayCountTypes.ACT_360
    depos = []
    deposit_rate = 0.0231381
    maturity_date = settlement_date.add_months(3)
    depo = IborDeposit(settlement_date, maturity_date, deposit_rate,
                       depoDCCType)
    depos.append(depo)

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

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

    accrual = DayCountTypes.THIRTY_E_360
    freq = FrequencyTypes.SEMI_ANNUAL

    spot_days = 2
    settlement_date = valuation_date.add_weekdays(spot_days)
    fixed_leg_type = SwapTypes.PAY
    interp_type = InterpTypes.FLAT_FWD_RATES

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

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

    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_leg.value(valuation_date, libor_curve))
    testCases.print(
        "FLOAT:", swaps[0]._floatLeg.value(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_leg.value(settlement_date, libor_curve))
    testCases.print(
        "FLOAT:", swaps[0]._floatLeg.value(settlement_date, libor_curve,
                                           libor_curve, None))
    testCases.banner("======================================================")

    #    swaps[0].print_fixed_leg_pv()
    #    swaps[0].print_float_leg_pv()

    oisCurve = buildOIS(valuation_date)
    #    print(oisCurve)

    liborDualCurve = IborDualCurve(valuation_date, oisCurve, depos, fras,
                                   swaps, InterpTypes.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_leg.value(valuation_date, oisCurve))
    testCases.print(
        "FLOAT:", swaps[0]._floatLeg.value(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_leg.value(settlement_date, oisCurve))
    testCases.print(
        "FLOAT:", swaps[0]._floatLeg.value(
            settlement_date,
            oisCurve,
            liborDualCurve,
            None,
        ))

    #    swaps[0].print_fixed_leg_pv()
    #    swaps[0].print_float_leg_pv()

    PLOT = False
    if PLOT is True:

        years = np.linspace(0, 5, 21)
        dates = settlement_date.add_years(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()
Example #10
0
def test_IborSwaptionQLExample():

    valuation_date = Date(4, 3, 2014)
    settlement_date = Date(4, 3, 2014)

    depoDCCType = DayCountTypes.THIRTY_E_360_ISDA
    depos = []
    depo = IborDeposit(settlement_date, "1W", 0.0023, depoDCCType)
    depos.append(depo)
    depo = IborDeposit(settlement_date, "1M", 0.0023, depoDCCType)
    depos.append(depo)
    depo = IborDeposit(settlement_date, "3M", 0.0023, depoDCCType)
    depos.append(depo)
    depo = IborDeposit(settlement_date, "6M", 0.0023, depoDCCType)
    depos.append(depo)

    # No convexity correction provided so I omit interest rate futures

    swaps = []
    accType = DayCountTypes.ACT_365F
    fixedFreqType = FrequencyTypes.SEMI_ANNUAL
    fixed_leg_type = SwapTypes.PAY

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

    libor_curve = IborSingleCurve(valuation_date, depos, [], swaps,
                                  InterpTypes.LINEAR_ZERO_RATES)

    exercise_date = settlement_date.add_tenor("5Y")
    swapMaturityDate = exercise_date.add_tenor("5Y")
    swapFixedCoupon = 0.040852
    swapFixedFrequencyType = FrequencyTypes.SEMI_ANNUAL
    swapFixedDayCountType = DayCountTypes.THIRTY_E_360_ISDA
    swapFloatFrequencyType = FrequencyTypes.QUARTERLY
    swapFloatDayCountType = DayCountTypes.ACT_360
    swapNotional = 1000000
    swaptionType = SwapTypes.PAY

    swaption = IborSwaption(settlement_date, exercise_date, swapMaturityDate,
                            swaptionType, swapFixedCoupon,
                            swapFixedFrequencyType, swapFixedDayCountType,
                            swapNotional, swapFloatFrequencyType,
                            swapFloatDayCountType)

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

    model = Black(0.1533)
    v = swaption.value(settlement_date, libor_curve, model)
    testCases.print(model.__class__, v)

    model = BlackShifted(0.1533, -0.008)
    v = swaption.value(settlement_date, libor_curve, model)
    testCases.print(model.__class__, v)

    model = SABR(0.132, 0.5, 0.5, 0.5)
    v = swaption.value(settlement_date, libor_curve, model)
    testCases.print(model.__class__, v)

    model = SABRShifted(0.352, 0.5, 0.15, 0.15, -0.005)
    v = swaption.value(settlement_date, libor_curve, model)
    testCases.print(model.__class__, v)

    model = HWTree(0.010000000, 0.00000000001)
    v = swaption.value(settlement_date, libor_curve, model)
    testCases.print(model.__class__, v)
def test_ibor_depositsFRAsSwaps():

    valuation_date = Date(18, 9, 2019)

    dccType = DayCountTypes.THIRTY_E_360_ISDA
    depos = []

    spot_days = 0
    settlement_date = valuation_date.add_weekdays(spot_days)

    deposit_rate = 0.050
    maturity_date = settlement_date.add_months(1)
    depo = IborDeposit(settlement_date, maturity_date, deposit_rate, dccType)
    depos.append(depo)

    maturity_date = settlement_date.add_months(2)
    depo = IborDeposit(settlement_date, maturity_date, deposit_rate, dccType)
    depos.append(depo)

    maturity_date = settlement_date.add_months(3)
    depo = IborDeposit(settlement_date, maturity_date, deposit_rate, dccType)
    depos.append(depo)

    maturity_date = settlement_date.add_months(6)
    depo = IborDeposit(settlement_date, maturity_date, deposit_rate, dccType)
    depos.append(depo)

    maturity_date = settlement_date.add_months(9)
    depo = IborDeposit(settlement_date, maturity_date, deposit_rate, dccType)
    depos.append(depo)

    maturity_date = settlement_date.add_months(12)
    depo = IborDeposit(settlement_date, maturity_date, deposit_rate, dccType)
    depos.append(depo)

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

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

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

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

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

    fixed_leg_type = SwapTypes.PAY
    maturity_date = settlement_date.add_months(36)
    swap = IborSwapOLD(settlement_date, maturity_date, fixed_leg_type,
                       swap_rate, fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settlement_date.add_months(48)
    swap = IborSwapOLD(settlement_date, maturity_date, fixed_leg_type,
                       swap_rate, fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settlement_date.add_months(60)
    swap = IborSwapOLD(settlement_date, maturity_date, fixed_leg_type,
                       swap_rate, fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settlement_date.add_months(72)
    swap = IborSwapOLD(settlement_date, maturity_date, fixed_leg_type,
                       swap_rate, fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settlement_date.add_months(84)
    swap = IborSwapOLD(settlement_date, maturity_date, fixed_leg_type,
                       swap_rate, fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settlement_date.add_months(96)
    swap = IborSwapOLD(settlement_date, maturity_date, fixed_leg_type,
                       swap_rate, fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settlement_date.add_months(108)
    swap = IborSwapOLD(settlement_date, maturity_date, fixed_leg_type,
                       swap_rate, fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settlement_date.add_months(120)
    swap = IborSwapOLD(settlement_date, maturity_date, fixed_leg_type,
                       swap_rate, fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settlement_date.add_months(132)
    swap = IborSwapOLD(settlement_date, maturity_date, fixed_leg_type,
                       swap_rate, fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settlement_date.add_months(144)
    swap = IborSwapOLD(settlement_date, maturity_date, fixed_leg_type,
                       swap_rate, fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settlement_date.add_months(180)
    swap = IborSwapOLD(settlement_date, maturity_date, fixed_leg_type,
                       swap_rate, fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settlement_date.add_months(240)
    swap = IborSwapOLD(settlement_date, maturity_date, fixed_leg_type,
                       swap_rate, fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settlement_date.add_months(300)
    swap = IborSwapOLD(settlement_date, maturity_date, fixed_leg_type,
                       swap_rate, fixedFreqType, fixedDCCType)
    swaps.append(swap)

    maturity_date = settlement_date.add_months(360)
    swap = IborSwapOLD(settlement_date, maturity_date, fixed_leg_type,
                       swap_rate, fixedFreqType, fixedDCCType)
    swaps.append(swap)

    libor_curve = FinIborSingleCurveOLD(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)
Example #12
0
def buildFullIssuerCurve(valuation_date):

    dcType = DayCountTypes.ACT_360
    depos = []
    irBump = 0.0

    m = 1.0  # 0.00000000000

    spot_days = 0
    settlement_date = valuation_date.add_days(spot_days)

    maturity_date = settlement_date.add_months(1)
    depo1 = IborDeposit(settlement_date, maturity_date, m * 0.0016, dcType)

    maturity_date = settlement_date.add_months(2)
    depo2 = IborDeposit(settlement_date, maturity_date, m * 0.0020, dcType)

    maturity_date = settlement_date.add_months(3)
    depo3 = IborDeposit(settlement_date, maturity_date, m * 0.0024, dcType)

    maturity_date = settlement_date.add_months(6)
    depo4 = IborDeposit(settlement_date, maturity_date, m * 0.0033, dcType)

    maturity_date = settlement_date.add_months(12)
    depo5 = IborDeposit(settlement_date, maturity_date, m * 0.0056, dcType)

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

    fras = []

    spot_days = 2
    settlement_date = valuation_date.add_days(spot_days)

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

    maturity_date = settlement_date.add_months(24)
    swap1 = IborSwap(settlement_date, maturity_date, SwapTypes.PAY,
                     m * 0.0044 + irBump, fixedFreq, dcType)
    swaps.append(swap1)

    maturity_date = settlement_date.add_months(36)
    swap2 = IborSwap(settlement_date, maturity_date, SwapTypes.PAY,
                     m * 0.0078 + irBump, fixedFreq, dcType)
    swaps.append(swap2)

    maturity_date = settlement_date.add_months(48)
    swap3 = IborSwap(settlement_date, maturity_date, SwapTypes.PAY,
                     m * 0.0119 + irBump, fixedFreq, dcType)
    swaps.append(swap3)

    maturity_date = settlement_date.add_months(60)
    swap4 = IborSwap(settlement_date, maturity_date, SwapTypes.PAY,
                     m * 0.0158 + irBump, fixedFreq, dcType)
    swaps.append(swap4)

    maturity_date = settlement_date.add_months(72)
    swap5 = IborSwap(settlement_date, maturity_date, SwapTypes.PAY,
                     m * 0.0192 + irBump, fixedFreq, dcType)
    swaps.append(swap5)

    maturity_date = settlement_date.add_months(84)
    swap6 = IborSwap(settlement_date, maturity_date, SwapTypes.PAY,
                     m * 0.0219 + irBump, fixedFreq, dcType)
    swaps.append(swap6)

    maturity_date = settlement_date.add_months(96)
    swap7 = IborSwap(settlement_date, maturity_date, SwapTypes.PAY,
                     m * 0.0242 + irBump, fixedFreq, dcType)
    swaps.append(swap7)

    maturity_date = settlement_date.add_months(108)
    swap8 = IborSwap(settlement_date, maturity_date, SwapTypes.PAY,
                     m * 0.0261 + irBump, fixedFreq, dcType)
    swaps.append(swap8)

    maturity_date = settlement_date.add_months(120)
    swap9 = IborSwap(settlement_date, maturity_date, SwapTypes.PAY,
                     m * 0.0276 + irBump, fixedFreq, dcType)
    swaps.append(swap9)

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

    cdsMarketContracts = []
    cdsCoupon = 0.005743
    maturity_date = valuation_date.next_cds_date(6)
    cds = CDS(valuation_date, maturity_date, cdsCoupon)
    cdsMarketContracts.append(cds)

    cdsCoupon = 0.007497
    maturity_date = valuation_date.next_cds_date(12)
    cds = CDS(valuation_date, maturity_date, cdsCoupon)
    cdsMarketContracts.append(cds)

    cdsCoupon = 0.011132
    maturity_date = valuation_date.next_cds_date(24)
    cds = CDS(valuation_date, maturity_date, cdsCoupon)
    cdsMarketContracts.append(cds)

    cdsCoupon = 0.013932
    maturity_date = valuation_date.next_cds_date(36)
    cds = CDS(valuation_date, maturity_date, cdsCoupon)
    cdsMarketContracts.append(cds)

    cdsCoupon = 0.015764
    maturity_date = valuation_date.next_cds_date(48)
    cds = CDS(valuation_date, maturity_date, cdsCoupon)
    cdsMarketContracts.append(cds)

    cdsCoupon = 0.017366
    maturity_date = valuation_date.next_cds_date(60)
    cds = CDS(valuation_date, maturity_date, cdsCoupon)
    cdsMarketContracts.append(cds)

    cdsCoupon = 0.020928
    maturity_date = valuation_date.next_cds_date(84)
    cds = CDS(valuation_date, maturity_date, cdsCoupon)
    cdsMarketContracts.append(cds)

    cdsCoupon = 0.022835
    maturity_date = valuation_date.next_cds_date(120)
    cds = CDS(valuation_date, maturity_date, cdsCoupon)
    cdsMarketContracts.append(cds)

    recovery_rate = 0.40

    issuer_curve = CDSCurve(valuation_date, cdsMarketContracts, libor_curve,
                            recovery_rate)

    return libor_curve, issuer_curve
Example #13
0
def test_FinFXForward():
    #  https://stackoverflow.com/questions/48778712
    #  /fx-vanilla-call-price-in-quantlib-doesnt-match-bloomberg

    valuation_date = Date(13, 2, 2018)
    expiry_date = valuation_date.add_months(12)
    # Forward is on EURUSD which is expressed as number of USD per EUR
    # ccy1 = EUR and ccy2 = USD
    forName = "EUR"
    domName = "USD"
    currency_pair = forName + domName  # Always ccy1ccy2
    spot_fx_rate = 1.300  # USD per EUR
    strike_fx_rate = 1.365  # USD per EUR
    ccy1InterestRate = 0.02  # USD Rates
    ccy2InterestRate = 0.05  # EUR rates

    spot_days = 0
    settlement_date = valuation_date.add_weekdays(spot_days)
    maturity_date = settlement_date.add_months(12)
    notional = 100.0
    calendar_type = CalendarTypes.TARGET

    depos = []
    fras = []
    swaps = []
    deposit_rate = ccy1InterestRate
    depo = IborDeposit(settlement_date, maturity_date, deposit_rate,
                       DayCountTypes.ACT_360, notional, calendar_type)
    depos.append(depo)
    for_discount_curve = IborSingleCurve(valuation_date, depos, fras, swaps)

    depos = []
    fras = []
    swaps = []
    deposit_rate = ccy2InterestRate
    depo = IborDeposit(settlement_date, maturity_date, deposit_rate,
                       DayCountTypes.ACT_360, notional, calendar_type)
    depos.append(depo)
    dom_discount_curve = IborSingleCurve(valuation_date, depos, fras, swaps)

    notional = 100.0
    notional_currency = forName

    fxForward = FXForward(expiry_date, strike_fx_rate, currency_pair, notional,
                          notional_currency)

    fwdValue = fxForward.value(valuation_date, spot_fx_rate,
                               dom_discount_curve, for_discount_curve)

    fwdFXRate = fxForward.forward(valuation_date, spot_fx_rate,
                                  dom_discount_curve, for_discount_curve)

    assert round(fwdFXRate, 4) == 1.3388

    assert round(fwdValue['value'], 4) == -2.4978
    assert round(fwdValue['cash_dom'], 4) == -249.7797
    assert round(fwdValue['cash_for'], 4) == -192.1382
    assert fwdValue['not_dom'] == 136.5
    assert fwdValue['not_for'] == 100.0
    assert fwdValue['ccy_dom'] == 'USD'
    assert fwdValue['ccy_for'] == 'EUR'
Example #14
0
def buildIborSingleCurve(valuation_date):

    settlement_date = valuation_date.add_days(2)
    dcType = DayCountTypes.ACT_360

    depos = []
    fras = []
    swaps = []

    maturity_date = settlement_date.add_months(1)
    depo1 = IborDeposit(valuation_date, maturity_date, -0.00251, dcType)
    depos.append(depo1)

    # Series of 1M futures
    start_date = settlement_date.next_imm_date()
    end_date = start_date.add_months(1)
    fra = IborFRA(start_date, end_date, -0.0023, dcType)
    fras.append(fra)

    start_date = start_date.add_months(1)
    end_date = start_date.add_months(1)
    fra = IborFRA(start_date, end_date, -0.00234, dcType)
    fras.append(fra)

    start_date = start_date.add_months(1)
    end_date = start_date.add_months(1)
    fra = IborFRA(start_date, end_date, -0.00225, dcType)
    fras.append(fra)

    start_date = start_date.add_months(1)
    end_date = start_date.add_months(1)
    fra = IborFRA(start_date, end_date, -0.00226, dcType)
    fras.append(fra)

    start_date = start_date.add_months(1)
    end_date = start_date.add_months(1)
    fra = IborFRA(start_date, end_date, -0.00219, dcType)
    fras.append(fra)

    start_date = start_date.add_months(1)
    end_date = start_date.add_months(1)
    fra = IborFRA(start_date, end_date, -0.00213, dcType)
    fras.append(fra)

    start_date = start_date.add_months(1)
    end_date = start_date.add_months(1)
    fra = IborFRA(start_date, end_date, -0.00186, dcType)
    fras.append(fra)

    start_date = start_date.add_months(1)
    end_date = start_date.add_months(1)
    fra = IborFRA(start_date, end_date, -0.00189, dcType)
    fras.append(fra)

    start_date = start_date.add_months(1)
    end_date = start_date.add_months(1)
    fra = IborFRA(start_date, end_date, -0.00175, dcType)
    fras.append(fra)

    start_date = start_date.add_months(1)
    end_date = start_date.add_months(1)
    fra = IborFRA(start_date, end_date, -0.00143, dcType)
    fras.append(fra)

    start_date = start_date.add_months(1)
    end_date = start_date.add_months(1)
    fra = IborFRA(start_date, end_date, -0.00126, dcType)
    fras.append(fra)

    start_date = start_date.add_months(1)
    end_date = start_date.add_months(1)
    fra = IborFRA(start_date, end_date, -0.00126, dcType)
    fras.append(fra)

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

    fixedFreq = FrequencyTypes.ANNUAL
    dcType = DayCountTypes.THIRTY_E_360
    fixed_leg_type = SwapTypes.PAY

    #######################################
    maturity_date = settlement_date.add_months(24)
    swap_rate = -0.001506
    swap1 = IborSwap(settlement_date, maturity_date, fixed_leg_type, swap_rate,
                     fixedFreq, dcType)
    swaps.append(swap1)

    #######################################
    maturity_date = settlement_date.add_months(36)
    swap_rate = -0.000185
    swap2 = IborSwap(settlement_date, maturity_date, fixed_leg_type, swap_rate,
                     fixedFreq, dcType)
    swaps.append(swap2)

    #######################################
    maturity_date = settlement_date.add_months(48)
    swap_rate = 0.001358
    swap3 = IborSwap(settlement_date, maturity_date, fixed_leg_type, swap_rate,
                     fixedFreq, dcType)
    swaps.append(swap3)

    #######################################
    maturity_date = settlement_date.add_months(60)
    swap_rate = 0.0027652
    swap4 = IborSwap(settlement_date, maturity_date, fixed_leg_type, swap_rate,
                     fixedFreq, dcType)
    swaps.append(swap4)

    #######################################
    maturity_date = settlement_date.add_months(72)
    swap_rate = 0.0041539
    swap5 = IborSwap(settlement_date, maturity_date, fixed_leg_type, swap_rate,
                     fixedFreq, dcType)
    swaps.append(swap5)

    #######################################
    maturity_date = settlement_date.add_months(84)
    swap_rate = 0.0054604
    swap6 = IborSwap(settlement_date, maturity_date, fixed_leg_type, swap_rate,
                     fixedFreq, dcType)
    swaps.append(swap6)

    #######################################
    maturity_date = settlement_date.add_months(96)
    swap_rate = 0.006674
    swap7 = IborSwap(settlement_date, maturity_date, fixed_leg_type, swap_rate,
                     fixedFreq, dcType)
    swaps.append(swap7)

    #######################################
    maturity_date = settlement_date.add_months(108)
    swap_rate = 0.007826
    swap8 = IborSwap(settlement_date, maturity_date, fixed_leg_type, swap_rate,
                     fixedFreq, dcType)
    swaps.append(swap8)

    #######################################
    maturity_date = settlement_date.add_months(120)
    swap_rate = 0.008821
    swap9 = IborSwap(settlement_date, maturity_date, fixed_leg_type, swap_rate,
                     fixedFreq, dcType)
    swaps.append(swap9)

    #######################################
    maturity_date = settlement_date.add_months(132)
    swap_rate = 0.0097379
    swap10 = IborSwap(settlement_date, maturity_date, fixed_leg_type,
                      swap_rate, fixedFreq, dcType)
    swaps.append(swap10)

    #######################################
    maturity_date = settlement_date.add_months(144)
    swap_rate = 0.0105406
    swap11 = IborSwap(settlement_date, maturity_date, fixed_leg_type,
                      swap_rate, fixedFreq, dcType)
    swaps.append(swap11)

    #######################################
    maturity_date = settlement_date.add_months(180)
    swap_rate = 0.0123927
    swap12 = IborSwap(settlement_date, maturity_date, fixed_leg_type,
                      swap_rate, fixedFreq, dcType)
    swaps.append(swap12)

    #######################################
    maturity_date = settlement_date.add_months(240)
    swap_rate = 0.0139882
    swap13 = IborSwap(settlement_date, maturity_date, fixed_leg_type,
                      swap_rate, fixedFreq, dcType)
    swaps.append(swap13)

    #######################################
    maturity_date = settlement_date.add_months(300)
    swap_rate = 0.0144972
    swap14 = IborSwap(settlement_date, maturity_date, fixed_leg_type,
                      swap_rate, fixedFreq, dcType)
    swaps.append(swap14)

    #######################################
    maturity_date = settlement_date.add_months(360)
    swap_rate = 0.0146081
    swap15 = IborSwap(settlement_date, maturity_date, fixed_leg_type,
                      swap_rate, fixedFreq, dcType)
    swaps.append(swap15)

    #######################################
    maturity_date = settlement_date.add_months(420)
    swap_rate = 0.01461897
    swap16 = IborSwap(settlement_date, maturity_date, fixed_leg_type,
                      swap_rate, fixedFreq, dcType)
    swaps.append(swap16)

    #######################################
    maturity_date = settlement_date.add_months(480)
    swap_rate = 0.014567455
    swap17 = IborSwap(settlement_date, maturity_date, fixed_leg_type,
                      swap_rate, fixedFreq, dcType)
    swaps.append(swap17)

    #######################################
    maturity_date = settlement_date.add_months(540)
    swap_rate = 0.0140826
    swap18 = IborSwap(settlement_date, maturity_date, fixed_leg_type,
                      swap_rate, fixedFreq, dcType)
    swaps.append(swap18)

    #######################################
    maturity_date = settlement_date.add_months(600)
    swap_rate = 0.01436822
    swap19 = IborSwap(settlement_date, maturity_date, fixed_leg_type,
                      swap_rate, fixedFreq, dcType)
    swaps.append(swap19)

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

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

    testCases.header("LABEL", "DATE", "VALUE")
    """ Check calibration """
    for depo in depos:
        v = depo.value(settlement_date, libor_curve)
        testCases.print("DEPO VALUE:", depo._maturity_date, v)

    for fra in fras:
        v = fra.value(settlement_date, libor_curve)
        testCases.print("FRA VALUE:", fra._maturity_date, v)

    for swap in swaps:
        v = swap.value(settlement_date, libor_curve)
        testCases.print("SWAP VALUE:", swap._maturity_date, v)

    return libor_curve
Example #15
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_leg_type = SwapTypes.RECEIVE

    fixedRate = 0.0150
    fixedDCCType = DayCountTypes.THIRTY_360_BOND
    fixedFreqType = FrequencyTypes.ANNUAL

    float_spread = 0.0
    floatDCCType = DayCountTypes.ACT_360
    floatFreqType = FrequencyTypes.SEMI_ANNUAL

    offMarketSwap = IborSwap(start_date, maturity_date, fixed_leg_type,
                             fixedRate, fixedFreqType, fixedDCCType, notional,
                             float_spread, floatFreqType, floatDCCType)

    interp_type = InterpTypes.LINEAR_ZERO_RATES

    depoDCCType = DayCountTypes.ACT_360
    depos = []

    ###########################################################################
    # MARKET
    ###########################################################################

    spot_days = 0
    settlement_date = valuation_date.add_weekdays(spot_days)
    depo = IborDeposit(settlement_date, "6M", -0.2510 / 100.0, depoDCCType)
    depos.append(depo)

    fras = []
    fraDCCType = DayCountTypes.ACT_360

    fra = IborFRA(settlement_date.add_tenor("1M"), "6M", -0.2450 / 100.0,
                  fraDCCType)
    fras.append(fra)
    fra = IborFRA(settlement_date.add_tenor("2M"), "6M", -0.2435 / 100.0,
                  fraDCCType)
    fras.append(fra)
    fra = IborFRA(settlement_date.add_tenor("3M"), "6M", -0.2400 / 100.0,
                  fraDCCType)
    fras.append(fra)
    fra = IborFRA(settlement_date.add_tenor("4M"), "6M", -0.2360 / 100.0,
                  fraDCCType)
    fras.append(fra)
    fra = IborFRA(settlement_date.add_tenor("5M"), "6M", -0.2285 / 100.0,
                  fraDCCType)
    fras.append(fra)
    fra = IborFRA(settlement_date.add_tenor("6M"), "6M", -0.2230 / 100.0,
                  fraDCCType)
    fras.append(fra)
    fra = IborFRA(settlement_date.add_tenor("7M"), "6M", -0.2110 / 100.0,
                  fraDCCType)
    fras.append(fra)
    fra = IborFRA(settlement_date.add_tenor("8M"), "6M", -0.1990 / 100.0,
                  fraDCCType)
    fras.append(fra)
    fra = IborFRA(settlement_date.add_tenor("9M"), "6M", -0.1850 / 100.0,
                  fraDCCType)
    fras.append(fra)
    fra = IborFRA(settlement_date.add_tenor("10M"), "6M", -0.1680 / 100.0,
                  fraDCCType)
    fras.append(fra)
    fra = IborFRA(settlement_date.add_tenor("11M"), "6M", -0.1510 / 100.0,
                  fraDCCType)
    fras.append(fra)
    fra = IborFRA(settlement_date.add_tenor("12M"), "6M", -0.1360 / 100.0,
                  fraDCCType)
    fras.append(fra)

    swaps = []
    fixed_leg_type = SwapTypes.PAY
    fixedDCCType = DayCountTypes.THIRTY_360_BOND
    fixedFreqType = FrequencyTypes.ANNUAL

    swap = IborSwap(settlement_date, "2Y", fixed_leg_type, -0.1525 / 100.0,
                    fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = IborSwap(settlement_date, "3Y", fixed_leg_type, -0.0185 / 100.0,
                    fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = IborSwap(settlement_date, "4Y", fixed_leg_type, 0.1315 / 100.0,
                    fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = IborSwap(settlement_date, "5Y", fixed_leg_type, 0.2745 / 100.0,
                    fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = IborSwap(settlement_date, "6Y", fixed_leg_type, 0.4135 / 100.0,
                    fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = IborSwap(settlement_date, "7Y", fixed_leg_type, 0.5439 / 100.0,
                    fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = IborSwap(settlement_date, "8Y", fixed_leg_type, 0.6652 / 100.0,
                    fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = IborSwap(settlement_date, "9Y", fixed_leg_type, 0.7784 / 100.0,
                    fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = IborSwap(settlement_date, "10Y", fixed_leg_type, 0.8799 / 100.0,
                    fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = IborSwap(settlement_date, "11Y", fixed_leg_type, 0.9715 / 100.0,
                    fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = IborSwap(settlement_date, "12Y", fixed_leg_type, 1.0517 / 100.0,
                    fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = IborSwap(settlement_date, "15Y", fixed_leg_type, 1.2369 / 100.0,
                    fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = IborSwap(settlement_date, "20Y", fixed_leg_type, 1.3965 / 100.0,
                    fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = IborSwap(settlement_date, "25Y", fixed_leg_type, 1.4472 / 100.0,
                    fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = IborSwap(settlement_date, "30Y", fixed_leg_type, 1.4585 / 100.0,
                    fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = IborSwap(settlement_date, "35Y", fixed_leg_type, 1.4595 / 100.0,
                    fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = IborSwap(settlement_date, "40Y", fixed_leg_type, 1.4535 / 100.0,
                    fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = IborSwap(settlement_date, "45Y", fixed_leg_type, 1.4410 / 100.0,
                    fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = IborSwap(settlement_date, "50Y", fixed_leg_type, 1.4335 / 100.0,
                    fixedFreqType, fixedDCCType)
    swaps.append(swap)

    iborDepos = depos.copy()
    iborFras = fras.copy()
    ibor_swaps = swaps.copy()

    iborCurve = IborSingleCurve(valuation_date, iborDepos, iborFras,
                                ibor_swaps, 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 = []

    spot_days = 0
    settlement_date = valuation_date.add_weekdays(spot_days)
    depo = IborDeposit(settlement_date, "1D", -0.3490 / 100.0, depoDCCType)
    depos.append(depo)

    fras = []

    swaps = []
    fixed_leg_type = SwapTypes.PAY
    fixedDCCType = DayCountTypes.ACT_365F
    fixedFreqType = FrequencyTypes.ANNUAL

    # Standard OIS with standard annual terms
    swap = OIS(settlement_date, "2W", fixed_leg_type, -0.3600 / 100.0,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = OIS(settlement_date, "1M", fixed_leg_type, -0.3560 / 100.0,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = OIS(settlement_date, "2M", fixed_leg_type, -0.3570 / 100.0,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = OIS(settlement_date, "3M", fixed_leg_type, -0.3580 / 100.0,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = OIS(settlement_date, "4M", fixed_leg_type, -0.3575 / 100.0,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = OIS(settlement_date, "5M", fixed_leg_type, -0.3578 / 100.0,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = OIS(settlement_date, "6M", fixed_leg_type, -0.3580 / 100.0,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = OIS(settlement_date, "7M", fixed_leg_type, -0.3600 / 100.0,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = OIS(settlement_date, "8M", fixed_leg_type, -0.3575 / 100.0,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = OIS(settlement_date, "9M", fixed_leg_type, -0.3569 / 100.0,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = OIS(settlement_date, "10M", fixed_leg_type, -0.3553 / 100.0,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = OIS(settlement_date, "11M", fixed_leg_type, -0.3534 / 100.0,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = OIS(settlement_date, "12M", fixed_leg_type, -0.3496 / 100.0,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = OIS(settlement_date, "18M", fixed_leg_type, -0.3173 / 100.0,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)

    swap = OIS(settlement_date, "2Y", fixed_leg_type, -0.2671 / 100.0,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = OIS(settlement_date, "30M", fixed_leg_type, -0.2070 / 100.0,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = OIS(settlement_date, "3Y", fixed_leg_type, -0.1410 / 100.0,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = OIS(settlement_date, "4Y", fixed_leg_type, -0.0060 / 100.0,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = OIS(settlement_date, "5Y", fixed_leg_type, 0.1285 / 100.0,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = OIS(settlement_date, "6Y", fixed_leg_type, 0.2590 / 100.0,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = OIS(settlement_date, "7Y", fixed_leg_type, 0.3830 / 100.0,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = OIS(settlement_date, "8Y", fixed_leg_type, 0.5020 / 100.0,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = OIS(settlement_date, "9Y", fixed_leg_type, 0.6140 / 100.0,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = OIS(settlement_date, "10Y", fixed_leg_type, 0.7160 / 100.0,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = OIS(settlement_date, "11Y", fixed_leg_type, 0.8070 / 100.0,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = OIS(settlement_date, "12Y", fixed_leg_type, 0.8890 / 100.0,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = OIS(settlement_date, "15Y", fixed_leg_type, 1.0790 / 100.0,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = OIS(settlement_date, "20Y", fixed_leg_type, 1.2460 / 100.0,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = OIS(settlement_date, "25Y", fixed_leg_type, 1.3055 / 100.0,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = OIS(settlement_date, "30Y", fixed_leg_type, 1.3270 / 100.0,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = OIS(settlement_date, "35Y", fixed_leg_type, 1.3315 / 100.0,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = OIS(settlement_date, "40Y", fixed_leg_type, 1.3300 / 100.0,
               fixedFreqType, fixedDCCType)
    swaps.append(swap)
    swap = OIS(settlement_date, "50Y", fixed_leg_type, 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 = IborDualCurve(valuation_date, oisCurveFF, iborDepos,
                                  iborFras, ibor_swaps, interp_type)
Example #16
0
def buildFullIssuerCurve1(mktSpreadBump, irBump):

    # https://www.markit.com/markit.jsp?jsppage=pv.jsp
    # YIELD CURVE 8-AUG-2019 SNAP AT 1600

    tradeDate = Date(9, 8, 2019)
    valuation_date = tradeDate.add_days(1)

    m = 1.0  # 0.00000000000

    dcType = DayCountTypes.ACT_360
    depos = []
    depo1 = IborDeposit(valuation_date, "1D", m * 0.0220, dcType)
    depos.append(depo1)

    spot_days = 2
    settlement_date = valuation_date.add_days(spot_days)

    maturity_date = settlement_date.add_months(1)
    depo1 = IborDeposit(settlement_date, maturity_date, m * 0.022009, dcType)

    maturity_date = settlement_date.add_months(2)
    depo2 = IborDeposit(settlement_date, maturity_date, m * 0.022138, dcType)

    maturity_date = settlement_date.add_months(3)
    depo3 = IborDeposit(settlement_date, maturity_date, m * 0.021810, dcType)

    maturity_date = settlement_date.add_months(6)
    depo4 = IborDeposit(settlement_date, maturity_date, m * 0.020503, dcType)

    maturity_date = settlement_date.add_months(12)
    depo5 = IborDeposit(settlement_date, maturity_date, m * 0.019930, dcType)

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

    fras = []

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

    maturity_date = settlement_date.add_months(24)
    swap1 = IborSwap(settlement_date, maturity_date, SwapTypes.PAY,
                     m * 0.015910 + irBump, fixedFreq, dcType)
    swaps.append(swap1)

    maturity_date = settlement_date.add_months(36)
    swap2 = IborSwap(settlement_date, maturity_date, SwapTypes.PAY,
                     m * 0.014990 + irBump, fixedFreq, dcType)
    swaps.append(swap2)

    maturity_date = settlement_date.add_months(48)
    swap3 = IborSwap(settlement_date, maturity_date, SwapTypes.PAY,
                     m * 0.014725 + irBump, fixedFreq, dcType)
    swaps.append(swap3)

    maturity_date = settlement_date.add_months(60)
    swap4 = IborSwap(settlement_date, maturity_date, SwapTypes.PAY,
                     m * 0.014640 + irBump, fixedFreq, dcType)
    swaps.append(swap4)

    maturity_date = settlement_date.add_months(72)
    swap5 = IborSwap(settlement_date, maturity_date, SwapTypes.PAY,
                     m * 0.014800 + irBump, fixedFreq, dcType)
    swaps.append(swap5)

    maturity_date = settlement_date.add_months(84)
    swap6 = IborSwap(settlement_date, maturity_date, SwapTypes.PAY,
                     m * 0.014995 + irBump, fixedFreq, dcType)
    swaps.append(swap6)

    maturity_date = settlement_date.add_months(96)
    swap7 = IborSwap(settlement_date, maturity_date, SwapTypes.PAY,
                     m * 0.015180 + irBump, fixedFreq, dcType)
    swaps.append(swap7)

    maturity_date = settlement_date.add_months(108)
    swap8 = IborSwap(settlement_date, maturity_date, SwapTypes.PAY,
                     m * 0.015610 + irBump, fixedFreq, dcType)
    swaps.append(swap8)

    maturity_date = settlement_date.add_months(120)
    swap9 = IborSwap(settlement_date, maturity_date, SwapTypes.PAY,
                     m * 0.015880 + irBump, fixedFreq, dcType)
    swaps.append(swap9)

    maturity_date = settlement_date.add_months(144)
    swap10 = IborSwap(settlement_date, maturity_date, SwapTypes.PAY,
                      m * 0.016430 + irBump, fixedFreq, dcType)
    swaps.append(swap10)

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

    cdsMarketContracts = []

    cdsCoupon = 0.04 + mktSpreadBump

    maturity_date = valuation_date.next_cds_date(6)
    cds = CDS(valuation_date, maturity_date, cdsCoupon)
    cdsMarketContracts.append(cds)

    maturity_date = valuation_date.next_cds_date(12)
    cds = CDS(valuation_date, maturity_date, cdsCoupon)
    cdsMarketContracts.append(cds)

    maturity_date = valuation_date.next_cds_date(24)
    cds = CDS(valuation_date, maturity_date, cdsCoupon)
    cdsMarketContracts.append(cds)

    maturity_date = valuation_date.next_cds_date(36)
    cds = CDS(valuation_date, maturity_date, cdsCoupon)
    cdsMarketContracts.append(cds)

    maturity_date = valuation_date.next_cds_date(48)
    cds = CDS(valuation_date, maturity_date, cdsCoupon)
    cdsMarketContracts.append(cds)

    maturity_date = valuation_date.next_cds_date(60)
    cds = CDS(valuation_date, maturity_date, cdsCoupon)
    cdsMarketContracts.append(cds)

    maturity_date = valuation_date.next_cds_date(84)
    cds = CDS(valuation_date, maturity_date, cdsCoupon)
    cdsMarketContracts.append(cds)

    maturity_date = valuation_date.next_cds_date(120)
    cds = CDS(valuation_date, maturity_date, cdsCoupon)
    cdsMarketContracts.append(cds)

    maturity_date = valuation_date.next_cds_date(180)
    cds = CDS(valuation_date, maturity_date, cdsCoupon)
    cdsMarketContracts.append(cds)

    recovery_rate = 0.40

    issuer_curve = CDSCurve(valuation_date, cdsMarketContracts, libor_curve,
                            recovery_rate)

    return libor_curve, issuer_curve
Example #17
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.add_months(1)
    depo1 = IborDeposit(settlement_date, maturity_date, m * 0.001709, dcType)

    maturity_date = settlement_date.add_months(2)
    depo2 = IborDeposit(settlement_date, maturity_date, m * 0.002123, dcType)

    maturity_date = settlement_date.add_months(3)
    depo3 = IborDeposit(settlement_date, maturity_date, m * 0.002469, dcType)

    maturity_date = settlement_date.add_months(6)
    depo4 = IborDeposit(settlement_date, maturity_date, m * 0.003045, dcType)

    maturity_date = settlement_date.add_months(12)
    depo5 = IborDeposit(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.add_months(24)
    swap1 = IborSwap(settlement_date, maturity_date, SwapTypes.PAY,
                     m * 0.002155 + irBump, fixedFreq, dcType)
    swaps.append(swap1)

    maturity_date = settlement_date.add_months(36)
    swap2 = IborSwap(settlement_date, maturity_date, SwapTypes.PAY,
                     m * 0.002305 + irBump, fixedFreq, dcType)
    swaps.append(swap2)

    maturity_date = settlement_date.add_months(48)
    swap3 = IborSwap(settlement_date, maturity_date, SwapTypes.PAY,
                     m * 0.002665 + irBump, fixedFreq, dcType)
    swaps.append(swap3)

    maturity_date = settlement_date.add_months(60)
    swap4 = IborSwap(settlement_date, maturity_date, SwapTypes.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 = CDS(effective_date, "6M", cdsCoupon)
    cdsMarketContracts.append(cds)

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

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

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

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

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

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

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

    recovery_rate = 0.40

    issuer_curve = CDSCurve(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.add_years(years)
    for dt in dates:
        df = libor_curve.df(dt)
        q = issuer_curve.survival_prob(dt)
        testCases.print("%16s" % dt, "%12.8f" % df, "%12.8f" % q)

    return libor_curve, issuer_curve
def test_derivativePricingExample():

    valuation_date = Date(10, 11, 2011)

    dccType = DayCountTypes.ACT_360
    depos = []

    # We do the O/N rate which settles on trade date
    spot_days = 0
    settlement_date = valuation_date.add_weekdays(spot_days)

    deposit_rate = 0.001410
    depo = IborDeposit(settlement_date, "ON", deposit_rate, dccType)
    depos.append(depo)

    spot_days = 1
    settlement_date = valuation_date.add_weekdays(spot_days)

    deposit_rate = 0.001410
    depo = IborDeposit(settlement_date, "TN", deposit_rate, dccType)
    depos.append(depo)

    spot_days = 2
    settlement_date = valuation_date.add_weekdays(spot_days)

    deposit_rate = 0.001910
    depo = IborDeposit(settlement_date, "1W", deposit_rate, dccType)
    depos.append(depo)

    deposit_rate = 0.002090
    depo = IborDeposit(settlement_date, "2W", deposit_rate, dccType)
    depos.append(depo)

    deposit_rate = 0.002490
    depo = IborDeposit(settlement_date, "1M", deposit_rate, dccType)
    depos.append(depo)

    deposit_rate = 0.003450
    depo = IborDeposit(settlement_date, "2M", deposit_rate, dccType)
    depos.append(depo)

    deposit_rate = 0.004570
    depo = IborDeposit(settlement_date, "3M", deposit_rate, dccType)
    depos.append(depo)

    deposit_rate = 0.005230
    depo = IborDeposit(settlement_date, "4M", deposit_rate, dccType)
    depos.append(depo)

    deposit_rate = 0.005860
    depo = IborDeposit(settlement_date, "5M", deposit_rate, dccType)
    depos.append(depo)

    deposit_rate = 0.006540
    depo = IborDeposit(settlement_date, "6M", deposit_rate, dccType)
    depos.append(depo)

    deposit_rate = 0.007080
    depo = IborDeposit(settlement_date, "7M", deposit_rate, dccType)
    depos.append(depo)

    deposit_rate = 0.007540
    depo = IborDeposit(settlement_date, "8M", deposit_rate, dccType)
    depos.append(depo)

    deposit_rate = 0.008080
    depo = IborDeposit(settlement_date, "9M", deposit_rate, dccType)
    depos.append(depo)

    deposit_rate = 0.008570
    depo = IborDeposit(settlement_date, "10M", deposit_rate, dccType)
    depos.append(depo)

    deposit_rate = 0.009130
    depo = IborDeposit(settlement_date, "11M", deposit_rate, dccType)
    depos.append(depo)

    fras = []

    swaps = []
    day_count_type = DayCountTypes.THIRTY_E_360_ISDA
    #    day_count_type = DayCountTypes.ACT_360
    freq_type = FrequencyTypes.SEMI_ANNUAL
    fixed_leg_type = SwapTypes.PAY

    swap_rate = 0.0058
    swap = IborSwapOLD(settlement_date, "1Y", fixed_leg_type, swap_rate,
                       freq_type, day_count_type)
    swaps.append(swap)

    swap_rate = 0.0060
    swap = IborSwapOLD(settlement_date, "2Y", fixed_leg_type, swap_rate,
                       freq_type, day_count_type)
    swaps.append(swap)

    swap_rate = 0.0072
    swap = IborSwapOLD(settlement_date, "3Y", fixed_leg_type, swap_rate,
                       freq_type, day_count_type)
    swaps.append(swap)

    swap_rate = 0.0096
    swap = IborSwapOLD(settlement_date, "4Y", fixed_leg_type, swap_rate,
                       freq_type, day_count_type)
    swaps.append(swap)

    swap_rate = 0.0124
    swap = IborSwapOLD(settlement_date, "5Y", fixed_leg_type, swap_rate,
                       freq_type, day_count_type)
    swaps.append(swap)

    swap_rate = 0.0173
    swap = IborSwapOLD(settlement_date, "7Y", fixed_leg_type, swap_rate,
                       freq_type, day_count_type)
    swaps.append(swap)

    swap_rate = 0.0219
    swap = IborSwapOLD(settlement_date, "10Y", fixed_leg_type, swap_rate,
                       freq_type, day_count_type)
    swaps.append(swap)

    swap_rate = 0.0283
    swap = IborSwapOLD(settlement_date, "30Y", fixed_leg_type, swap_rate,
                       freq_type, day_count_type)
    swaps.append(swap)

    numRepeats = 10
    start = time.time()

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

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

    start = time.time()

    for _ in range(0, numRepeats):
        _ = FinIborSingleCurveOLD(valuation_date, depos, fras, swaps,
                                  InterpTypes.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)
Example #19
0
def testFinIborCashSettledSwaption():

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

    valuation_date = Date(1, 1, 2020)
    settlement_date = Date(1, 1, 2020)

    depoDCCType = DayCountTypes.THIRTY_E_360_ISDA
    depos = []
    depo = IborDeposit(settlement_date, "1W", 0.0023, depoDCCType)
    depos.append(depo)
    depo = IborDeposit(settlement_date, "1M", 0.0023, depoDCCType)
    depos.append(depo)
    depo = IborDeposit(settlement_date, "3M", 0.0023, depoDCCType)
    depos.append(depo)
    depo = IborDeposit(settlement_date, "6M", 0.0023, depoDCCType)
    depos.append(depo)

    # No convexity correction provided so I omit interest rate futures

    settlement_date = Date(2, 1, 2020)

    swaps = []
    accType = DayCountTypes.ACT_365F
    fixedFreqType = FrequencyTypes.SEMI_ANNUAL
    fixed_leg_type = SwapTypes.PAY

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

    libor_curve = IborSingleCurve(valuation_date, depos, [], swaps,
                                  InterpTypes.LINEAR_ZERO_RATES)

    exercise_date = settlement_date.add_tenor("5Y")
    swapMaturityDate = exercise_date.add_tenor("5Y")
    swapFixedCoupon = 0.040852
    swapFixedFrequencyType = FrequencyTypes.SEMI_ANNUAL
    swapFixedDayCountType = DayCountTypes.THIRTY_E_360_ISDA
    swapFloatFrequencyType = FrequencyTypes.QUARTERLY
    swapFloatDayCountType = DayCountTypes.ACT_360
    swapNotional = 1000000
    fixed_leg_type = SwapTypes.PAY

    swaption = IborSwaption(settlement_date, exercise_date, swapMaturityDate,
                            fixed_leg_type, swapFixedCoupon,
                            swapFixedFrequencyType, swapFixedDayCountType,
                            swapNotional, swapFloatFrequencyType,
                            swapFloatDayCountType)

    model = Black(0.1533)
    v = swaption.value(settlement_date, libor_curve, model)
    testCases.print("Swaption No-Arb Value:", v)

    fwdSwapRate1 = libor_curve.swap_rate(exercise_date, swapMaturityDate,
                                         swapFixedFrequencyType,
                                         swapFixedDayCountType)

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

    fwdSwap = IborSwap(exercise_date, swapMaturityDate, fixed_leg_type,
                       swapFixedCoupon, swapFixedFrequencyType,
                       swapFixedDayCountType)

    fwdSwapRate2 = fwdSwap.swap_rate(settlement_date, libor_curve)
    testCases.print("Fwd Swap Swap Rate:", fwdSwapRate2)

    model = Black(0.1533)

    v = swaption.cash_settled_value(valuation_date, libor_curve, fwdSwapRate2,
                                    model)

    testCases.print("Swaption Cash Settled Value:", v)
def test_bloombergPricingExample(interp_type):
    """ 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
    spot_days = 0
    settlement_date = valuation_date.add_weekdays(spot_days)
    depoDCCType = DayCountTypes.ACT_360
    depos = []
    deposit_rate = 0.0231381
    maturity_date = settlement_date.add_months(3)
    depo = IborDeposit(settlement_date, maturity_date, deposit_rate,
                       depoDCCType)
    depos.append(depo)

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

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

    accrual = DayCountTypes.THIRTY_E_360
    freq = FrequencyTypes.SEMI_ANNUAL

    spot_days = 2
    settlement_date = valuation_date.add_weekdays(spot_days)
    notional = ONE_MILLION
    fixed_leg_type = SwapTypes.PAY

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

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

    # 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(valuation_date, libor_curve, libor_curve,
                                 None))
    testCases.print("FIXED:", swaps[0].fixed_leg_value(valuation_date,
                                                       libor_curve))
    testCases.print(
        "FLOAT:", swaps[0].float_leg_value(valuation_date, libor_curve,
                                           libor_curve, None))

    testCases.header("VALUATION TO SWAP SETTLEMENT DATE", " PV")
    testCases.print(
        "VALUE:", swaps[0].value(settlement_date, libor_curve, libor_curve,
                                 None))
    testCases.print("FIXED:", swaps[0].fixed_leg_value(settlement_date,
                                                       libor_curve))
    testCases.print(
        "FLOAT:", swaps[0].float_leg_value(settlement_date, libor_curve,
                                           libor_curve, None))

    # swaps[0].print_fixed_leg_pv()
    # swaps[0].print_float_leg_pv()

    if 1 == 0:
        plt.figure()

        years = np.linspace(0, 50, 500)
        dates = settlement_date.add_years(years)
        fwds = libor_curve.fwd(dates)
        plt.plot(years, fwds, label="Fwd Rate")
        plt.title(interp_type)
        plt.xlabel("Years")
        plt.legend()

        years = np.linspace(0, 50, 500)
        dates = settlement_date.add_years(years)
        fwds = libor_curve.zero_rate(dates)
        plt.plot(years, fwds, label="Zero Rate")
        plt.title(interp_type)
        plt.xlabel("Years")
        plt.ylabel("Rate")
        plt.legend()
Example #21
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
    spot_days = 0
    settlement_date = valuation_date.add_weekdays(spot_days)
    depoDCCType = DayCountTypes.ACT_360
    depos = []
    deposit_rate = 0.0231381
    maturity_date = settlement_date.add_months(3)
    depo = IborDeposit(settlement_date, maturity_date, deposit_rate,
                       depoDCCType)
    depos.append(depo)

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

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

    accrual = DayCountTypes.THIRTY_E_360
    freq = FrequencyTypes.SEMI_ANNUAL

    spot_days = 2
    settlement_date = valuation_date.add_weekdays(spot_days)
    fixed_leg_type = SwapTypes.PAY
    interp_type = InterpTypes.FLAT_FWD_RATES

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

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

    assert round(
        swaps[0].value(valuation_date, libor_curve, libor_curve, None),
        4) == 0.0
    assert round(swaps[0]._fixed_leg.value(valuation_date, libor_curve),
                 4) == -53707.6667
    assert round(
        swaps[0]._float_leg.value(valuation_date, libor_curve, libor_curve,
                                  None), 4) == 53707.6667

    assert round(
        swaps[0].value(settlement_date, libor_curve, libor_curve, None),
        4) == 0.0
    assert round(swaps[0]._fixed_leg.value(settlement_date, libor_curve),
                 4) == -53714.5507
    assert round(
        swaps[0]._float_leg.value(settlement_date, libor_curve, libor_curve,
                                  None), 4) == 53714.5507

    oisCurve = buildOIS(valuation_date)

    liborDualCurve = IborDualCurve(valuation_date, oisCurve, depos, fras,
                                   swaps, InterpTypes.FLAT_FWD_RATES, True)

    assert round(
        swaps[0].value(valuation_date, oisCurve, liborDualCurve, None),
        4) == 0.0
    assert round(swaps[0]._fixed_leg.value(valuation_date, oisCurve),
                 4) == -55524.5642
    assert round(
        swaps[0]._float_leg.value(valuation_date, oisCurve, liborDualCurve,
                                  None), 4) == 55524.5642

    assert round(
        swaps[0].value(settlement_date, oisCurve, liborDualCurve, None),
        4) == 0.0
    assert round(swaps[0]._fixed_leg.value(settlement_date, oisCurve),
                 4) == -55524.5709
    assert round(
        swaps[0]._float_leg.value(settlement_date, oisCurve, liborDualCurve,
                                  None), 4) == 55524.5709
Example #22
0
def test_FinFXVanillaOptionBloombergExample():
    # Example Bloomberg Pricing at
    # https://stackoverflow.com/questions/48778712/fx-vanilla-call-price-in-quantlib-doesnt-match-bloomberg

    valuation_date = Date(13, 2, 2018)
    expiry_date = Date(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

    currency_pair = forName + domName  # Always FORDOM
    spot_fx_rate = 1.30
    strike_fx_rate = 1.3650
    volatility = 0.20

    spot_days = 0
    settlement_date = valuation_date.add_weekdays(spot_days)
    maturity_date = settlement_date.add_months(12)
    notional = 1000000.0
    notional_currency = "EUR"
    calendar_type = CalendarTypes.TARGET

    depos = []
    fras = []
    swaps = []
    depo = IborDeposit(settlement_date, maturity_date, domDepoRate,
                       DayCountTypes.ACT_360, notional, calendar_type)
    depos.append(depo)
    dom_discount_curve = IborSingleCurve(valuation_date, depos, fras, swaps)

    depos = []
    fras = []
    swaps = []
    depo = IborDeposit(settlement_date, maturity_date, forDepoRate,
                       DayCountTypes.ACT_360, notional, calendar_type)
    depos.append(depo)
    for_discount_curve = IborSingleCurve(valuation_date, depos, fras, swaps)

    model = BlackScholes(volatility)

    call_option = FXVanillaOption(expiry_date, strike_fx_rate, currency_pair,
                                  OptionTypes.EUROPEAN_CALL, notional,
                                  notional_currency, 2)

    value = call_option.value(valuation_date, spot_fx_rate, dom_discount_curve,
                              for_discount_curve, model)

    assert round(value['v'], 4) == 0.0601
    assert round(value['cash_dom'], 4) == 60145.5078
    assert round(value['cash_for'], 4) == 46265.7752
    assert round(value['pips_dom'], 4) == 0.0601
    assert round(value['pips_for'], 4) == 0.0339
    assert round(value['pct_dom'], 4) == 0.0441
    assert round(value['pct_for'], 4) == 0.0463
    assert round(value['not_dom'], 4) == 1365000.0
    assert round(value['not_for'], 4) == 1000000.0
    assert value['ccy_dom'] == 'USD'
    assert value['ccy_for'] == 'EUR'

    delta = call_option.delta(valuation_date, spot_fx_rate, dom_discount_curve,
                              for_discount_curve, model)

    assert round(delta['pips_spot_delta'], 4) == 0.3671
    assert round(delta['pips_fwd_delta'], 4) == 0.3859
    assert round(delta['pct_spot_delta_prem_adj'], 4) == 0.3208
    assert round(delta['pct_fwd_delta_prem_adj'], 4) == 0.3373