Exemple #1
0
    def test_excel_example_with_zero_coupon_bond(self):

        todays_date = Date(25, August, 2011)

        settlement_days = 3
        face_amount = 100
        calendar = TARGET()
        maturity_date = Date(26, February, 2024)

        bond = ZeroCouponBond(settlement_days, calendar, face_amount,
                              maturity_date, Following, 100.0, todays_date)

        discounting_term_structure = YieldTermStructure(relinkable=True)
        flat_term_structure = FlatForward(settlement_days=1,
                                          forward=0.044,
                                          calendar=NullCalendar(),
                                          daycounter=Actual365Fixed(),
                                          compounding=Continuous,
                                          frequency=Annual)
        discounting_term_structure.link_to(flat_term_structure)

        engine = DiscountingBondEngine(discounting_term_structure)

        bond.set_pricing_engine(engine)

        self.assertEquals(calendar.advance(todays_date, 3, Days),
                          bond.settlement_date())
        self.assertEquals(0., bond.accrued_amount(bond.settlement_date()))
        self.assertAlmostEquals(57.6915, bond.clean_price, 4)
Exemple #2
0
    def test_german_calendar(self):

        frankfcal = Germany(Market.Eurex)
        first_date = Date(31, Oct, 2009)
        second_date = Date(1, Jan, 2010)

        Dec_30_2009 = Date(30, Dec, 2009)
        Jan_4_2010 = Date(4, Jan, 2010)

        self.assertEqual(Dec_30_2009, frankfcal.adjust(second_date, Preceding))
        self.assertEqual(Jan_4_2010,
                         frankfcal.adjust(second_date, ModifiedPreceding))

        mat = Period(2, Months)

        self.assertEqual(
            Jan_4_2010,
            frankfcal.advance(first_date,
                              period=mat,
                              convention=Following,
                              end_of_month=False))
        self.assertEqual(
            Dec_30_2009,
            frankfcal.advance(first_date,
                              period=mat,
                              convention=ModifiedFollowing,
                              end_of_month=False))
        self.assertEqual(
            41,
            frankfcal.business_days_between(first_date, second_date, False,
                                            False))
Exemple #3
0
    def test_clean_price(self):
        notional = 1000000.0
        fixed_rates = [0.1]
        fixed_day_count = Actual365Fixed()
        fixed_calendar = self.calendar
        fixed_index = self.ii
        contractObservationLag = Period(3, Months)
        observationInterpolation = InterpolationType.Flat
        settlement_days = 3
        growth_only = True

        baseCPI = 206.1

        fixed_schedule = Schedule.from_rule(Date(2, 10,
                                                 2007), Date(2, 10, 2052),
                                            Period(6, Months), fixed_calendar,
                                            Unadjusted, Rule.Backward)

        cpi_bond = CPIBond(settlement_days, notional, growth_only, baseCPI,
                           contractObservationLag, fixed_index,
                           observationInterpolation, fixed_schedule,
                           fixed_rates, fixed_day_count, ModifiedFollowing)

        engine = DiscountingBondEngine(self.yts)
        cpi_bond.set_pricing_engine(engine)
        set_coupon_pricer(cpi_bond.cashflows, CPICouponPricer(self.yts))
        storedPrice = 383.01816406
        calculated = cpi_bond.clean_price
        self.assertAlmostEqual(storedPrice, calculated)
 def test_past_coupon_rate(self):
     """Testing rate for past overnight-indexed coupon"""
     past_coupon = self.make_coupon(Date(18, October, 2021),
                                    Date(18, November, 2021))
     expected_rate = 0.000987136104
     expected_amount = self.notional * expected_rate * 31.0 / 360
     self.assertAlmostEqual(past_coupon.rate, expected_rate)
     self.assertAlmostEqual(past_coupon.amount, expected_amount)
    def test_equality(self):
        date1 = Date(1, 1, 2011)
        date2 = Date(1, 1, 2011)
        date3 = datetime.date(2011, 1, 1)

        self.assertEqual(date1, date2)

        self.assertEqual(date1, date3)
    def test_accrued_amount_in_the_past(self):
        """Testing accrued amount in the past for overnight-indexed coupon"""
        coupon = self.make_coupon(Date(18, October, 2021),
                                  Date(18, January, 2022))

        expected_amount = self.notional * 0.000987136104 * 31.0 / 360
        self.assertAlmostEqual(coupon.accrued_amount(Date(18, November, 2021)),
                               expected_amount)
