def discretize_ta_features(self, ta): """ Returns a dict containing the discretized version of the input dict of technical indicators """ dta = {} dta['rsma5_20'] = to_discrete_single(ta['rsma5_20'], 0) dta['rsma8_15'] = to_discrete_single(ta['rsma8_15'], 0) dta['rsma20_50'] = to_discrete_single(ta['rsma20_50'], 0) dta['rema5_20'] = to_discrete_single(ta['rema5_20'], 0) dta['rema8_15'] = to_discrete_single(ta['rema8_15'], 0) dta['rema20_50'] = to_discrete_single(ta['rema20_50'], 0) dta['macd_12_26'] = to_discrete_single(ta['macd_12_26'], 0) dta['ao'] = to_discrete_single(ta['ao'], 0) dta['adx'] = to_discrete_single(ta['adx'], 20) dta['wd'] = to_discrete_single(ta['wd'], 0) dta['ppo'] = to_discrete_single(ta['ppo'], 0) dta['rsi'] = to_discrete_double(ta['rsi'], 30, 70) dta['mfi'] = to_discrete_double(ta['mfi'], 30, 70) dta['tsi'] = to_discrete_double(ta['tsi'], -25, 25) dta['stoch'] = to_discrete_double(ta['stoch'], 20, 80) dta['cmo'] = to_discrete_double(ta['cmo'], -50, 50) dta['atrp'] = to_discrete_single(ta['atrp'], 30) dta['pvo'] = to_discrete_single(ta['pvo'], 0) dta['fi13'] = to_discrete_single(ta['fi13'], 0) dta['fi50'] = to_discrete_single(ta['fi50'], 0) dta['adi'] = to_discrete_single(ta['adi'], 0) dta['obv'] = to_discrete_single(ta['obv'], 0) for k in dta.keys(): dta[k] = [ np.nan if np.isnan(x) else np.asscalar(x) for x in dta[k] ] return dta
def discretize_ta_features(ta: pd.DataFrame): """ Returns a dict containing the discretized version of the input dict of technical indicators """ dta = pd.DataFrame(index=ta.index) for k in ta.columns: if k.startswith(('rsma', 'rema', 'macd', 'pvo', 'fi', 'adi', 'obv', 'ao', 'wd', 'ppo')): dta[k] = to_discrete_single(ta[k].values, 0) elif k.startswith('adx'): dta[k] = to_discrete_single(ta[k].values, 20) elif k.startswith('rsi'): dta[k] = to_discrete_double(ta[k].values, 30, 70) elif k.startswith('mfi'): dta[k] = to_discrete_double(ta[k].values, 30, 70) elif k.startswith('tsi'): dta[k] = to_discrete_double(ta[k].values, -25, 25) elif k.startswith('stoch'): dta[k] = to_discrete_double(ta[k].values, 20, 80) elif k.startswith('cmo'): dta[k] = to_discrete_double(ta[k].values, -50, 50) elif k.startswith('atrp'): dta[k] = to_discrete_single(ta[k].values, 30) return dta
def discretize_ta_features_old(ta): """ Returns a dict containing the discretized version of the input dict of technical indicators """ dta = {} for k, v in ta.items(): if k.startswith([ 'rsma', 'rema', 'macd', 'pvo', 'fi', 'adi', 'obv', 'ao', 'wd', 'ppo' ]): dta[k] = to_discrete_single(v, 0) elif k.startswith('adx'): dta[k] = to_discrete_single(v, 20) elif k.startswith('rsi'): dta[k] = to_discrete_double(v, 30, 70) elif k.startswith('mfi'): dta[k] = to_discrete_double(v, 30, 70) elif k.startswith('tsi'): dta[k] = to_discrete_double(v, -25, 25) elif k.startswith('stoch'): dta[k] = to_discrete_double(v, 20, 80) elif k.startswith('cmo'): dta[k] = to_discrete_double(v, -50, 50) elif k.startswith('atrp'): dta[k] = to_discrete_single(v, 30) for k in dta.keys(): dta[k] = [np.nan if np.isnan(x) else np.asscalar(x) for x in dta[k]] return dta
def predict(self, x, **kwargs): if not self.model: return None try: forecast = self.model.forecast(steps=x.shape[0]) return to_discrete_double(forecast, -0.01, 0.01) except (ValueError, np.linalg.linalg.LinAlgError): logger.error('{}: convergence error'.format(str(self.model.get_params())))
def predict(self, x, **kwargs): try: forecast = self.model.forecast(steps=x.shape[0]) return to_discrete_double(forecast, -0.01, 0.01) except (ValueError, np.linalg.linalg.LinAlgError): logger.error( 'Exponential Smoothing convergence error (a:{},b:{})'.format( self.params.get('alpha'), self.params.get('beta'))) return
def predict(self, x, **kwargs): if not self.model: return None try: forecast = self.model.forecast(steps=x.shape[0]) return to_discrete_double(forecast, -0.01, 0.01) except (ValueError, np.linalg.linalg.LinAlgError): logger.error('ARIMA convergence error (order {} {} {})'.format( self.params['order'][0], self.params['order'][1], self.params['order'][2]))
def predict(self, X): params = self.get_params() # Check is fit had been called check_is_fitted(self) if not self._model: return None try: forecast = self._model.forecast(steps=X.shape[0]) self.y_ = to_discrete_double(forecast, -0.01, 0.01) except (ValueError, np.linalg.linalg.LinAlgError): logger.error( 'predict: ARIMA convergence error (order {} {} {})'.format( params['p'], params['d'], params['q']))
def build_ta_dataset(self): ohlcv, ohlcv_target = self.datasets[DatasetType.OHLCV] if ohlcv is None: raise RuntimeError('No ohlcv loaded!') ta = self.get_ta_features(ohlcv['high'].values, ohlcv['low'].values, ohlcv['close'].values, ohlcv['volume'].values) dta = self.discretize_ta_features(ta) continuous = pd.DataFrame(index=ohlcv.index) discrete = pd.DataFrame(index=ohlcv.index) variation = pd.DataFrame(index=ohlcv.index) for k, dk in zip( ta.keys(), dta.keys()): # Keys are the same both for 'ta' and 'dta' continuous[k] = ta[k] discrete[dk] = dta[dk] #variation[k] = pct_variation(ta[k], -1) variation[k] = continuous[k].pct_change(periods=1) #pct_var = pct_variation(ohlcv['close'].values, 1) # set to number of forecast periods pct_var = pd.Series(np.roll(ohlcv.close.pct_change(periods=1), -1), index=ohlcv.index) classes = to_discrete_double(pct_var.fillna(method='ffill'), -0.01, 0.01) ## For debugging ds # continuous['close'] = ohlcv['close'].values # continuous['next_close'] = future(ohlcv['close'].values, 1) # continuous['discrete'] = classes # continuous['target'] = pct_var # discrete['target'] = classes # self.features[DatasetType.CONTINUOUS_TA] = continuous # self.features[DatasetType.DISCRETE_TA] = discrete self.datasets[DatasetType.CONTINUOUS_TA] = ( continuous, pd.Series(pct_var, index=continuous.index)) self.datasets[DatasetType.VARIATION_TA] = (variation, pd.Series( pct_var, index=continuous.index)) self.datasets[DatasetType.DISCRETE_TA] = (discrete, pd.Series( classes, index=discrete.index)) return continuous, discrete