def test_dtutil(): import datetime date = 20170105 dt = jutil.convert_int_to_datetime(date) assert jutil.shift(date, 2) == 20170119 assert jutil.convert_datetime_to_int(jutil.shift(dt, 2)) == 20170119 t = 145539 dt = jutil.combine_date_time(date, t) assert jutil.split_date_time(dt) == (date, t) assert jutil.get_next_period_day(date, 'day', 1, 1) == 20170109 assert jutil.get_next_period_day(date, 'week', 2, 0) == 20170116 assert jutil.get_next_period_day(date, 'month', 1, 0) == 20170201 date = 20170808 assert jutil.get_next_period_day(20170831, 'day', extra_offset=1) == 20170904 assert jutil.get_next_period_day(20170831, 'day', n=2, extra_offset=0) == 20170904 assert jutil.get_next_period_day(20170831, 'day', n=7, extra_offset=0) == 20170911 assert jutil.get_next_period_day(20170831, 'week', extra_offset=1) == 20170905 assert jutil.get_next_period_day(20170831, 'month', extra_offset=0) == 20170901 monthly = 20170101 while monthly < 20180301: monthly = jutil.get_next_period_day(monthly, 'month', extra_offset=0) assert datetime.datetime.strptime(str(monthly), "%Y%m%d").weekday() < 5
def init_from_config(self, props, data_api): """ Initialize various attributes like start/end date, universe/symbol, fields, etc. If your want to parse symbol, but use a custom benchmark index, please directly assign self.data_benchmark. Parameters ---------- props : dict start_date, end_date, freq, symbol, fields, etc. data_api : BaseDataServer """ # data_api.init_from_config(props) self.data_api = data_api sep = ',' # initialize parameters self.start_date = props['start_date'] self.extended_start_date = jutil.shift(self.start_date, n_weeks=-1) # query more data self.end_date = props['end_date'] self.adjust_mode = props.get("adjust_mode", "post") # get and filter fields fields = props.get('fields', []) if fields: fields = props['fields'].split(sep) self.fields = [field for field in fields if self._is_predefined_field(field)] if len(self.fields) < len(fields): print("Field name [{}] not valid, ignore.".format(set.difference(set(fields), set(self.fields)))) # initialize universe/symbol universe = props.get('universe', "") symbol = props.get('symbol', "") benchmark = props.get('benchmark', '') if symbol and universe: raise ValueError("Please use either [symbol] or [universe].") if not (symbol or universe): raise ValueError("One of [symbol] or [universe] must be provided.") if universe: univ_list = universe.split(',') self.universe = univ_list symbols_list = [] for univ in univ_list: symbols_list.extend(data_api.query_index_member(univ, self.extended_start_date, self.end_date)) self.symbol = sorted(list(set(symbols_list))) else: self.symbol = sorted(symbol.split(sep)) if benchmark: self.benchmark = benchmark else: if self.universe: if len(self.universe) > 1: print("More than one universe are used: {}, " "use the first one ({}) as index by default. " "If you want to use other benchmark, " "please specify benchmark in configs.".format(repr(self.universe), self.universe[0])) self.benchmark = self.universe[0] print("Initialize config success.")