예제 #1
0
    def test_relinkable_structures(self):

        discounting_term_structure = YieldTermStructure()

        settlement_days = 3
        flat_term_structure = FlatForward(settlement_days=settlement_days,
            forward=0.044, calendar=NullCalendar(), daycounter=Actual360())

        discounting_term_structure.link_to(flat_term_structure)

        evaluation_date = Settings().evaluation_date +100
        self.assertEqual(
            flat_term_structure.discount(evaluation_date),
            discounting_term_structure.discount(evaluation_date)
        )


        another_flat_term_structure = FlatForward(settlement_days=10,
            forward=0.067, calendar=NullCalendar(), daycounter=Actual365Fixed())

        discounting_term_structure.link_to(another_flat_term_structure)

        self.assertEqual(
            another_flat_term_structure.discount(evaluation_date),
            discounting_term_structure.discount(evaluation_date)
        )

        self.assertNotEqual(
            flat_term_structure.discount(evaluation_date),
            discounting_term_structure.discount(evaluation_date)
        )
예제 #2
0
    def test_calendar_creation(self):

        calendar = TARGET()
        self.assertEquals('TARGET',  calendar.name())

        ukcalendar = UnitedKingdom()
        self.assertEquals('UK settlement',  ukcalendar.name())

        lse_cal = UnitedKingdom(market=EXCHANGE)
        self.assertEquals('London stock exchange',  lse_cal.name())

        null_calendar = NullCalendar()
        self.assertEquals('Null', null_calendar.name())
예제 #3
0
    def test_calendar_creation(self):

        calendar = TARGET()
        self.assertEquals('TARGET', calendar.name())

        ukcalendar = UnitedKingdom()
        self.assertEquals('UK settlement', ukcalendar.name())

        lse_cal = UnitedKingdom(market=EXCHANGE)
        self.assertEquals('London stock exchange', lse_cal.name())

        null_calendar = NullCalendar()
        self.assertEquals('Null', null_calendar.name())
예제 #4
0
    def test_reference_evaluation_data_changed(self):
        """Testing term structure against evaluation date change... """

        quote = SimpleQuote()
        term_structure = FlatForward(settlement_days=self.settlement_days,
            forward=quote, calendar=NullCalendar(), daycounter=Actual360())

        quote.value = 0.03

        expected = []
        for days in [10, 30, 60, 120, 360, 720]:
            expected.append(
                term_structure.discount(self.adjusted_today + days)
            )

        Settings().evaluation_date = self.adjusted_today + 30

        calculated = []
        for days in [10, 30, 60, 120, 360, 720]:
            calculated.append(
                term_structure.discount(self.adjusted_today+ 30 + days)
            )

        for i, val in enumerate(expected):
            self.assertAlmostEqual(val, calculated[i])
예제 #5
0
파일: test_bonds.py 프로젝트: stan2133/pyql
    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)
예제 #6
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)
예제 #7
0
class CalendarFactory:
    _lookup = dict([(cal.name(), cal) for cal in [
        TARGET(),
        NullCalendar(),
        Germany(),
        Germany(EUREX),
        Germany(FrankfurtStockExchange),
        Germany(GER_SETTLEMENT),
        Germany(EUWAX),
        Germany(XETRA),
        UnitedKingdom(),
        UnitedKingdom(EXCHANGE),
        UnitedKingdom(METALS),
        UnitedKingdom(UK_SETTLEMENT),
        UnitedStates(),
        UnitedStates(GOVERNMENTBOND),
        UnitedStates(NYSE),
        UnitedStates(NERC),
        UnitedStates(US_SETTLEMENT)
    ]])
