def query_index_weights_daily(self, index, start_date, end_date): """ Return all securities that have been in index during start_date and end_date. Parameters ---------- index : str start_date : int end_date : int Returns ------- res : pd.DataFrame Index is trade_date, columns are symbols. """ start_dt = jutil.convert_int_to_datetime(start_date) start_dt_extended = start_dt - pd.Timedelta(days=45) start_date_extended = jutil.convert_datetime_to_int(start_dt_extended) trade_dates = self.query_trade_dates(start_date_extended, end_date) df_weight_raw = self.query_index_weights_range(index, start_date=start_date_extended, end_date=end_date) res = df_weight_raw.reindex(index=trade_dates) res = res.fillna(method='ffill') res = res.loc[res.index >= start_date] res = res.loc[res.index <= end_date] mask_col = res.sum(axis=0) > 0 return res res = res.loc[:, mask_col]
def query_index_weights_daily(self, index, start_date, end_date): """ Return all securities that have been in index during start_date and end_date. Parameters ---------- index : str start_date : int end_date : int Returns ------- res : pd.DataFrame Index is trade_date, columns are symbols. """ start_dt = jutil.convert_int_to_datetime(start_date) start_dt_extended = start_dt - pd.Timedelta(days=45) start_date_extended = jutil.convert_datetime_to_int(start_dt_extended) trade_dates = self.query_trade_dates(start_date_extended, end_date) df_weight_raw = self.query_index_weights_range(index, start_date=start_date_extended, end_date=end_date) res = df_weight_raw.reindex(index=trade_dates) res = res.fillna(method='ffill') res = res.loc[res.index >= start_date] res = res.loc[res.index <= end_date] mask_col = res.sum(axis=0) > 0 res = res.loc[:, mask_col] return res
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 _get_first_period_day(self): current = self.start_date current_date = jutil.convert_int_to_datetime(current) period = self.ctx.strategy.period # set current date to first date of current period # set offset to first date of previous period if period == 'day': offset = pd.tseries.offsets.BDay() elif period == 'week': offset = pd.tseries.offsets.Week(weekday=0) current_date -= pd.tseries.offsets.Day(current_date.weekday()) elif period == 'month': offset = pd.tseries.offsets.BMonthBegin() current_date -= pd.tseries.offsets.Day(current_date.day - 1) else: raise NotImplementedError( "Frequency as {} not support".format(period)) current_date -= offset * self.ctx.strategy.n_periods current = jutil.convert_datetime_to_int(current_date) return self._get_next_period_day( current, self.ctx.strategy.period, n=self.ctx.strategy.n_periods, extra_offset=self.ctx.strategy.days_delay)
def matrix_from_different_date(): dates = pd.date_range(end='2018-09-08', periods=5, freq='3M') dataset = pd.DataFrame() for dt in dates: dataset = dataset.append( last_center_matrix(end_date=jutil.convert_datetime_to_int(dt), target=20)) return dataset
def get_next_trade_date(self, date): """ Parameters ---------- date : int Returns ------- res : int """ dt = jutil.convert_int_to_datetime(date) delta = pd.Timedelta(weeks=2) dt_new = dt + delta date_new = jutil.convert_datetime_to_int(dt_new) dates = self.get_trade_date_range(date, date_new) mask = dates > date res = dates[mask][0] return res
def get_last_trade_date(self, date): """ Parameters ---------- date : int Returns ------- res : int """ dt = jutil.convert_int_to_datetime(date) delta = pd.Timedelta(weeks=2) dt_old = dt - delta date_old = jutil.convert_datetime_to_int(dt_old) dates = self.get_trade_date_range(date_old, date) mask = dates < date res = dates[mask][-1] return res
def query_last_trade_date(self, date): """ Parameters ---------- date : int Returns ------- res : int """ dt = jutil.convert_int_to_datetime(date) delta = pd.Timedelta(weeks=2) dt_old = dt - delta date_old = jutil.convert_datetime_to_int(dt_old) dates = self.query_trade_dates(date_old, date) mask = dates < date res = dates[mask][-1] return int(res)
def query_next_trade_date(self, date, n=1): """ Parameters ---------- date : int n : int, optional Next n trade dates, default 0 (next trade date). Returns ------- res : int """ dt = jutil.convert_int_to_datetime(date) delta = pd.Timedelta(weeks=(n // 7 + 2)) dt_new = dt + delta date_new = jutil.convert_datetime_to_int(dt_new) dates = self.query_trade_dates(date, date_new) mask = dates > date res = dates[mask][n - 1] return res
def query_next_trade_date(self, date, n=1): """ Parameters ---------- date : int n : int, optional Next n trade dates, default 0 (next trade date). Returns ------- res : int """ dt = jutil.convert_int_to_datetime(date) delta = pd.Timedelta(weeks=(n // 7 + 2)) dt_new = dt + delta date_new = jutil.convert_datetime_to_int(dt_new) dates = self.query_trade_dates(date, date_new) mask = dates > date res = dates[mask][n-1] return int(res)
def _get_current_date(): now_utc = datetime.datetime.utcnow() now_utc8 = now_utc + datetime.timedelta(hours=8) now_date = jutil.convert_datetime_to_int(now_utc8) return now_date
def _get_next_period_day(self, current, period, n=1, extra_offset=0): """ Get the n'th day in next period from current day. Parameters ---------- current : int Current date in format "%Y%m%d". period : str Interval between current and next. {'day', 'week', 'month'} n : int n times period. extra_offset : int n'th business day after next period. Returns ------- nxt : int """ #while True: #for _ in range(4): current_date = jutil.convert_int_to_datetime(current) while current <= self.end_date: if period == 'day': offset = pd.tseries.offsets.BDay() # move to next business day if extra_offset < 0: raise ValueError("Wrong offset for day period") elif period == 'week': offset = pd.tseries.offsets.Week( weekday=0) # move to next Monday if extra_offset < -5: raise ValueError("Wrong offset for week period") elif period == 'month': offset = pd.tseries.offsets.BMonthBegin( ) # move to first business day of next month if extra_offset < -31: raise ValueError("Wrong offset for month period") else: raise NotImplementedError( "Frequency as {} not support".format(period)) offset = offset * n begin_date = current_date + offset if period == 'day': end_date = begin_date + pd.tseries.offsets.Day() * 366 elif period == 'week': end_date = begin_date + pd.tseries.offsets.Day() * 6 elif period == 'month': end_date = begin_date + pd.tseries.offsets.BMonthBegin( ) #- pd.tseries.offsets.BDay() if extra_offset > 0: next_date = begin_date + extra_offset * pd.tseries.offsets.BDay( ) if next_date >= end_date: next_date = end_date - pd.tseries.offsets.BDay() elif extra_offset < 0: next_date = end_date + extra_offset * pd.tseries.offsets.BDay() if next_date < begin_date: next_date = begin_date else: next_date = begin_date date = next_date while date < end_date: nxt = jutil.convert_datetime_to_int(date) if self._is_trade_date(nxt): return nxt date += pd.tseries.offsets.BDay() date = next_date while date >= begin_date: nxt = jutil.convert_datetime_to_int(date) if self._is_trade_date(nxt): return nxt date -= pd.tseries.offsets.BDay() # no trading day in this period, try next period current_date = end_date - pd.tseries.offsets.BDay() current = jutil.convert_datetime_to_int(current_date) # Check if there are more trade dates self._get_next_trade_date(current) raise ValueError("no trading day after {0}".format(current))