def testDailySchedule(self):
        # Jan 2 and Jan 3 are skipped as New Year holiday
        # Jan 7 is skipped as weekend
        # Jan 8 is adjusted to Jan 9 with following convention
        startDate = Date(2012, 1, 1)
        s = Schedule(startDate, startDate + 7,
                     Period(length=1, units=TimeUnits.Days),
                     Calendar("China.SSE"), BizDayConventions.Preceding)

        expected = [
            Date(2011, 12, 30),
            Date(2012, 1, 4),
            Date(2012, 1, 5),
            Date(2012, 1, 6),
            Date(2012, 1, 9)
        ]
        self.checkDates(s, expected)

        # The schedule should skip Saturday 21st and Sunday 22rd.
        # Previously, it would adjust them to Friday 20th, resulting
        # in three copies of the same date.
        startDate = Date(2012, 1, 17)
        s = Schedule(startDate, startDate + 7,
                     Period(length=1, units=TimeUnits.Days),
                     Calendar("Target"), BizDayConventions.Preceding)
        expected = [
            Date(2012, 1, 17),
            Date(2012, 1, 18),
            Date(2012, 1, 19),
            Date(2012, 1, 20),
            Date(2012, 1, 23),
            Date(2012, 1, 24)
        ]
        self.checkDates(s, expected)
 def testScheduleInitializeWithYearly(self):
     startDate = Date(2012, 2, 29)
     endDate = Date(2013, 3, 1)
     tenor = Period('1y')
     cal = Calendar('NullCalendar')
     sch = Schedule(startDate, endDate, tenor, cal)
     expected = [Date(2012, 2, 29), Date(2013, 2, 28), Date(2013, 3, 1)]
     for i in range(sch.size()):
         self.assertEqual(expected[i], sch[i])
 def testScheduleInitialize(self):
     startDate = Date(2013, 3, 31)
     endDate = Date(2013, 6, 30)
     tenor = Period('1m')
     cal = Calendar('NullCalendar')
     sch = Schedule(startDate, endDate, tenor, cal)
     expected = [
         Date(2013, 3, 31),
         Date(2013, 4, 30),
         Date(2013, 5, 31),
         Date(2013, 6, 30)
     ]
     for i in range(sch.size()):
         self.assertEqual(expected[i], sch[i])
Пример #4
0
def makeSchedule(firstDate, endDate, tenor):
    cal = Calendar('NullCalendar')
    firstDate = check_date(firstDate)
    endDate = check_date(endDate)
    tenor = Period(tenor)
    schedule = Schedule(firstDate, endDate, tenor, cal)
    return [d.toDateTime() for d in schedule]
def makeSchedule(firstDate,
                 endDate,
                 tenor,
                 calendar='NullCalendar',
                 dateRule=BizDayConventions.Following,
                 dateGenerationRule=DateGeneration.Forward):

    cal = Calendar(calendar)
    firstDate = check_date(firstDate)
    endDate = check_date(endDate)
    tenor = check_period(tenor)

    if tenor.units() == TimeUnits.BDays:
        schedule = []
        if dateGenerationRule == DateGeneration.Forward:
            d = cal.adjustDate(firstDate, dateRule)
            while d <= endDate:
                schedule.append(d)
                d = cal.advanceDate(d, tenor, dateRule)
        elif dateGenerationRule == DateGeneration.Backward:
            d = cal.adjustDate(endDate, dateRule)
            while d >= firstDate:
                schedule.append(d)
                d = cal.advanceDate(d, -tenor, dateRule)
            schedule = sorted(schedule)
    else:
        schedule = Schedule(firstDate,
                            endDate,
                            tenor,
                            cal,
                            convention=dateRule,
                            dateGenerationRule=dateGenerationRule)
    return [d.toDateTime() for d in schedule]
    def testScheduleDeepCopy(self):
        startDate = Date(2013, 3, 31)
        endDate = Date(2013, 6, 30)
        tenor = Period('1m')
        cal = Calendar('NullCalendar')
        sch = Schedule(startDate, endDate, tenor, cal)
        copied_sch = copy.deepcopy(sch)

        self.assertEqual(sch, copied_sch)
Пример #7
0
def get_pos_adj_date(start_date,
                     end_date,
                     formats="%Y-%m-%d",
                     calendar='China.SSE',
                     freq='m',
                     return_biz_day=False):
    """
    :param start_date: str/datetime.datetime, start date of strategy
    :param end_date: str/datetime.datetime, end date of strat egy
    :param formats: optional, formats of the string date
    :param calendar: str, optional, name of the calendar to use in dates math
    :param freq: str, optional, the frequency of data
    :param return_biz_day: bool, optional, if the return dates are biz days
    :return: list of datetime.datetime, pos adjust dates
    """
    if isinstance(start_date, str) and isinstance(end_date, str):
        d_start_date = Date.strptime(start_date, formats)
        d_end_date = Date.strptime(end_date, formats)
    elif isinstance(start_date, datetime.datetime) and isinstance(
            end_date, datetime.datetime):
        d_start_date = Date.fromDateTime(start_date)
        d_end_date = Date.fromDateTime(end_date)

    cal = Calendar(calendar)
    pos_adjust_date = Schedule(d_start_date, d_end_date,
                               Period(length=1, units=_freqDict[freq]), cal,
                               BizDayConventions.Unadjusted)
    # it fails if setting dStartDate to be first adjustment date, then use Schedule to compute the others
    # so i first compute dates list in each period, then compute the last date of each period
    # last day of that period(month) is the pos adjustment date
    if _freqDict[freq] == TimeUnits.Weeks:
        pos_adjust_date = [
            Date.nextWeekday(date, Weekdays.Friday)
            for date in pos_adjust_date[:-1]
        ]
    elif _freqDict[freq] == TimeUnits.Months:
        pos_adjust_date = [
            cal.endOfMonth(date) for date in pos_adjust_date[:-1]
        ]
    elif _freqDict[freq] == TimeUnits.Years:
        pos_adjust_date = [
            Date(date.year(), 12, 31) for date in pos_adjust_date[:-1]
        ]

    if return_biz_day:
        pos_adjust_date = [
            cal.adjustDate(date, BizDayConventions.Preceding)
            for date in pos_adjust_date
        ]
    pos_adjust_date = [Date.toDateTime(date) for date in pos_adjust_date]
    pos_adjust_date = [
        date for date in pos_adjust_date if date <= d_end_date.toDateTime()
    ]

    return pos_adjust_date
Пример #8
0
def makeSchedule(firstDate,
                 endDate,
                 tenor,
                 calendar='NullCalendar',
                 dateRule=BizDayConventions.Following):

    cal = Calendar(calendar)
    firstDate = check_date(firstDate)
    endDate = check_date(endDate)
    tenor = Period(tenor)
    schedule = Schedule(firstDate, endDate, tenor, cal, convention=dateRule)
    return [d.toDateTime() for d in schedule]
    def testSchedulePickle(self):
        startDate = Date(2013, 3, 31)
        endDate = Date(2013, 6, 30)
        tenor = Period('1m')
        cal = Calendar('NullCalendar')
        sch = Schedule(startDate, endDate, tenor, cal)

        f = tempfile.NamedTemporaryFile('w+b', delete=False)
        pickle.dump(sch, f)
        f.close()

        with open(f.name, 'rb') as f2:
            pickled_sch = pickle.load(f2)
            self.assertEqual(sch, pickled_sch)

        os.unlink(f.name)