def __init__(self, alpha=None, retain_alpha_sign=True, universe='top2000', delay=1, capital=1e6): if universe in stock_universes: self.universe = universe uni_file = database_dir + universe + '.adj_ammend.mat' uni_var = scipy.io.loadmat(uni_file)[universe].astype(float) uni_var[uni_var == 0.] = np.nan self._uni_mask = uni_var else: raise KeyError('stock universe does not exist') self.alpha = alpha self.retain_alpha_sign = retain_alpha_sign if not isinstance(delay, int) or delay < 0: raise ValueError('delay must be int and >=0') self.delay = delay self.capital = capital self._market_data = {} dates = Simulator._read_var('dates') dates = [time.strptime(str(idate), "[%Y%m%d]") for idate in dates] dates = [datetime.datetime(*idate[0:6]) for idate in dates] self.dates = dates open_p = Simulator._read_var('open', self._uni_mask) open_ret1 = open_p / trans.ts_delay(open_p, 1) - 1.0 self._market_data['open_ret1'] = open_ret1 close_p = Simulator._read_var('close', self._uni_mask) close_ret1 = close_p / trans.ts_delay(close_p, 1) - 1.0 self._market_data['close_ret1'] = close_ret1 vwap = Simulator._read_var('vwap', self._uni_mask) vwap_ret1 = vwap / trans.ts_delay(vwap, 1) - 1.0 Simulator._clean_data('vwap_ret1', vwap_ret1) forward_vwap_ret1 = trans.ts_delay(vwap_ret1, -1 - self.delay) self._market_data['vwap_ret1'] = vwap_ret1 self._market_data['forward_vwap_ret1'] = forward_vwap_ret1 return
def momen_smooth_inner(mysim): momen_signal = np.sign( trans.ts_delay(trans.ts_mean(mysim('ret1'), window_width), window_width)) ret1_shock = np.zeros_like(mysim('ret1')) ret1_shock[mysim('ret1') > threshold] = 1 ret1_shock[mysim('ret1') < -threshold] = -1 pshock, nshock = stats_util.time_to_lastshock(ret1_shock) shock_index = np.logical_or(pshock < 60, nshock < 60) momen_signal[np.logical_and(momen_signal > 0, ~shock_index)] = np.nan momen_signal[np.logical_and(momen_signal < 0, shock_index)] = np.nan return momen_signal
def momen_longterm_inner(mysim): ave_past_ret = trans.ts_mean( mysim('ret1'), lookback_period[1] - lookback_period[0] + 1) ret1_shock = np.zeros_like(mysim('ret1')) ret1_shock[mysim('ret1') > threshold] = 1 ret1_shock[mysim('ret1') < -threshold] = -1 pshock, nshock = stats_util.time_to_lastshock(ret1_shock) shock_index = np.logical_or(pshock <= shock_effect_length, nshock <= shock_effect_length) ave_past_ret[shock_index] = np.nan ave_past_ret = trans.ts_delay(ave_past_ret, -lookback_period[1]) alpha = trans.ts_make_squarewave(ave_past_ret, hold_length) return alpha
def series_to_nfeatures(data2d, num_features, additional_feature=None): """from time series `data2d` select past `num_features` times to be used as features. For example, time series [0, 1, 3, 5] and num_features = 2 becomes [[nan, 0], [0, 1], [1, 3], [3, 5]] treat all the stocks as the same """ from trans import ts_delay num_stocks = data2d.shape[0] num_times = data2d.shape[1] if additional_feature is None: feature_vec = np.zeros((num_times*num_stocks, num_features)) else: feature_vec = np.zeros((num_times*num_stocks, num_features+1)) for i in range(0, num_features): feature_vec[:,i] = ts_delay(data2d, i).flatten() if additional_feature is not None: feature_vec[:,num_features] = np.tile(additional_feature, (1, num_stocks)) return feature_vec
def momen_HF_inner(mysim): return trans.ts_delay(mysim(varname), delayed_days)
def momen_longterm_inner(mysim): ave_past_ret = trans.ts_mean( mysim('ret1'), lookback_period[1] - lookback_period[0] + 1) ave_past_ret = trans.ts_delay(ave_past_ret, -lookback_period[1]) alpha = trans.ts_make_squarewave(ave_past_ret, hold_length) return alpha