def date_stamps(start_date, end_date): start_date = Date.fromDateTime(start_date) end_date = Date.fromDateTime(end_date) start_year = start_date.year() start_month = start_date.month() if start_month <= 3: start_month = 3 elif start_month <= 6: start_month = 6 elif start_month <= 9: start_month = 9 else: start_month = 12 stamps = [] start_point = Date(start_year, start_month, 1) start_point = Date.endOfMonth(start_point) while start_point <= end_date: stamps.append(start_point.toDateTime().strftime('%Y%m%d')) start_point = Date.endOfMonth(start_point + '3m') return stamps
def bizDatesList(holidayCenter, fromDate, toDate): cal = Calendar(holidayCenter) fromDate = Date.fromDateTime(fromDate) toDate = Date.fromDateTime(toDate) assert fromDate <= toDate, "from date ({0} must be earlier than to date {1}".format( fromDate, toDate) return [d.toDateTime() for d in cal.bizDatesList(fromDate, toDate)]
def datesList(fromDate, toDate): fromDate = Date.fromDateTime(fromDate) toDate = Date.fromDateTime(toDate) assert fromDate <= toDate, "from date ({0} must be earlier than to date {1}".format( fromDate, toDate) return [ Date.fromExcelSerialNumber(serial).toDateTime() for serial in range(fromDate.serialNumber, toDate.serialNumber + 1) ]
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
def advanceDateByCalendar(holidayCenter, referenceDate, period, convention=BizDayConventions.Following): cal = Calendar(holidayCenter) refer = Date.fromDateTime(referenceDate) return cal.advanceDate(refer, period, convention).toDateTime()
def __init__(self, layer_factor, alpha_factor, sec_return, tiaocang_date, tiaocang_date_window_size=12, save_sec_score=True, factor_weight_type=FactorWeightType.ICWeight, alpha_factor_sign=None, na_handler=FactorNAHandler.ReplaceWithMedian): self._layerFactor = layer_factor self._layerFactorNames = [ layer_factor.name for layer_factor in self._layerFactor ] self._alphaFactor = alpha_factor self._alphaFactorNames = [ alpha_factor.name for alpha_factor in self._alphaFactor ] self._secReturn = sec_return self._tiaoCangDate = tiaocang_date self._startDate = str(Date.fromDateTime(self._tiaoCangDate[0])) self._endDate = str(Date.fromDateTime(self._tiaoCangDate[-1])) self._tiaoCangDateWindowSize = tiaocang_date_window_size pyFinAssert( len(self._tiaoCangDate) > self._tiaoCangDateWindowSize, ValueError, "length of tiaoCangDate must be larger than moving window size") self._saveSecScore = save_sec_score self._factorWeightType = factor_weight_type self._alphaFactorSign = alpha_factor_sign self._na_handler = na_handler self._rank_ic = None if self._factorWeightType == FactorWeightType.EqualWeight: pyFinAssert( len(self._alphaFactorSign) == len(self._alphaFactor), ValueError, "length of alpha_factor_sign({0}), does not equal to that of alpha factor({1})" .format(len(self._alphaFactorSign), len(self._alphaFactor)))
def map_to_biz_day(date_series, calendar='China.SSE', convention=BizDayConventions.Preceding): """ :param date_series: pd.Sereis, datetime.datetime :param calendar: str, optional, name of the calendar to use in dates math :param convention: str, optional, pyFin date conventions :return: pd.Series, datetime.datetime 用更快的方式计算, 避免对每个日期进行循环 """ unique_date_list = sorted(set(date_series)) py_date_list = [Date.fromDateTime(date) for date in unique_date_list] py_date_list = [ Calendar(calendar).adjustDate(date, convention) for date in py_date_list ] biz_day_list = [Date.toDateTime(date) for date in py_date_list] dict_date_map = dict(zip(unique_date_list, biz_day_list)) ret = date_series.map(dict_date_map) return ret
def get_report_date(act_date, return_biz_day=True): """ :param act_date: str/datetime.datetime, 任意日期 :param return_biz_day: bool, 是否返回交易日 :return: datetime, 对应应使用的报告日期, 从wind数据库中爬取 此函数的目的是要找到,任意时刻可使用最新的季报数据的日期,比如2-20日可使用的最新季报是去年的三季报(对应日期为9-30), """ if isinstance(act_date, str): act_date = Date.strptime(act_date) elif isinstance(act_date, datetime.datetime): act_date = Date.fromDateTime(act_date) act_month = act_date.month() act_year = act_date.year() if 1 <= act_month <= 3: # 第一季度使用去年三季报的数据 year = act_year - 1 month = 9 day = 30 elif 4 <= act_month <= 7: # 第二季度使用当年一季报 year = act_year month = 3 day = 31 elif 8 <= act_month <= 9: # 第三季度使用当年中报 year = act_year month = 6 day = 30 else: year = act_year # 第四季度使用当年三季报 month = 9 day = 30 if return_biz_day: date_adj = Calendar('China.SSE').adjustDate( Date(year, month, day), BizDayConventions.Preceding) ret = date_adj.toDateTime() else: ret = datetime.datetime(year, month, day) return ret
def check_date(date): from PyFin.DateUtilities import Date if isinstance(date, str): return Date.strptime(date, dateFormat='%Y-%m-%d') else: return Date.fromDateTime(date)
def advanceDateByCalendar(holidayCenter, referenceDate, period, convention=BizDayConventions.Following): cal = Calendar(holidayCenter) refer = Date.fromDateTime(referenceDate) return cal.advanceDate(refer, period, convention).toDateTime()
def advanceDate(referenceDate, period): d = Date.fromDateTime(referenceDate) + period return d.toDateTime()
def holDatesList(holidayCenter, fromDate, toDate, includeWeekend=True): cal = Calendar(holidayCenter) fromDate = Date.fromDateTime(fromDate) toDate = Date.fromDateTime(toDate) assert fromDate <= toDate, "from date ({0} must be earlier than to date {1}".format(fromDate, toDate) return [d.toDateTime() for d in cal.holDatesList(fromDate, toDate, includeWeekend)]
def datesList(fromDate, toDate): fromDate = Date.fromDateTime(fromDate) toDate = Date.fromDateTime(toDate) assert fromDate <= toDate, "from date ({0} must be earlier than to date {1}".format(fromDate, toDate) return [Date.fromExcelSerialNumber(serial).toDateTime() for serial in range(fromDate.serialNumber, toDate.serialNumber + 1)]
def isBizDay(holidayCenter, ref): cal = Calendar(holidayCenter) ref = Date.fromDateTime(ref) return cal.isBizDay(ref)