示例#1
0
    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
        start_date = Date(2012, 1, 1)
        s = Schedule(start_date, start_date + 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.
        start_date = Date(2012, 1, 17)
        s = Schedule(start_date, start_date + 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)
示例#2
0
    def testScheduleDeepCopy(self):
        start_date = Date(2013, 3, 31)
        end_date = Date(2013, 6, 30)
        tenor = Period('1m')
        cal = Calendar('NullCalendar')
        sch = Schedule(start_date, end_date, tenor, cal)
        copied_sch = copy.deepcopy(sch)

        self.assertEqual(sch, copied_sch)
示例#3
0
 def testScheduleInitializeWithYearly(self):
     start_date = Date(2012, 2, 29)
     end_date = Date(2013, 3, 1)
     tenor = Period('1y')
     cal = Calendar('NullCalendar')
     sch = Schedule(start_date, end_date, 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])
示例#4
0
 def testScheduleInitialize(self):
     start_date = Date(2013, 3, 31)
     end_date = Date(2013, 6, 30)
     tenor = Period('1m')
     cal = Calendar('NullCalendar')
     sch = Schedule(start_date, end_date, 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])
示例#5
0
    def testSchedulePickle(self):
        start_date = Date(2013, 3, 31)
        end_date = Date(2013, 6, 30)
        tenor = Period('1m')
        cal = Calendar('NullCalendar')
        sch = Schedule(start_date, end_date, 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)
示例#6
0
def get_tiaocang_date(start_date,
                      end_date,
                      freq=FreqType.EOM,
                      calendar='China.SSE',
                      date_format='%Y-%m-%d'):
    """
    :param start_date: str/datetime.datetime, 开始日期
    :param end_date: str/datetime.datetime, 结束日期
    :param freq: str enum, default=EOM, 月度
    :param calendar: str, 日历名称
    :param date_format: str, start_date/end_date 如果是str格式,其格式的日期形式
    :return: list, datetime.datetime
    返回在开始日至结束日之间的调仓日(交易日)
    """
    calendar = calendar
    date_format = date_format
    start_date = ensure_pyfin_date(start_date, date_format)
    end_date = ensure_pyfin_date(end_date, date_format)

    cal = Calendar(calendar)
    tiaocang_date = Schedule(start_date, end_date, Period('1' + freq), cal,
                             BizDayConventions.Unadjusted)

    if freq.upper() == FreqType.EOW:
        tiaocang_date = [
            Date.nextWeekday(date, Weekdays.Friday) for date in tiaocang_date
        ]
    elif freq.upper() == FreqType.EOM:
        tiaocang_date = [cal.endOfMonth(date) for date in tiaocang_date]
    elif freq.upper() == FreqType.EOY:
        tiaocang_date = [Date(date.year(), 12, 31) for date in tiaocang_date]

    tiaocang_date = [
        cal.adjustDate(date, BizDayConventions.Preceding).toDateTime()
        for date in tiaocang_date if start_date <= date <= end_date
    ]

    return sorted(set(tiaocang_date))
示例#7
0
    def testAdvanceDate(self):
        referenceDate = Date(2014, 1, 31)
        sseCal = Calendar('China.SSE')
        ibCal = Calendar('China.IB')

        bizDayConv = BizDayConventions.Following

        # test null period
        self.assertEqual(
            sseCal.advanceDate(referenceDate, Period('0b'), bizDayConv),
            Date(2014, 2, 7))

        # test negative period
        self.assertEqual(
            sseCal.advanceDate(referenceDate, Period('-5b'), bizDayConv),
            Date(2014, 1, 24))

        # The difference is caused by Feb 8 is SSE holiday but a working day for IB market
        self.assertEqual(
            sseCal.advanceDate(referenceDate, Period('2b'), bizDayConv),
            Date(2014, 2, 10))
        self.assertEqual(
            sseCal.advanceDate(referenceDate, Period('2d'), bizDayConv),
            Date(2014, 2, 7))
        self.assertEqual(
            ibCal.advanceDate(referenceDate, Period('2b'), bizDayConv),
            Date(2014, 2, 8))
        self.assertEqual(
            ibCal.advanceDate(referenceDate, Period('2d'), bizDayConv),
            Date(2014, 2, 7))

        bizDayConv = BizDayConventions.ModifiedFollowing
        # May 31, 2014 is a holiday
        self.assertEqual(
            sseCal.advanceDate(referenceDate, Period('4m'), bizDayConv, True),
            Date(2014, 5, 30))
