コード例 #1
0
class ScheduleMethodTestCase(unittest.TestCase):
    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)

    def test_size(self):

        self.assertEquals(15, self.schedule.size())

    def test_dates(self):

        expected_dates_length = self.schedule.size()
        dates = list(self.schedule.dates())

        self.assertEquals(expected_dates_length, len(dates))

    def test_at(self):

        expected_date = self.calendar.adjust(self.from_date, Following)
        self.assertTrue(expected_date == self.schedule.at(0))

        next_date = self.calendar.adjust(self.from_date + Period(4, Weeks),
                                         Following)
        expected_date = Date(20, next_date.month, next_date.year)

        self.assertTrue(expected_date == self.schedule.at(1))

    def test_previous_next_reference_date(self):
        from_date = Date(3, Sep, 2011)
        to_date = Date(15, Dec, 2011)
        tenor = Period(1, Months)
        calendar = UnitedKingdom()
        convention = Following
        termination_convention = Following
        rule = Forward

        fwd_schedule = Schedule(from_date, to_date, tenor, calendar,
                                convention, termination_convention, rule)

        expected_date = Date(5, Sep, 2011)
        self.assert_(expected_date == fwd_schedule.next_date(from_date))

        rule = Backward

        bwd_schedule = Schedule(from_date, to_date, tenor, calendar,
                                convention, termination_convention, rule)

        expected_date = Date(15, Nov, 2011)
        self.assert_(expected_date == bwd_schedule.previous_date(to_date))
コード例 #2
0
ファイル: test_calendar.py プロジェクト: vishalbelsare/pyql
    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))
コード例 #3
0
ファイル: test_calendar.py プロジェクト: GuidoE/pyql
    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))
コード例 #4
0
ファイル: test_schedule.py プロジェクト: lnsongxf/pyql
    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)
コード例 #5
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)
コード例 #6
0
ファイル: test_calendar.py プロジェクト: GuidoE/pyql
    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.assertEquals(4, day_count)
コード例 #7
0
ファイル: test_calendar.py プロジェクト: vishalbelsare/pyql
    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)
コード例 #8
0
ファイル: test_calendar.py プロジェクト: PythonCharmers/pyql
    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())
コード例 #9
0
ファイル: test_calendar.py プロジェクト: GuidoE/pyql
    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)
コード例 #10
0
ファイル: test_calendar.py プロジェクト: vishalbelsare/pyql
    def test_calendar_creation(self):

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

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

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

        null_calendar = NullCalendar()
        self.assertEqual('Null', null_calendar.name)
コード例 #11
0
ファイル: test_calendar.py プロジェクト: AlexArgus/pyql
    def test_adjust_business_day(self):

        ukcal = UnitedKingdom()

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

        adjusted_date = ukcal.adjust(bank_holiday_date)
        following_date = bank_holiday_date + 1
        self.assertTrue(following_date == adjusted_date)

        adjusted_date = ukcal.adjust(bank_holiday_date, convention=Preceding)
        following_date = bank_holiday_date - 3  # bank holiday is a Monday
        self.assertTrue(following_date == adjusted_date)

        adjusted_date = ukcal.adjust(bank_holiday_date, convention=ModifiedPreceding)
        following_date = bank_holiday_date + 1  # Preceding is on a different
        # month
        self.assertTrue(following_date == adjusted_date)
コード例 #12
0
ファイル: test_calendar.py プロジェクト: AlexArgus/pyql
    def test_holiday_list_acces_and_modification(self):

        ukcal = UnitedKingdom()

        holidays = list(holiday_list(ukcal, Date(1, Jan, 2011), Date(31, 12, 2011)))
        self.assertEquals(UK_HOLIDAYS_2011, len(holidays))

        new_holiday_date = Date(23, August, 2011)

        ukcal.add_holiday(new_holiday_date)

        holidays = list(holiday_list(ukcal, Date(1, Jan, 2011), Date(31, 12, 2011)))
        self.assertEquals(UK_HOLIDAYS_2011 + 1, len(holidays))

        ukcal.remove_holiday(new_holiday_date)

        holidays = list(holiday_list(ukcal, Date(1, Jan, 2011), Date(31, 12, 2011)))
        self.assertEquals(UK_HOLIDAYS_2011, len(holidays))