예제 #8
0
def _bndprice(bond_yield, coupon_rate, pricing_date, maturity_date, period,
              basis, compounding_frequency):
    """
    Clean price and accrued interest of a bond
    """

    _period = str_to_frequency(period)

    evaluation_date = pydate_to_qldate(pricing_date)

    settings = Settings()
    settings.evaluation_date = evaluation_date

    calendar = TARGET()
    termination_date = pydate_to_qldate(maturity_date)

    # effective date must be before settlement date, but do not
    # care about exact issuance date of bond

    effective_date = Date(termination_date.day, termination_date.month,
                          evaluation_date.year)
    effective_date = calendar.advance(effective_date,
                                      -1,
                                      Years,
                                      convention=Unadjusted)

    settlement_date = calendar.advance(evaluation_date,
                                       2,
                                       Days,
                                       convention=ModifiedFollowing)

    face_amount = 100.0
    redemption = 100.0

    fixed_bond_schedule = Schedule(effective_date, termination_date,
                                   Period(_period), calendar,
                                   ModifiedFollowing, ModifiedFollowing,
                                   Backward)

    issue_date = effective_date
    cnt = DayCounter.from_name(basis)
    settlement_days = 2

    bond = FixedRateBond(settlement_days, face_amount, fixed_bond_schedule,
                         [coupon_rate], cnt, Following, redemption, issue_date)

    discounting_term_structure = YieldTermStructure(relinkable=True)

    cnt_yield = DayCounter.from_name('Actual/Actual (Historical)')

    flat_term_structure = FlatForward(settlement_days=2,
                                      forward=bond_yield,
                                      calendar=NullCalendar(),
                                      daycounter=cnt_yield,
                                      compounding=Compounded,
                                      frequency=_period)

    discounting_term_structure.link_to(flat_term_structure)

    engine = DiscountingBondEngine(discounting_term_structure)

    bond.set_pricing_engine(engine)

    price = bond.clean_price
    ac = bond.accrued_amount(pydate_to_qldate(settlement_date))

    return (price, ac)
예제 #9
0
from quantlib.time.calendars.null_calendar import NullCalendar
import quantlib.time.calendars.germany as ger
import quantlib.time.calendars.united_states as us
import quantlib.time.calendars.united_kingdom as uk
import quantlib.time.calendars.japan as jp
import quantlib.time.calendars.switzerland as sw
from quantlib.time.calendar import TARGET
from quantlib.util.object_registry import ObjectRegistry

#ISO-3166 country codes (http://en.wikipedia.org/wiki/ISO_3166-1)
ISO_3166_CALENDARS = {
    'TARGET': TARGET(),
    'NULL': NullCalendar(),
    'DEU': ger.Germany(),
    'EUREX': ger.Germany(ger.EUREX),
    'FSE': ger.Germany(ger.FRANKFURT_STOCK_EXCHANGE),
    'EUWAX': ger.Germany(ger.EUWAX),
    'XETRA': ger.Germany(ger.XETRA),
    'GBR': uk.UnitedKingdom(),
    'LSE': uk.UnitedKingdom(uk.EXCHANGE),
    'LME': uk.UnitedKingdom(uk.METALS),
    'USA': us.UnitedStates(),
    'USA-GVT-BONDS': us.UnitedStates(us.GOVERNMENTBOND),
    'NYSE': us.UnitedStates(us.NYSE),
    'NERC': us.UnitedStates(us.NERC),
    'JPN': jp.Japan(),
    'CHE': sw.Switzerland()
}


def initialize_code_registry():
예제 #10
0
파일: test_bonds.py 프로젝트: stan2133/pyql
    def test_excel_example_with_floating_rate_bond(self):

        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_date = calendar.adjust(Date(28, January, 2011))
        settlement_days = 3  #1
        face_amount = 13749769.27  #2
        coupon_rate = 0.05
        redemption = 100.0

        float_bond_schedule = Schedule(effective_date, termination_date,
                                       Period(Annual), calendar,
                                       ModifiedFollowing, ModifiedFollowing,
                                       Backward)  #3

        flat_discounting_term_structure = YieldTermStructure(relinkable=True)
        forecastTermStructure = YieldTermStructure(relinkable=True)

        dc = Actual360()
        ibor_index = Euribor6M(forecastTermStructure)  #5

        fixing_days = 2  #6
        gearings = [1, 0.0]  #7
        spreads = [1, 0.05]  #8
        caps = []  #9
        floors = []  #10
        pmt_conv = ModifiedFollowing  #11

        issue_date = effective_date

        float_bond = FloatingRateBond(settlement_days, face_amount,
                                      float_bond_schedule, ibor_index, dc,
                                      fixing_days, gearings, spreads, caps,
                                      floors, pmt_conv, redemption, issue_date)

        flat_term_structure = FlatForward(settlement_days=1,
                                          forward=0.055,
                                          calendar=NullCalendar(),
                                          daycounter=Actual365Fixed(),
                                          compounding=Continuous,
                                          frequency=Annual)
        flat_discounting_term_structure.link_to(flat_term_structure)
        forecastTermStructure.link_to(flat_term_structure)

        engine = DiscountingBondEngine(flat_discounting_term_structure)

        float_bond.set_pricing_engine(engine)
        cons_option_vol = ConstantOptionletVolatility(settlement_days,
                                                      UnitedStates(SETTLEMENT),
                                                      pmt_conv, 0.95,
                                                      Actual365Fixed())
        coupon_pricer = BlackIborCouponPricer(cons_option_vol)

        set_coupon_pricer(float_bond, coupon_pricer)

        self.assertEquals(Date(10, Jul, 2016), termination_date)
        self.assertEquals(calendar.advance(todays_date, 3, Days),
                          float_bond.settlement_date())
        self.assertEquals(Date(11, Jul, 2016), float_bond.maturity_date)
        self.assertAlmostEqual(
            0.6944, float_bond.accrued_amount(float_bond.settlement_date()), 4)
        self.assertAlmostEqual(98.2485, float_bond.dirty_price, 4)
        self.assertAlmostEqual(13500805.2469, float_bond.npv, 4)