Exemple #7
0
    def test_short_calculation_first_period_isda(self):
        day_counter = ActualActual(ISDA)
        from_date = Date(1, February, 1999)
        to_date = Date(1, July, 1999)

        expected_result = 0.410958904110

        self.assertAlmostEquals(expected_result,
                                day_counter.year_fraction(from_date, to_date))
 def test_future_coupon_rate(self):
     """Testing rate for future overnight-indexed coupon"""
     self.forecast_curve.link_to(flat_rate(0.0010, Actual360()))
     future_coupon = self.make_coupon(Date(10, December, 2021),
                                      Date(10, January, 2022))
     expected_rate = 0.001000043057
     expected_amount = self.notional * expected_rate * 31.0 / 360
     self.assertAlmostEqual(future_coupon.rate, expected_rate)
     self.assertAlmostEqual(future_coupon.amount, expected_amount)
    def test_is_imm_date(self):

        dt = Date(17, Sep, 2014)
        is_imm = imm.is_IMM_date(dt)
        self.assertTrue(is_imm)

        dt = Date(18, Sep, 2014)
        is_imm = imm.is_IMM_date(dt)
        self.assertFalse(is_imm)
Exemple #10
0
    def test_is_business_day(self):

        ukcal = UnitedKingdom()

        bank_holiday_date = Date(3, May, 2010)  #Early May Bank Holiday
        business_day = Date(28, March, 2011)

        self.assertFalse(ukcal.is_business_day(bank_holiday_date))
        self.assertTrue(ukcal.is_business_day(business_day))
Exemple #11
0
    def test_next_date(self):
        dt = Date(19, Jun, 2014)
        dt_2 = imm.next_date(dt)
        # 17 sep 2014
        self.assertEqual(dt_2, date(2014, 9, 17))

        dt_3 = imm.next_date('M9', True, Date(1, 6, 2019))
        # 18 sep 2019
        self.assertEqual(dt_3, date(2019, 9, 18))
Exemple #12
0
    def test_short_calculation_second_period_afb(self):
        day_counter = ActualActual(AFB)
        from_date = Date(1, July, 1999)
        to_date = Date(1, July, 2000)

        expected_result = 1.000000000000

        self.assertAlmostEquals(expected_result,
                                day_counter.year_fraction(from_date, to_date))
Exemple #13
0
    def test_thirty360(self):

        day_counter = Thirty360(EUROBONDBASIS)
        from_date = Date(1, July, 1999)
        to_date = Date(1, July, 2000)

        expected_result = 1.000000000000

        self.assertAlmostEquals(expected_result,
                                day_counter.year_fraction(from_date, to_date))
    def test_accrued_amount_in_the_futures(self):
        """Testing accrued amount in the future for overnight-indexed coupon"""
        self.forecast_curve.link_to(flat_rate(0.0010, Actual360()))
        coupon = self.make_coupon(Date(10, December, 2021),
                                  Date(10, March, 2022))

        accrual_date = Date(10, January, 2022)
        expected_rate = 0.001000043057
        expected_amount = self.notional * expected_rate * 31.0 / 360
        self.assertAlmostEqual(coupon.accrued_amount(accrual_date),
                               expected_amount)
 def test_rate_when_today_is_holiday(self):
     """Testing rate for overnight-indexed coupon when today is a holiday"""
     with Settings() as s:
         s.evaluation_date = Date(20, November, 2021)
         self.forecast_curve.link_to(flat_rate(0.0010, Actual360()))
         coupon = self.make_coupon(Date(10, November, 2021),
                                   Date(10, December, 2021))
         expected_rate = 0.000930035180
         expected_amount = self.notional * expected_rate * 30.0 / 360
         self.assertAlmostEqual(coupon.rate, expected_rate)
         self.assertAlmostEqual(coupon.amount, expected_amount)
    def test_from_datetime_classmethod(self):

        date1 = Date(19, Nov, 1998)

        datetime_date = datetime.date(1998, 11, 19)
        from_datetime_date = Date.from_datetime(datetime_date)
        self.assertEqual(from_datetime_date.serial, date1.serial)

        datetime_datetime = datetime.datetime(1998, 11, 19, 0o1, 00)
        from_datetime_datetime = Date.from_datetime(datetime_datetime)
        self.assertEqual(from_datetime_datetime.serial, date1.serial)