コード例 #13
0
ファイル: test_calendar.py プロジェクト: vishalbelsare/pyql
    def test_adjust_business_day(self):

        ukcal = UnitedKingdom()

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

        adjusted_date = ukcal.adjust(bank_holiday_date)
        following_date = bank_holiday_date + 1
        self.assertTrue(following_date == adjusted_date)

        adjusted_date = ukcal.adjust(bank_holiday_date, convention=Preceding)
        following_date = bank_holiday_date - 3  # bank holiday is a Monday
        self.assertTrue(following_date == adjusted_date)

        adjusted_date = ukcal.adjust(bank_holiday_date,
                                     convention=ModifiedPreceding)
        following_date = bank_holiday_date + 1  # Preceding is on a different
        # month
        self.assertTrue(following_date == adjusted_date)
コード例 #14
0
ファイル: calendar_factory.py プロジェクト: sosh72/pyql
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)
    ]])
コード例 #15
0
ファイル: test_calendar.py プロジェクト: vishalbelsare/pyql
    def test_holiday_list_acces_and_modification(self):

        ukcal = UnitedKingdom()

        holidays = ukcal.holiday_list(Date(1, Jan, 2011), Date(31, 12, 2011))
        self.assertEqual(UK_HOLIDAYS_2011, len(holidays))

        new_holiday_date = Date(23, August, 2011)

        ukcal.add_holiday(new_holiday_date)

        holidays = ukcal.holiday_list(Date(1, Jan, 2011), Date(31, 12, 2011))
        self.assertEqual(UK_HOLIDAYS_2011 + 1, len(holidays))

        ukcal.remove_holiday(new_holiday_date)

        holidays = ukcal.holiday_list(Date(1, Jan, 2011), Date(31, 12, 2011))
        self.assertEqual(UK_HOLIDAYS_2011, len(holidays))
コード例 #16
0
ファイル: test_schedule.py プロジェクト: enthought/pyql
    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.from_rule(
            self.from_date, self.to_date, self.tenor, self.calendar,
            self.convention, self.termination_convention, self.rule
        )
コード例 #17
0
ファイル: test_calendar.py プロジェクト: vishalbelsare/pyql
    def test_joint(self):

        ukcal = UnitedKingdom()
        uscal = UnitedStates()

        bank_holiday_date = Date(3, May, 2010)  #Early May Bank Holiday
        thanksgiving_holiday_date = Date(22, Nov, 2012)

        jtcal = JointCalendar(ukcal, uscal, JOINHOLIDAYS)

        self.assertFalse(jtcal.is_business_day(bank_holiday_date))
        self.assertFalse(jtcal.is_business_day(thanksgiving_holiday_date))

        jtcal = JointCalendar(ukcal, uscal, JOINBUSINESSDAYS)

        self.assertTrue(jtcal.is_business_day(bank_holiday_date))
        self.assertTrue(jtcal.is_business_day(thanksgiving_holiday_date))
コード例 #18
0
    def test_schedule_from_dates(self):
        dates = [Date(3, Sep, 2011), Date(5, Nov, 2011), Date(15, Dec, 2011)]
        tenor = Period(1, Months)
        calendar = UnitedKingdom()
        convention = Following
        termination_convention = Following
        rule = Forward

        schedule = Schedule.from_dates(dates, calendar, convention,
                                       termination_convention, tenor, rule)

        expected_date = Date(3, Sep, 2011)
        self.assert_(expected_date == schedule.next_date(Date(3, Sep, 2011)))

        expected_date = Date(5, Nov, 2011)
        self.assert_(expected_date == schedule.next_date(Date(4, Sep, 2011)))

        expected_date = Date(15, Dec, 2011)
        self.assert_(expected_date == schedule.next_date(Date(6, Nov, 2011)))
コード例 #19
0
ファイル: test_schedule.py プロジェクト: systemtrader/pyql
    def test_previous_next_reference_date(self):
        from_date = Date(3, Sep, 2011)
        to_date = Date(15, Dec, 2011)
        tenor = Period(1, Months)
        calendar = UnitedKingdom()
        convention = Following
        termination_convention = Following
        rule = Forward

        fwd_schedule = Schedule(from_date, to_date, tenor, calendar, convention,
                termination_convention, rule)

        expected_date = Date(5, Sep, 2011)
        self.assertEqual(expected_date, fwd_schedule.next_date(from_date))

        rule = Backward

        bwd_schedule = Schedule(from_date, to_date, tenor, calendar, convention,
                termination_convention, rule)

        expected_date = Date(15, Nov, 2011)
        self.assertEqual(expected_date, bwd_schedule.previous_date(to_date))