示例#8
0
    def test_BasicFunctions(self):
        year = 2015
        month = 7
        day = 24
        str_repr = "{0}-{1:02d}-{2:02d}".format(year, month, day)
        inner_repr = "Date({0}, {1}, {2})".format(year, month, day)

        test_date = Date(year, month, day)
        self.assertEqual(str(test_date), str_repr, "date string:\n"
                                                   "expected:   {0:s}\n"
                                                   "calculated: {1:s}".format(str_repr, str(test_date)))

        self.assertEqual(repr(test_date), inner_repr, "date representation:\n"
                                                      "expected:   {0:s}\n"
                                                      "calculated: {1:s}".format(inner_repr, repr(test_date)))

        self.assertEqual(test_date.year(), year, "date year:\n"
                                                 "expected:   {0:d}\n"
                                                 "calculated: {1:d}".format(year, test_date.year()))

        self.assertEqual(test_date.month(), month, "date month:\n"
                                                   "expected:   {0:d}\n"
                                                   "calculated: {1:d}".format(month, test_date.month()))

        self.assertEqual(test_date.dayOfMonth(), day, "date day:\n"
                                                      "expected:   {0:d}\n"
                                                      "calculated: {1:d}".format(day, test_date.dayOfMonth()))

        self.assertEqual(test_date.dayOfYear(), test_date - Date(2015, 1, 1) + 1, "date day:\n"
                                                                                  "expected:   {0:d}\n"
                                                                                  "calculated: {1:d}"
                         .format(test_date - Date(2015, 1, 1) + 1, test_date.dayOfYear()))
        self.assertEqual(test_date.weekday(), 6, "date weekday:\n"
                                                 "expected:   {0:d}\n"
                                                 "calculated: {1:d}".format(5, test_date.weekday()))

        self.assertEqual(test_date.toDateTime(), dt.datetime(year, month, day), "date datetime representation\n"
                                                                                "expected:   {0}\n"
                                                                                "calculated: {1}".format(
            dt.datetime(year, month, day), test_date.toDateTime()))

        serial_number = test_date.serialNumber
        serial_date = Date(serial_number=serial_number)

        self.assertEqual(serial_date, test_date, "date excel serial number representation\n"
                                                 "expected:   {0:d}"
                                                 "calculated: {1:d}".format(serial_date.serialNumber,
                                                                            test_date.serialNumber))

        # test comparisons
        previous_date = test_date - 1
        self.assertTrue(previous_date < test_date, "{0} is not earlier than {1}".format(previous_date, test_date))
        self.assertFalse(previous_date >= test_date,
                         "{0} should not be later than or equal to {1}".format(previous_date, test_date))
        self.assertTrue((previous_date + 1) == test_date,
                        "{0} plus one day should be equal to {1}".format(previous_date, test_date))

        # check static members
        self.assertEqual(Date.minDate(), Date(1901, 1, 1), "min date is wrong")
        self.assertEqual(Date.maxDate(), Date(2199, 12, 31), "max date is wrong")
        self.assertEqual(Date.endOfMonth(test_date), Date(year, month, 31), "end of month is wrong")
        self.assertTrue(Date.isEndOfMonth(Date(year, month, 31)), "{0} should be the end of month")
        self.assertEqual(Date.nextWeekday(test_date, test_date.weekday()), test_date,
                         "{0}'s next same week day should be {1}"
                         .format(test_date, test_date))
        expected_date = dt.date.today()
        expected_date = dt.datetime(expected_date.year, expected_date.month, expected_date.day)
        self.assertEqual(Date.todaysDate().toDateTime(), expected_date, "today's date\n"
                                                                        "expected:   {0}\n"
                                                                        "calculated: {1}".format(expected_date,
                                                                                                 Date.todaysDate()))

        # nth-week day
        with self.assertRaises(ValueError):
            _ = Date.nthWeekday(0, Weekdays.Friday, 1, 2015)

        with self.assertRaises(ValueError):
            _ = Date.nthWeekday(6, Weekdays.Friday, 1, 2015)

        self.assertEqual(Date.nthWeekday(3, Weekdays.Wednesday, 8, 2015), Date(2015, 8, 19))

        # check plus/sub

        three_weeks_after = test_date + '3W'
        expected_date = test_date + 21
        self.assertEqual(three_weeks_after, expected_date, "date + 3w period\n"
                                                           "expected:   {0}\n"
                                                           "calculated: {1}".format(expected_date, three_weeks_after))

        three_months_before = test_date - "3M"
        expected_date = Date(year, month - 3, day)
        self.assertEqual(three_months_before, expected_date, "date - 3m period\n"
                                                             "expected:   {0}\n"
                                                             "calculated: {1}".format(expected_date,
                                                                                      three_months_before))

        three_months_before = test_date - Period("3M")
        expected_date = Date(year, month - 3, day)
        self.assertEqual(three_months_before, expected_date, "date - 3m period\n"
                                                             "expected:   {0}\n"
                                                             "calculated: {1}".format(expected_date,
                                                                                      three_months_before))

        three_months_after = test_date + "3m"
        expected_date = Date(year, month + 3, day)
        self.assertEqual(three_months_after, expected_date, "date + 3m period\n"
                                                            "expected:   {0}\n"
                                                            "calculated: {1}".format(expected_date, three_months_after))

        one_year_and_two_months_before = test_date - "14m"
        expected_date = Date(year - 1, month - 2, day)
        self.assertEqual(one_year_and_two_months_before, expected_date, "date - 14m period\n"
                                                                        "expected:   {0}\n"
                                                                        "calculated: {1}".format(expected_date,
                                                                                                 three_months_before))

        one_year_and_two_months_before = test_date + "14m"
        expected_date = Date(year + 1, month + 2, day)
        self.assertEqual(one_year_and_two_months_before, expected_date, "date + 14m period\n"
                                                                        "expected:   {0}\n"
                                                                        "calculated: {1}".format(expected_date,
                                                                                                 three_months_before))

        five_months_after = test_date + "5m"
        expected_date = Date(year, month + 5, day)
        self.assertEqual(five_months_after, expected_date, "date + 5m period\n"
                                                           "expected:   {0}\n"
                                                           "calculated: {1}".format(expected_date, five_months_after))