Exemple #17
0
    def test_date_creation(self):

        date1 = Date(19, Nov, 1998)
        self.assertEquals(11, date1.month)

        date2 = Date(29, Feb, 2008)
        self.assertEquals(2, date2.month)

        with self.assertRaises(RuntimeError):
            # getting an invalid day
            date2 = Date(29, Feb, 2009)
Exemple #18
0
    def test_from_datetime_classmethod(self):

        date1 = Date(19, Nov, 1998)

        datetime_date = datetime.date(1998, 11, 19)
        from_datetime_date = Date.from_datetime(datetime_date)
        self.assertEquals(from_datetime_date.serial, date1.serial)

        datetime_datetime = datetime.datetime(1998, 11, 19, 01, 00)
        from_datetime_datetime = Date.from_datetime(datetime_datetime)
        self.assertEquals(from_datetime_datetime.serial, date1.serial)
Exemple #19
0
    def test_canada_calendar(self):

        cacal = Canada()
        holiday_date = Date(1, Jul, 2015)

        self.assertTrue(cacal.is_holiday(holiday_date))

        cacal = Canada(market=TSX)
        holiday_date = Date(3, August, 2015)

        self.assertTrue(cacal.is_holiday(holiday_date))
Exemple #20
0
    def test_united_states_calendar(self):

        uscal = UnitedStates()
        holiday_date = Date(4, Jul, 2010)

        self.assertTrue(uscal.is_holiday(holiday_date))

        uscal = UnitedStates(market=NYSE)
        holiday_date = Date(5, Sep, 2011)  # Labor day

        self.assertTrue(uscal.is_holiday(holiday_date))
Exemple #21
0
    def test_next_weekday(self):
        ''' Test next weekday

        The Friday following Tuesday, January 15th, 2002 was January 18th, 2002.
        see http://www.cpearson.com/excel/DateTimeWS.htm
        '''

        date1 = Date(15, Jan, 2002)
        date2 = next_weekday(date1, Friday)

        expected_date = Date(18, Jan, 2002)
        self.assertTrue(expected_date == date2)
Exemple #22
0
    def test_business_days_between_dates(self):

        ukcal = UnitedKingdom()

        date1 = Date(30, May, 2011)

        # 30st of May is Spring Bank Holiday
        date2 = Date(3, June, 2011)

        day_count = ukcal.business_days_between(date1, date2, include_last=True)

        self.assertEqual(4, day_count)
Exemple #23
0
    def test_short_calculation_second_period_isma(self):
        day_counter = ActualActual(ISMA)
        from_date = Date(1, July, 1999)
        to_date = Date(1, July, 2000)
        ref_start = Date(1, July, 1999)
        ref_end = Date(1, July, 2000)

        expected_result = 1.000000000000

        self.assertAlmostEquals(
            expected_result,
            day_counter.year_fraction(from_date, to_date, ref_start, ref_end))
Exemple #24
0
    def test_excel_example_with_fixed_rate_bond(self):
        '''Port the QuantLib Excel adding bond example to Python. '''

        todays_date = Date(25, August, 2011)

        settings = Settings()
        settings.evaluation_date = todays_date

        calendar = TARGET()
        effective_date = Date(10, Jul, 2006)
        termination_date = calendar.advance(effective_date,
                                            10,
                                            Years,
                                            convention=Unadjusted)

        settlement_days = 3
        face_amount = 100.0
        coupon_rate = 0.05
        redemption = 100.0

        fixed_bond_schedule = Schedule.from_rule(effective_date,
                                                 termination_date,
                                                 Period(Annual), calendar,
                                                 ModifiedFollowing,
                                                 ModifiedFollowing, Backward)

        issue_date = effective_date
        bond = FixedRateBond(settlement_days, face_amount,
                             fixed_bond_schedule, [coupon_rate],
                             ActualActual(ISMA), Following, redemption,
                             issue_date)

        discounting_term_structure = YieldTermStructure()
        flat_term_structure = FlatForward(settlement_days=1,
                                          forward=0.044,
                                          calendar=NullCalendar(),
                                          daycounter=Actual365Fixed(),
                                          compounding=Continuous,
                                          frequency=Annual)

        discounting_term_structure.link_to(flat_term_structure)

        engine = DiscountingBondEngine(discounting_term_structure)

        bond.set_pricing_engine(engine)

        self.assertEqual(Date(10, Jul, 2016), termination_date)
        self.assertEqual(calendar.advance(todays_date, 3, Days),
                         bond.settlement_date())
        self.assertEqual(Date(11, Jul, 2016), bond.maturity_date)
        self.assertAlmostEqual(0.6849,
                               bond.accrued_amount(bond.settlement_date()), 4)
        self.assertAlmostEqual(102.1154, bond.clean_price, 4)