コード例 #20
0
ファイル: test_schedule.py プロジェクト: lnsongxf/pyql
    def test_create_schedule(self):

        from_date = Date(1, Jan, 2011)
        to_date = Date(31, Dec, 2011)
        tenor = Period(3, Weeks)
        calendar = UnitedKingdom()
        convention = Following
        termination_convention = Following
        rule = Forward

        schedule = Schedule(from_date, to_date, tenor, calendar, convention,
                            termination_convention, rule)

        self.assertIsNotNone(schedule)

        for date in schedule.dates():
            pass

        # Constructor using the defaults for the different conventions
        schedule = Schedule(from_date, to_date, tenor, calendar)

        self.assertIsNotNone(schedule)
コード例 #21
0
    def test_holiday_list_acces_and_modification(self):

        ukcal = UnitedKingdom()

        holidays = list(
            holiday_list(ukcal, Date(1, Jan, 2011), Date(31, 12, 2011)))
        self.assertEquals(8, len(holidays))

        new_holiday_date = Date(23, August, 2011)

        ukcal.add_holiday(new_holiday_date)

        holidays = list(
            holiday_list(ukcal, Date(1, Jan, 2011), Date(31, 12, 2011)))
        self.assertEquals(9, len(holidays))

        ukcal.remove_holiday(new_holiday_date)

        holidays = list(
            holiday_list(ukcal, Date(1, Jan, 2011), Date(31, 12, 2011)))
        self.assertEquals(8, len(holidays))
コード例 #22
0
ファイル: test_schedule.py プロジェクト: enthought/pyql
class ScheduleMethodTestCase(unittest.TestCase):

    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.from_rule(
            self.from_date, self.to_date, self.tenor, self.calendar,
            self.convention, self.termination_convention, self.rule
        )

    def test_size(self):

        self.assertEqual(15, self.schedule.size())
        self.assertEqual(15, len(self.schedule))

    def test_dates(self):

        expected_dates_length = self.schedule.size()
        dates = list(self.schedule.dates())

        self.assertEqual(expected_dates_length, len(dates))

    def test_iter_dates(self):

        expected_dates_length = self.schedule.size()
        dates= [date for date in self.schedule]

        self.assertEqual(expected_dates_length, len(dates))

    def test_at(self):

        expected_date = self.calendar.adjust(self.from_date, Following)
        self.assertEqual(expected_date, self.schedule.at(0))
        self.assertEqual(expected_date, self.schedule[0])

        next_date = self.calendar.adjust(
            self.from_date + Period(4, Weeks), Following
        )
        expected_date = Date(20, next_date.month, next_date.year)

        self.assertEqual(expected_date, self.schedule.at(1))
        self.assertEqual(expected_date, self.schedule[1])

    def test_previous_next_reference_date(self):
        from_date = Date(3, Sep, 2011)
        to_date = Date(15, Dec, 2011)
        tenor = Period(1, Months)
        calendar = UnitedKingdom()
        convention = Following
        termination_convention = Following
        rule = Forward

        fwd_schedule = Schedule.from_rule(from_date, to_date,
                tenor, calendar, convention, termination_convention, rule)

        expected_date = Date(5, Sep, 2011)
        self.assertEqual(expected_date, fwd_schedule.next_date(from_date))

        rule = Backward

        bwd_schedule = Schedule.from_rule(from_date, to_date,
                tenor, calendar, convention, termination_convention, rule)

        expected_date = Date(15, Nov, 2011)
        self.assertEqual(expected_date, bwd_schedule.previous_date(to_date))

    def test_schedule_from_dates(self):
        dates = [Date(3, Sep, 2011),
                 Date(5, Nov, 2011),
                 Date(15, Dec, 2011)]
        tenor = Period(1, Months)
        calendar = UnitedKingdom()
        convention = Following
        termination_convention = Following
        rule = Forward

        schedule = Schedule.from_dates(dates,
                calendar, convention, termination_convention, tenor, rule)

        expected_date = Date(3, Sep, 2011)
        self.assert_(expected_date == schedule.next_date(Date(3, Sep, 2011)))

        expected_date = Date(5, Nov, 2011)
        self.assert_(expected_date == schedule.next_date(Date(4, Sep, 2011)))

        expected_date = Date(15, Dec, 2011)
        self.assert_(expected_date == schedule.next_date(Date(6, Nov, 2011)))
コード例 #23
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