Exemplo n.º 1
0
    def test_first_example_afb(self):
        day_counter = ActualActual(AFB)

        self.assertAlmostEqual(
            0.497267759563,
            day_counter.year_fraction(self.from_date, self.to_date)
        )
Exemplo n.º 2
0
    def test_first_example_afb(self):
        day_counter = ActualActual(AFB)

        self.assertAlmostEqual(
            0.497267759563,
            day_counter.year_fraction(self.from_date, self.to_date)
        )
Exemplo n.º 3
0
    def test_first_example_isma(self):
        day_counter = ActualActual(ISMA)

        self.assertAlmostEqual(
            0.5,
            day_counter.year_fraction(self.from_date, self.to_date,
                                      self.ref_start, self.ref_end))
Exemplo n.º 4
0
    def test_first_example_isda(self):

        day_counter = ActualActual(ISDA)

        self.assertAlmostEqual(
            0.497724380567,
            day_counter.year_fraction(self.from_date, self.to_date))
Exemplo n.º 5
0
    def test_first_example_isma(self):
        day_counter = ActualActual(ISMA)

        self.assertAlmostEqual(
            0.5,
            day_counter.year_fraction(self.from_date, self.to_date,
                self.ref_start, self.ref_end)
        )
Exemplo n.º 6
0
    def test_first_example_isda(self):

        day_counter = ActualActual(ISDA)

        self.assertAlmostEqual(
            0.497724380567,
            day_counter.year_fraction(self.from_date, self.to_date)
        )
Exemplo n.º 7
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))
Exemplo n.º 8
0
    def test_short_calculation_first_period_afb(self):
        day_counter = ActualActual(AFB)
        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))
Exemplo n.º 9
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))
Exemplo n.º 10
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))
Exemplo n.º 11
0
    def test_short_calculation_first_period_isma(self):
        day_counter = ActualActual(ISMA)
        from_date = Date(1, February, 1999)
        to_date = Date(1, July, 1999)
        ref_start = Date(1, July, 1998)
        ref_end = Date(1, July, 1999)
        expected_result = 0.410958904110

        self.assertAlmostEquals(expected_result, day_counter.year_fraction(from_date, to_date, ref_start, ref_end))
Exemplo n.º 12
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))
Exemplo n.º 13
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))
Exemplo n.º 14
0
class DayCounters(dict):
    from quantlib.time.daycounters.thirty360 import Thirty360
    from quantlib.time.daycounter import (
        Actual360, Actual365Fixed
    )
    from quantlib.time.daycounters.actual_actual import (
        ActualActual, ISMA, ISDA, Bond
    )

    _lookup = dict([(dc.name(), dc) for dc in
                    [Thirty360(), Actual360(), Actual365Fixed(),
                     ActualActual(), ActualActual(ISMA), ActualActual(ISDA),
                     ActualActual(Bond)
                     ]]
                   )

    def __init__(self, *args):
        dict.__init__(self, self._lookup)
        self.update(*args)

    @classmethod
    def year_fraction(cls, date1, date2, daycounter=None):
        '''
        Calculate the fraction of a year between two date1 and date2,
        based on the daycount specified.
        dates may be ccyymmdd or python datetime.dates
        '''
        pydate1, pydate2 = map(pydate, (date1, date2))

        if not daycounter:
            daycounter = cls.ActualActual()

        qldate1 = qldate_from_pydate(pydate1)
        qldate2 = qldate_from_pydate(pydate2)

        return daycounter.year_fraction(qldate1, qldate2)

    @classmethod
    def daycount(cls, date1, date2, daycounter=None):
        pydate1, pydate2 = map(pydate, (date1, date2))

        if not daycounter:
            daycounter = cls.ActualActual()

        qldate1 = qldate_from_pydate(pydate1)
        qldate2 = qldate_from_pydate(pydate2)

        return daycounter.day_count(qldate1, qldate2)
Exemplo n.º 15
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)
Exemplo n.º 16
0
                                    Years,
                                    convention=Unadjusted)

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

fixed_bond_schedule = Schedule(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(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)
pricing_engine = DiscountingBondEngine(discounting_term_structure)
bond.set_pricing_engine(pricing_engine)

print('Settlement date: ', bond.settlement_date())
print('Maturity date:', bond.maturity_date)
print('Accrued amount: ', bond.accrued_amount(bond.settlement_date()))
Exemplo n.º 17
0
    def test_pricing_bond(self):
        '''Inspired by the C++ code from http://quantcorner.wordpress.com/.'''

        settings = Settings()

        # Date setup
        calendar = TARGET()

        # Settlement date
        settlement_date = calendar.adjust(Date(28, January, 2011))

        # Evaluation date
        fixing_days = 1
        settlement_days = 1

        todays_date = calendar.advance(settlement_date, -fixing_days, Days)

        settings.evaluation_date = todays_date

        # Bound attributes
        face_amount = 100.0
        redemption = 100.0
        issue_date = Date(27, January, 2011)
        maturity_date = Date(31, August, 2020)
        coupon_rate = 0.03625
        bond_yield = 0.034921

        discounting_term_structure = YieldTermStructure(relinkable=True)
        flat_term_structure = FlatForward(
            reference_date=settlement_date,
            forward=bond_yield,
            daycounter=Actual365Fixed(
            ),  #actual_actual.ActualActual(actual_actual.Bond),
            compounding=Compounded,
            frequency=Semiannual)
        # have a look at the FixedRateBondHelper to simplify this
        # construction
        discounting_term_structure.link_to(flat_term_structure)

        #Rate
        fixed_bond_schedule = Schedule(issue_date, maturity_date,
                                       Period(Semiannual),
                                       UnitedStates(market=GOVERNMENTBOND),
                                       Unadjusted, Unadjusted, Backward, False)

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

        bond.set_pricing_engine(discounting_term_structure)

        # tests
        self.assertTrue(Date(27, January, 2011), bond.issue_date)
        self.assertTrue(Date(31, August, 2020), bond.maturity_date)
        self.assertTrue(settings.evaluation_date, bond.valuation_date)

        # the following assertion fails but must be verified
        self.assertAlmostEqual(101.1, bond.clean_price, 1)
        self.assertAlmostEqual(101.1, bond.net_present_value, 1)
        self.assertAlmostEqual(101.1, bond.dirty_price)
        self.assertAlmostEqual(0.009851, bond.accrued_amount())

        print(settings.evaluation_date)
        print('Principal: {}'.format(face_amount))
        print('Issuing date: {} '.format(bond.issue_date))
        print('Maturity: {}'.format(bond.maturity_date))
        print('Coupon rate: {:.4%}'.format(coupon_rate))
        print('Yield: {:.4%}'.format(bond_yield))
        print('Net present value: {:.4f}'.format(bond.net_present_value))
        print('Clean price: {:.4f}'.format(bond.clean_price))
        print('Dirty price: {:.4f}'.format(bond.dirty_price))
        print('Accrued coupon: {:.6f}'.format(bond.accrued_amount()))
        print('Accrued coupon: {:.6f}'.format(
            bond.accrued_amount(Date(1, March, 2011))))