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))
def testBasicFunctions(self): testDate = Date(2015, 7, 11) cal = Calendar('China.SSE') self.assertTrue(cal.isWeekEnd(testDate.weekday()), "{0} is expected to be a weekend".format(testDate)) testDate = Date(2015, 7, 13) self.assertTrue(not cal.isWeekEnd(testDate.weekday()), "{0} is expected not to be a weekend".format(testDate)) testDate = Date(2015, 5, 29) cal = Calendar('China.SSE') self.assertTrue( cal.isEndOfMonth(testDate), "{0} is expected to be a end of month".format(testDate)) testDate = Date(2015, 5, 1) cal = Calendar('China.SSE') endOfMonth = cal.endOfMonth(testDate) self.assertEqual( endOfMonth, Date(2015, 5, 29), "The month end of 2015/5 is expected to be {0}".format( Date(2015, 5, 29))) bizDates1 = cal.bizDaysBetween(Date(2015, 1, 1), Date(2015, 12, 31), True, False) bizDates2 = cal.bizDaysBetween(Date(2015, 12, 31), Date(2015, 1, 1), False, True) self.assertEqual(bizDates1, bizDates2)
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))