Exemple #1
0
    def test_christmas_is_holiday(self):

        calendar = TARGET()

        date = Date(24,12, 2011)

        self.assertTrue(calendar.is_holiday(date))
    def test_christmas_is_holiday(self):

        calendar = TARGET()

        date = Date(24, 12, 2011)

        self.assertTrue(calendar.is_holiday(date))
Exemple #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())
    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())
    def test_iteration_on_date_list(self):

        date_iterator = holiday_list(TARGET(), Date(1, Jan, 2000),
                                     Date(1, Jan, 2001))

        holidays = [
            Date(21, Apr, 2000),
            Date(24, Apr, 2000),
            Date(1, May, 2000),
            Date(25, Dec, 2000),
            Date(26, Dec, 2000),
            Date(1, Jan, 2001)
        ]

        for date in date_iterator:
            self.assertIn(date, holidays)
Exemple #6
0
class Calendars(dict):
    '''
    Wrapper for quantlib Calendar objects and methods. Accepts python.datetime
    objects 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 pybg.quantlib.time.calendar import TARGET
    from pybg.quantlib.time.calendars.null_calendar import NullCalendar
    from pybg.quantlib.time.calendars.germany import (Germany, EUREX,
                                                      FrankfurtStockExchange,
                                                      SETTLEMENT as
                                                      GER_SETTLEMENT, EUWAX,
                                                      XETRA)
    from pybg.quantlib.time.calendars.united_kingdom import (EXCHANGE, METALS,
                                                             SETTLEMENT as
                                                             UK_SETTLEMENT,
                                                             UnitedKingdom)
    from pybg.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, pydate, 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(parse_date(pydate))
        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, pydate, 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(parse_date(pydate))
        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, pydate, calendar=None):
        if not calendar:
            calendar = cls.TARGET()

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

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

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

            except:
                return None