Exemple #25
0
    def setUp(self):
        self.from_date = Date(1, Jan, 2011)
        self.to_date = Date(31, Dec, 2011)
        self.tenor = Period(4, Weeks)
        self.calendar = UnitedKingdom()
        self.convention = Following
        self.termination_convention = Preceding
        self.rule = Twentieth

        self.schedule = Schedule(self.from_date, self.to_date, self.tenor,
                                 self.calendar, self.convention,
                                 self.termination_convention, self.rule)
Exemple #26
0
    def test_calendar_date_advance(self):
        ukcal = UnitedKingdom()

        bank_holiday_date = Date(3, May, 2010)  #Early May Bank Holiday

        advanced_date = ukcal.advance(bank_holiday_date, step=6, units=Months)
        expected_date = Date(3, November, 2010)
        self.assertTrue(expected_date == advanced_date)

        period_10days = Period(10, Days)
        advanced_date = ukcal.advance(bank_holiday_date, period=period_10days)
        expected_date = Date(17, May, 2010)
        self.assertTrue(expected_date == advanced_date)
    def test_bucket_analysis_option(self):

        settings = Settings()

        calendar = TARGET()

        todays_date = Date(15, May, 1998)
        settlement_date = Date(17, May, 1998)

        settings.evaluation_date = todays_date

        option_type = Put
        underlying = 40
        strike = 40
        dividend_yield = 0.00
        risk_free_rate = 0.001
        volatility = SimpleQuote(0.20)
        maturity = Date(17, May, 1999)
        daycounter = Actual365Fixed()

        underlyingH = SimpleQuote(underlying)

        payoff = PlainVanillaPayoff(option_type, strike)

        flat_term_structure = FlatForward(reference_date=settlement_date,
                                          forward=risk_free_rate,
                                          daycounter=daycounter)
        flat_dividend_ts = FlatForward(reference_date=settlement_date,
                                       forward=dividend_yield,
                                       daycounter=daycounter)

        flat_vol_ts = BlackConstantVol(settlement_date, calendar, volatility,
                                       daycounter)

        black_scholes_merton_process = BlackScholesMertonProcess(
            underlyingH, flat_dividend_ts, flat_term_structure, flat_vol_ts)

        european_exercise = EuropeanExercise(maturity)
        european_option = VanillaOption(payoff, european_exercise)
        analytic_european_engine = AnalyticEuropeanEngine(
            black_scholes_merton_process)

        european_option.set_pricing_engine(analytic_european_engine)

        delta, gamma = bucket_analysis([underlyingH, volatility],
                                       [european_option],
                                       shift=1e-4,
                                       type=Centered)
        self.assertAlmostEqual(delta[0], european_option.delta)
        self.assertAlmostEqual(delta[1], european_option.vega)
        self.assertAlmostEqual(gamma[0], european_option.gamma, 5)
    def test_bucketanalysis_bond(self):

        face_amount = 100.0
        redemption = 100.0
        issue_date = Date(27, January, 2011)
        maturity_date = Date(1, January, 2021)
        coupon_rate = 0.055

        fixed_bond_schedule = Schedule.from_rule(
            issue_date,
            maturity_date,
            Period(Semiannual),
            UnitedStates(market=GovernmentBond),
            Unadjusted,
            Unadjusted,
            Backward,
            False)

        bond = FixedRateBond(
            self.settlement_days,
            face_amount,
            fixed_bond_schedule,
            [coupon_rate],
            ActualActual(Bond),
            Unadjusted,
            redemption,
            issue_date
        )

        pricing_engine = DiscountingBondEngine(self.ts)
        bond.set_pricing_engine(pricing_engine)

        self.assertAlmostEqual(bond.npv, 100.82127876105724)
        quotes = [rh.quote for rh in self.rate_helpers]
        delta, gamma = bucket_analysis(quotes, [bond])
        self.assertEqual(len(quotes), len(delta))
        old_values = [q.value for q in quotes]
        delta_manual = []
        gamma_manual = []
        pv = bond.npv
        shift = 1e-4
        for v, q in zip(old_values, quotes):
            q.value = v + shift
            pv_plus = bond.npv
            q.value = v - shift
            pv_minus = bond.npv
            delta_manual.append((pv_plus - pv_minus) * 0.5 / shift)
            gamma_manual.append((pv_plus - 2 * pv + pv_minus) / shift ** 2)
            q.value = v
        assert_allclose(delta, delta_manual)
        assert_allclose(gamma, gamma_manual, atol=1e-4)
