def test_basic(self, test_curves):
        notional = 1e6
        start_date = date(2018, 7, 13)
        tenor_in_months = 6
        fixed_rate = 0.02
        swap = InterestRateSwap(notional, start_date, tenor_in_months, fixed_rate)

        libor_curve, ois_curve = test_curves

        d1 = date(2018, 10, 15)
        d2 = date(2019, 1, 14)
        fixed_leg_value = (notional * fixed_rate * thirty_360.yf(start_date, d2)
                           * ois_curve.df(d2))
        forward1 = libor_curve.forward(start_date, d1, actual_360)
        yf1 = actual_360.yf(start_date, d1)
        forward2 = libor_curve.forward(d1, d2, actual_360)
        yf2 = actual_360.yf(d1, d2)
        libor_leg_value = notional * (forward1 * yf1 * ois_curve.df(d1)
                                      + forward2 * yf2 * ois_curve.df(d2))
        swap_value = libor_leg_value - fixed_leg_value
        assert(abs(swap.value(libor_curve, ois_curve) - swap_value) < 1e-9)

        # Multiply notional by -10, double fixed rate.
        swap2 = InterestRateSwap(-10.0 * notional, start_date, tenor_in_months, 2.0 * fixed_rate)
        swap_value2 = -10.0 * (swap_value - fixed_leg_value)
        assert(abs(swap2.value(libor_curve, ois_curve) - swap_value2) < 1e-9)
 def __init__(self, notional, start_date, tenor_in_months, rate):
     """Creates a Libor deposit."""
     self.start_date = start_date
     self.flow_on_start_date = -notional
     self.end_date = add_months_mod_foll(start_date, tenor_in_months)
     year_fraction = actual_360.yf(start_date, self.end_date)
     self.flow_on_end_date = notional * (1.0 + rate * year_fraction)
 def test_basic(self):
     notional = 1e6
     start_date = date(2018, 7, 9)
     tenor_in_months = 3
     rate = 0.05
     libor_deposit = LiborDeposit(notional, start_date, tenor_in_months, rate)
     end_date = date(2018, 10, 9)
     known_df = 1.0 / (1.0 + rate * actual_360.yf(start_date, end_date))
     libor_curve = InterestRateCurve(start_date, [end_date], [known_df])
     ois_curve = InterestRateCurve(start_date, [end_date], [1.0])
     assert(abs(libor_deposit.value(libor_curve, ois_curve)) < 1e-6)
def test_forward():
    base_date = date(2018, 7, 13)
    dates = [date(2018, 10, 1), date(2019, 1, 1)]
    dfs = [0.97, 0.95]
    curve = InterestRateCurve(base_date, dates, dfs)

    start_date = date(2018, 8, 15)
    end_date = date(2018, 10, 15)
    start_df = curve.df(start_date)
    end_df = curve.df(end_date)
    forward = curve.forward(start_date, end_date, actual_360)
    yf = actual_360.yf(start_date, end_date)
    assert(abs(start_df / (1.0 + forward * yf) - end_df) < 1e-9)
    def test_single_period(self, test_curves):
        notional = 1e8
        start_date = date(2018, 7, 16)
        tenor = 3
        ois_leg_spread = 0.0020
        swap = OisBasisSwap(notional, start_date, tenor, ois_leg_spread)

        libor_curve, ois_curve = test_curves

        end_date = date(2018, 10, 16)
        libor_forward = libor_curve.forward(start_date, end_date, actual_360)
        ois_forward = ois_curve.forward(start_date, end_date, actual_360)
        yf = actual_360.yf(start_date, end_date)
        expected_value = (notional * (libor_forward - ois_forward - ois_leg_spread)
                          * yf * ois_curve.df(end_date))

        actual_value = swap.value(libor_curve, ois_curve)
        assert(abs(actual_value - expected_value) < 1e-9)