예제 #11
0
파일: enums-2.py 프로젝트: sosh72/pyql
class Calendars(dict):

    '''
    Wrapper for quantlib Calendar objects and methods.
    Accepts python.datetime objects and strings
    instead of pyql.quantlib dates.
    
    :adjust:            Adjust date to business day
    :advance:           Advance date by specified period
    :is_business_day:   Checks date
    
    can be used as a dict using name property of pyql.quantlib.calendar objects
    for example:
        
        Calendars()['TARGET'] returns TARGET calendar
        
    '''

    from quantlib.time.calendar import TARGET
    from quantlib.time.calendars.null_calendar import NullCalendar
    from quantlib.time.calendars.germany import (
        Germany, EUREX, FrankfurtStockExchange, SETTLEMENT as GER_SETTLEMENT,
        EUWAX, XETRA
    )
    from quantlib.time.calendars.united_kingdom import (
        EXCHANGE, METALS, SETTLEMENT as UK_SETTLEMENT,
        UnitedKingdom
    )
    from quantlib.time.calendars.united_states import (
        UnitedStates, GOVERNMENTBOND, NYSE, NERC, SETTLEMENT as US_SETTLEMENT
    )

    _lookup = dict([(cal.name(), cal) for cal in
                    [TARGET(), NullCalendar(),
                     Germany(), Germany(EUREX), Germany(
                         FrankfurtStockExchange),
                        Germany(GER_SETTLEMENT), Germany(
                            EUWAX), Germany(XETRA),
                        UnitedKingdom(),
                        UnitedKingdom(EXCHANGE), UnitedKingdom(METALS),
                        UnitedKingdom(UK_SETTLEMENT),
                        UnitedStates(),
                        UnitedStates(GOVERNMENTBOND), UnitedStates(
                            NYSE), UnitedStates(NERC),
                        UnitedStates(US_SETTLEMENT)]
                    ]
                   )

    def __init__(self, *args):

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

    @classmethod
    def adjust(cls, date, calendar=None, convention=None):

        if not calendar:
            calendar = cls.TARGET()

        elif not hasattr(calendar, "adjust"):
            return None

        if not convention:
            convention = BusinessDayConventions.Following

        qldate = qldate_from_pydate(pydate(date))
        try:
            return pydate_from_qldate(calendar.adjust(qldate, convention))
        except:
            try:
                return pydate_from_qldate(calendar().adjust(qldate,
                                                            convention))
            except:
                return None

    @classmethod
    def advance(cls, date, n, timeunit=None, calendar=None, convention=None):
        """
        Advance pydate according the specified calendar and convention
        
        :pydate:   e.g. 19600809, date(1964, 9, 29), '5-23-1993'
        :n:        integer
        :timeunit: e.g., enums.TimeUnits.Days

        usage
        -----
        
        Note 9/6/1980 is a weekend
    
        >>> Calendars.advance(19800906, 1)
        datetime.date(1980, 9, 8)
        
        """
        if not calendar:
            calendar = cls.TARGET()

        elif not hasattr(calendar, "advance"):
            return None

        if not convention:
            convention = BusinessDayConventions.Following

        if not timeunit:
            timeunit = TimeUnits.Days

        qldate = qldate_from_pydate(pydate(date))
        try:
            return pydate_from_qldate(calendar.advance(qldate, n, timeunit))

        except:
            try:
                return pydate_from_qldate(
                    calendar().advance(qldate, n, timeunit)
                )

            except:
                print("failure {}".format(qldate))
                return None

    @classmethod
    def is_business_day(cls, date, calendar=None):
        if not calendar:
            calendar = cls.TARGET()

        elif not hasattr(calendar, "advance"):
            return None

        qldate = qldate_from_pydate(pydate(date))
        try:
            return calendar.is_business_day(qldate)

        except:
            try:
                return calendar().is_business_day(qldate)

            except:
                return None
예제 #12
0
파일: bonds.py 프로젝트: adriancdperu/pyql
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())
print 'Clean price:', bond.clean_price