Exemple #29
0
    def test_bucket_analysis_option(self):

        settings = Settings()

        calendar = TARGET()

        todays_date = Date(15, May, 1998)
        settlement_date = Date(17, May, 1998)

        settings.evaluation_date = todays_date

        option_type = Put
        underlying = 40
        strike = 40
        dividend_yield = 0.00
        risk_free_rate = 0.001
        volatility = 0.20
        maturity = Date(17, May, 1999)
        daycounter = Actual365Fixed()

        underlyingH = SimpleQuote(underlying)

        payoff = PlainVanillaPayoff(option_type, strike)

        flat_term_structure = FlatForward(reference_date=settlement_date,
                                          forward=risk_free_rate,
                                          daycounter=daycounter)
        flat_dividend_ts = FlatForward(reference_date=settlement_date,
                                       forward=dividend_yield,
                                       daycounter=daycounter)

        flat_vol_ts = BlackConstantVol(settlement_date, calendar, volatility,
                                       daycounter)

        black_scholes_merton_process = BlackScholesMertonProcess(
            underlyingH, flat_dividend_ts, flat_term_structure, flat_vol_ts)

        european_exercise = EuropeanExercise(maturity)
        european_option = VanillaOption(payoff, european_exercise)
        analytic_european_engine = AnalyticEuropeanEngine(
            black_scholes_merton_process)

        european_option.set_pricing_engine(analytic_european_engine)

        ba_eo = bucket_analysis([[underlyingH]], [european_option], [1], 0.50,
                                1)

        self.assertTrue(2, ba_eo)
        self.assertTrue(type(tuple), ba_eo)
        self.assertEqual(1, len(ba_eo[0][0]))
        self.assertAlmostEqual(-0.4582666150152517, ba_eo[0][0][0])
 def test_accrued_amount_spanning_today(self):
     """Testing accrued amount spanning today for current overnight-indexed coupon"""
     self.forecast_curve.link_to(flat_rate(0.0010, Actual360()))
     coupon = self.make_coupon(Date(10, November, 2021),
                               Date(10, January, 2022))
     expected_amount = self.notional * 0.000926701551 * 30.0 / 360
     self.assertAlmostEqual(coupon.accrued_amount(Date(10, December, 2021)),
                            expected_amount)
     self.sofr.add_fixing(Date(23, November, 2021), 0.0007)
     expected_amount = self.notional * 0.000916700760 * 30.0 / 360
     self.assertAlmostEqual(coupon.accrued_amount(Date(10, December, 2021)),
                            expected_amount)
     self.sofr.clear_fixings()
     self.sofr.add_fixings(self.dates, self.past_rates)
    def test_arithmetic_operators(self):

        # addition
        date1 = Date(19, Nov, 1998)
        date2 = date1 + 5
        expected_date = Date(24, Nov, 1998)
        self.assertTrue(expected_date == date2)

        # iadd
        date1 = Date(19, Nov, 1998)
        date1 += 3
        expected_date = Date(22, Nov, 1998)
        self.assertTrue(expected_date == date1)

        # substraction
        date1 = Date(19, Nov, 1998)
        date3 = date1 - 5
        expected_date = Date(14, Nov, 1998)
        self.assertTrue(expected_date == date3)

        with self.assertRaises(TypeError):
            # invalid operation
            date3 - "pomme"

        self.assertTrue(date3 - date1, 5)

        # isub
        date1 = Date(19, Nov, 1998)
        date1 -= 3
        expected_date = Date(16, Nov, 1998)
        self.assertTrue(expected_date == date1)
Exemple #32
0
    def test_today(self):

        py_today = datetime.date.today()

        ql_today = today()

        self.assertEqual(py_today.day, ql_today.day)
        self.assertEqual(py_today.month, ql_today.month)
        self.assertEqual(py_today.year, ql_today.year)

        py_now = datetime.datetime.now()
        ql_datetime = local_date_time()
        self.assertAlmostEqual(Date.from_datetime(py_now), ql_datetime)