def generate_feature(data): high = data.High.values low = data.Low.values close = data.Close.values feature_df = pd.DataFrame(index=data.index) feature_df["ADX"] = ADX = talib.ADX(high, low, close, timeperiod=14) feature_df["ADXR"] = ADXR = talib.ADXR(high, low, close, timeperiod=14) feature_df["APO"] = APO = talib.APO(close, fastperiod=12, slowperiod=26, matype=0) feature_df["AROONOSC"] = AROONOSC = talib.AROONOSC(high, low, timeperiod=14) feature_df["CCI"] = CCI = talib.CCI(high, low, close, timeperiod=14) feature_df["CMO"] = CMO = talib.CMO(close, timeperiod=14) feature_df["DX"] = DX = talib.DX(high, low, close, timeperiod=14) feature_df["MINUS_DI"] = MINUS_DI = talib.MINUS_DI(high, low, close, timeperiod=14) feature_df["MINUS_DM"] = MINUS_DM = talib.MINUS_DM(high, low, timeperiod=14) feature_df["MOM"] = MOM = talib.MOM(close, timeperiod=10) feature_df["PLUS_DI"] = PLUS_DI = talib.PLUS_DI(high, low, close, timeperiod=14) feature_df["PLUS_DM"] = PLUS_DM = talib.PLUS_DM(high, low, timeperiod=14) feature_df["PPO"] = PPO = talib.PPO(close, fastperiod=12, slowperiod=26, matype=0) feature_df["ROC"] = ROC = talib.ROC(close, timeperiod=10) feature_df["ROCP"] = ROCP = talib.ROCP(close, timeperiod=10) feature_df["ROCR100"] = ROCR100 = talib.ROCR100(close, timeperiod=10) feature_df["RSI"] = RSI = talib.RSI(close, timeperiod=14) feature_df["ULTOSC"] = ULTOSC = talib.ULTOSC(high, low, close, timeperiod1=7, timeperiod2=14, timeperiod3=28) feature_df["WILLR"] = WILLR = talib.WILLR(high, low, close, timeperiod=14) feature_df = feature_df.fillna(0.0) matrix = np.stack(( ADX, ADXR, APO, AROONOSC, CCI, CMO, DX, MINUS_DI, ROCR100, ROC, MINUS_DM, MOM, PLUS_DI, PLUS_DM, PPO, ROCP, WILLR, ULTOSC, RSI)) matrix = np.nan_to_num(matrix) matrix = matrix.transpose() return feature_df, matrix
def ppo(candles: np.ndarray, fastperiod=12, slowperiod=26, matype=0, sequential=False) -> Union[float, np.ndarray]: """ PPO - Percentage Price Oscillator :param candles: np.ndarray :param fastperiod: int - default: 12 :param slowperiod: int - default: 26 :param matype: int - default: 0 :param sequential: bool - default=False :return: float | np.ndarray """ if not sequential and len(candles) > 240: candles = candles[-240:] res = talib.PPO(candles[:, 2], fastperiod=fastperiod, slowperiod=slowperiod, matype=matype) return res if sequential else res[-1]
def momentum(self): adx = talib.ADX(self.high,self.low,self.close,self.period) adxr = talib.ADXR(self.high,self.low,self.close,self.period) apo = talib.APO(self.high,self.low,self.close,self.period) aroondown, aroonup = talib.AROON(self.high, self.low, period) aroonosc = talib.AROONOSC(self.high,self.low,self.period) bop = talib.BOP(self.opens,self.high,self.low,self.close) cci = talib.CCI(self.high,self.low,self.close,self.period) cmo = talib.CMO(self.close,self.period) dx = talib.DX(self.high,self.low,self.close,self.period) macd, macdsignal, macdhist = talib.MACD(self.close, fastperiod=period, slowperiod=period*5, signalperiod=period*2) macd1, macdsignal1, macdhist1 = talib.MACDEXT(self.close, fastperiod=12, fastmatype=0, slowperiod=26, slowmatype=0, signalperiod=9, signalmatype=0) macd2, macdsignal2, macdhist2 = talib.MACDFIX(self.close, signalperiod=9) mfi = talib.MFI(self.high, self.low, self.close, self.volume, timeperiod=14) minus_di = talib.MINUS_DI(self.high, self.low, self.close, timeperiod=14) minus_dm = talib.MINUS_DM(self.high, self.low, timeperiod=14) mom = talib.MOM(self.close, timeperiod=10) plus_di = talib.PLUS_DI(self.high, self.low, self.close, timeperiod=14) plus_dm = talib.PLUS_DM(self.high, self.low, timeperiod=14) ppo = talib.PPO(self.close, fastperiod=12, slowperiod=26, matype=0) roc = talib.ROC(self.close, timeperiod=10) rocp = talib.ROCP(self.close, timeperiod=10) rocr = talib.ROCR(self.close, timeperiod=10) rocr100 = talib.ROCR100(self.close, timeperiod=10) rsi = talib.RSI(self.close, timeperiod=14) slowk, slowd = talib.STOCH(self.high, self.low, self.close, fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0) fastk, fastd = talib.STOCHF(self.high, self.low, self.close, fastk_period=5, fastd_period=3, fastd_matype=0) fastk1, fastd1 = talib.STOCHRSI(self.close, timeperiod=14, fastk_period=5, fastd_period=3, fastd_matype=0) trix = talib.TRIX(self.close, timeperiod=30) ultosc = talib.ULTOSC(self.high, self.low, self.close, timeperiod1=7, timeperiod2=14, timeperiod3=28) willr = talib.WILLR(self.high, self.low, self.close, timeperiod=14)
def ppo(prices_df: pd.DataFrame, fast=12, slow=26, signal=9) -> Tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame]: prices_arr = prices_df.to_numpy() ppos, pposignals = np.empty_like(prices_arr), np.empty_like(prices_arr) for col in range(prices_arr.shape[1]): ppos[:, col] = talib.PPO(prices_arr[:, col], fastperiod=fast, slowperiod=slow, matype=MA_Type.EMA) try: pposignals[:, col] = talib.EMA(ppo, timeperiod=signal) except: pposignals[:, col] = np.nan ppohists = ppos - pposignals return (pd.DataFrame(ppos, dtype=prices_arr.dtype, index=prices_df.index, columns=prices_df.columns), pd.DataFrame(pposignals, dtype=prices_arr.dtype, index=prices_df.index, columns=prices_df.columns), pd.DataFrame(ppohists, dtype=prices_arr.dtype, index=prices_df.index, columns=prices_df.columns))
def test_ppo(self): """ Test Percentage Price Oscillator. """ q = qufilab.ppo(self.close) t = talib.PPO(self.close, matype=1) np.testing.assert_allclose(q, t, rtol=self.tolerance)
def ppo(Close, fast=12, slow=26): tmp_ppo = ta.PPO(Close, fastperiod=fast, slowperiod=slow, matype=0) ppo = pd.Series(index=Close.index, data=[0] * len(Close)) ppo.loc[tmp_ppo > 0] = 1 ppo.loc[tmp_ppo <= 0] = -1 return ppo
def ppo(close_ts, fastperiod=12, slowperiod=26, matype=0): import talib close_np = close_ts.cpu().detach().numpy() close_df = pd.DataFrame(close_np) PPO = close_df.apply(lambda x: talib.PPO(x, fastperiod=12, slowperiod=26, matype=0)) PPO_ts = torch.tensor(PPO.values, dtype=close_ts.dtype, device=close_ts.device) return PPO_ts
def getMomentumIndicators(df): high = df['High'] low = df['Low'] close = df['Close'] open = df['Open'] volume = df['Volume'] df['ADX'] = ta.ADX(high, low, close, timeperiod=14) df['SMA'] = ta.ADXR(high, low, close, timeperiod=14) df['APO'] = ta.APO(close, fastperiod=12, slowperiod=26, matype=0) df['AROONDOWN'], df['AROOONUP'] = ta.AROON(high, low, timeperiod=14) df['AROONOSC'] = ta.AROONOSC(high, low, timeperiod=14) df['BOP'] = ta.BOP(open, high, low, close) df['CCI'] = ta.CCI(high, low, close, timeperiod=14) df['CMO'] = ta.CMO(close, timeperiod=14) df['DX'] = ta.DX(high, low, close, timeperiod=14) df['MACD'], df['MACDSIGNAL'], df['MACDHIST'] = ta.MACD(close, fastperiod=12, slowperiod=26, signalperiod=9) df['MFI'] = ta.MFI(high, low, close, volume, timeperiod=14) df['MINUS_DI'] = ta.MINUS_DI(high, low, close, timeperiod=14) df['MINUS_DM']= ta.MINUS_DM(high, low, timeperiod=14) df['MOM'] = ta.MOM(close, timeperiod=10) df['PLUS_DM'] =ta.PLUS_DM(high, low, timeperiod=14) df['PPO'] = ta.PPO(close, fastperiod=12, slowperiod=26, matype=0) df['ROC'] = ta.ROC(close, timeperiod=10) df['ROCP'] = ta.ROCP(close, timeperiod=10) df['ROCR'] = ta.ROCR(close, timeperiod=10) df['ROCR100'] = ta.ROCR100(close, timeperiod=10) df['RSI'] = ta.RSI(close, timeperiod=14) df['SLOWK'], df['SLOWD'] = ta.STOCH(high, low, close, fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0) df['FASTK'], df['FASTD'] = ta.STOCHF(high, low, close, fastk_period=5, fastd_period=3, fastd_matype=0) df['FASTK2'], df['FASTD2'] = ta.STOCHRSI(close, timeperiod=14, fastk_period=5, fastd_period=3, fastd_matype=0) df['TRIX'] = ta.TRIX(close, timeperiod=30) df['ULTOSC'] = ta.ULTOSC(high, low, close, timeperiod1=7, timeperiod2=14, timeperiod3=28) df['WILLR'] = ta.WILLR(high, low, close, timeperiod=14)
def ppo(candles: np.ndarray, fast_period: int = 12, slow_period: int = 26, matype: int = 0, source_type: str = "close", sequential: bool = False) -> Union[float, np.ndarray]: """ PPO - Percentage Price Oscillator :param candles: np.ndarray :param fast_period: int - default: 12 :param slow_period: int - default: 26 :param matype: int - default: 0 :param source_type: str - default: "close" :param sequential: bool - default=False :return: float | np.ndarray """ candles = slice_candles(candles, sequential) source = get_candle_source(candles, source_type=source_type) res = talib.PPO(source, fastperiod=fast_period, slowperiod=slow_period, matype=matype) return res if sequential else res[-1]
def run_indicators(symbol, file): try: if os.path.exists(file): # read file df = np.genfromtxt(file, delimiter=',') # run talib indicators using dataframe # create additional columns in the datafarame closePrice = df[:, 4] openPrice = df[:, 1] highPrice = df[:, 2] lowPrice = df[:, 3] volume = df[:, 5] sma = talib.SMA(closePrice) atr = talib.ATR(highPrice, lowPrice, closePrice) natr = talib.NATR(highPrice, lowPrice, closePrice) adx = talib.ADX(highPrice, lowPrice, closePrice) mfi = talib.MFI(highPrice, lowPrice, closePrice, volume) ppo = talib.PPO(closePrice, fastperiod=12, slowperiod=26, matype=0) rsi = talib.RSI(closePrice) slowk, slowd = talib.STOCH(highPrice, lowPrice, closePrice, fastk_period=14, slowk_period=3, slowd_period=3) macd, macdsignal, macdhist = talib.MACD(closePrice, fastperiod=12, slowperiod=26, signalperiod=9) # get ticker current price client = Client(os.environ.get('BINANCE_API_KEY'), os.environ.get('BINANCE_SECRET_KEY'), {"timeout": 100}) closing_price = client.get_symbol_ticker(symbol=symbol) # create crypto object crypto = {} crypto['ticker'] = symbol crypto['price'] = closing_price['price'] crypto['SMA14'] = sma[-1] crypto['ATR'] = atr[-1] crypto['NATR'] = natr[-1] crypto['ADX'] = adx[-1] crypto['MFI'] = mfi[-1] crypto['RSI'] = rsi[-1] crypto['STO'] = slowk[-1], slowd[-1] crypto['MACD'] = macd[-1] crypto['PPO'] = ppo[-1] else: crypto = {} crypto['ticker'] = symbol return crypto except Exception as e: print(str(e))
def ppo(self, n, array=False): """ PPO. """ result = talib.PPO(self.close, n) if array: return result return result[-1]
def ppo(self, n: int, array: bool = False) -> Union[float, np.ndarray]: """ PPO. """ result = talib.PPO(self.close, n) if array: return result return result[-1]
def PPO(close, fastperiod=12, slowperiod=26, matype=0): # Percentage Price Oscillator param = { 'fastperiod': fastperiod, 'slowperiod': slowperiod, 'matype': matype, } return talib.PPO(close, **param)
def PPO(self, fastperiod=12, slowperiod=26, matype=0): real_data = np.array([self.df.close], dtype='f8') ppo = talib.PPO(real_data[0], fastperiod=fastperiod, slowperiod=slowperiod, matype=matype) # return go.Scatter( # x=self.df.index, # y=ppo, # name='PPO' # ) return ppo
def get_po(price_arr, cur_pos, period=10): if cur_pos <= period: s = 0 else: s = cur_pos - period tmp_arr = price_arr[s:cur_pos] prices = np.array(tmp_arr, dtype=float) return ta.PPO(prices)[-1]
def onMarketDataUpdate(self, market, code, md): stock_info = self.stock_info[code] # The following time is not allowed to trade. Only trade from 9:30 am to 12:00 am, and from 13:00 to 16:00 time_info = md.timestamp.split('_') if int(time_info[1][:4]) not in (range(930, 1201) + range(1300, 1601)) or \ (stock_info[MA] and md.lastPrice < 1 < stock_info[MA]): return # For open price record last day close price if time_info[0] != stock_info[LAST_DATE]: stock_info[LAST_DATE] = time_info[0] if len(stock_info[CLOSE_PRICE]) > max( self.rsi_period, self.ma_period, self.ppo_slow_period) + 1: stock_info[CLOSE_PRICE].pop(0) if stock_info[LAST_PRICE]: stock_info[CLOSE_PRICE].append(stock_info[LAST_PRICE]) if len(stock_info[CLOSE_PRICE]) < max( self.rsi_period, self.ma_period, self.ppo_slow_period): stock_info[LAST_PRICE] = md.lastPrice self.stock_info[code].update(stock_info) return stock_info[MA] = talib.MA(numpy.array(stock_info[CLOSE_PRICE]), self.ma_period)[-1] stock_info[STD] = talib.STDDEV( numpy.array(stock_info[CLOSE_PRICE]), self.ma_period)[-1] stock_info[PPO] = talib.PPO(numpy.array(stock_info[CLOSE_PRICE]), fastperiod=self.ppo_fast_period, slowperiod=self.ppo_slow_period)[-1] if stock_info[ HOLD_VOLUME] and stock_info[PPO] < self.ppo_sell_threshold: self.short_security(md.timestamp, code, md.bidPrice1) stock_info[RSI] = talib.RSI(numpy.array(stock_info[CLOSE_PRICE] + [md.lastPrice]), timeperiod=self.rsi_period)[-1] if stock_info[RSI] < self.rsi_buy_bound: std_factor = 2 else: std_factor = 1.2 if md.askPrice1 + std_factor * stock_info[STD] < stock_info[MA] and \ stock_info[PPO] > self.ppo_buy_threshold: self.long_security(md.timestamp, code, md.askPrice1) elif stock_info[HOLD_VOLUME] and md.bidPrice1 >= stock_info[MA]: self.short_security(md.timestamp, code, md.bidPrice1) stock_info[LAST_PRICE] = md.lastPrice self.stock_info[code].update(stock_info)
def ppo(df, n=12, m=26, normalize=False, col='Close'): c = df[col].as_matrix() _ppo = pd.Series(ta.PPO(c, fastperiod=n, slowperiod=m, matype=1), index=df.index, name="PPO_" + str(n) + "_" + str(m)) if normalize: _ppo = z_score(_ppo) return _ppo
def _get_indicators(security, open_name, close_name, high_name, low_name, volume_name): """ expand the features of the data through technical analysis across 26 different signals :param security: data which features are going to be expanded :param open_name: open price column name :param close_name: close price column name :param high_name: high price column name :param low_name: low price column name :param volume_name: traded volumn column name :return: expanded and extracted data """ open_price = security[open_name].values close_price = security[close_name].values low_price = security[low_name].values high_price = security[high_name].values volume = security[volume_name].values if volume_name else None security['MOM'] = talib.MOM(close_price) security['HT_DCPERIOD'] = talib.HT_DCPERIOD(close_price) security['HT_DCPHASE'] = talib.HT_DCPHASE(close_price) security['SINE'], security['LEADSINE'] = talib.HT_SINE(close_price) security['INPHASE'], security['QUADRATURE'] = talib.HT_PHASOR( close_price) security['ADXR'] = talib.ADXR(high_price, low_price, close_price) security['APO'] = talib.APO(close_price) security['AROON_UP'], _ = talib.AROON(high_price, low_price) security['CCI'] = talib.CCI(high_price, low_price, close_price) security['PLUS_DI'] = talib.PLUS_DI(high_price, low_price, close_price) security['PPO'] = talib.PPO(close_price) security['MACD'], security['MACD_SIG'], security[ 'MACD_HIST'] = talib.MACD(close_price) security['CMO'] = talib.CMO(close_price) security['ROCP'] = talib.ROCP(close_price) security['FASTK'], security['FASTD'] = talib.STOCHF( high_price, low_price, close_price) security['TRIX'] = talib.TRIX(close_price) security['ULTOSC'] = talib.ULTOSC(high_price, low_price, close_price) security['WILLR'] = talib.WILLR(high_price, low_price, close_price) security['NATR'] = talib.NATR(high_price, low_price, close_price) security['RSI'] = talib.RSI(close_price) security['EMA'] = talib.EMA(close_price) security['SAREXT'] = talib.SAREXT(high_price, low_price) # security['TEMA'] = talib.EMA(close_price) security['RR'] = security[close_name] / security[close_name].shift( 1).fillna(1) security['LOG_RR'] = np.log(security['RR']) if volume_name: security['MFI'] = talib.MFI(high_price, low_price, close_price, volume) # security['AD'] = talib.AD(high_price, low_price, close_price, volume) # security['OBV'] = talib.OBV(close_price, volume) security[volume_name] = np.log(security[volume_name]) security.drop([open_name, close_name, high_name, low_name], axis=1) security = security.dropna().astype(np.float32) return security
def generate_feature(data): high = data.High.values low = data.Low.values close = data.Close.values # feature_df = pd.DataFrame(index=data.index) feature_df = data.copy() feature_df["ADX"] = ADX = talib.ADX(high, low, close, timeperiod=14) feature_df["ADXR"] = ADXR = talib.ADXR(high, low, close, timeperiod=14) feature_df["APO"] = APO = talib.APO(close, fastperiod=12, slowperiod=26, matype=0) feature_df["AROONOSC"] = AROONOSC = talib.AROONOSC(high, low, timeperiod=14) feature_df["CCI"] = CCI = talib.CCI(high, low, close, timeperiod=14) feature_df["CMO"] = CMO = talib.CMO(close, timeperiod=14) feature_df["DX"] = DX = talib.DX(high, low, close, timeperiod=14) feature_df["MINUS_DI"] = MINUS_DI = talib.MINUS_DI(high, low, close, timeperiod=14) feature_df["MINUS_DM"] = MINUS_DM = talib.MINUS_DM(high, low, timeperiod=14) feature_df["MOM"] = MOM = talib.MOM(close, timeperiod=10) feature_df["PLUS_DI"] = PLUS_DI = talib.PLUS_DI(high, low, close, timeperiod=14) feature_df["PLUS_DM"] = PLUS_DM = talib.PLUS_DM(high, low, timeperiod=14) feature_df["PPO"] = PPO = talib.PPO(close, fastperiod=12, slowperiod=26, matype=0) feature_df["ROC"] = ROC = talib.ROC(close, timeperiod=10) feature_df["ROCP"] = ROCP = talib.ROCP(close, timeperiod=10) feature_df["ROCR100"] = ROCR100 = talib.ROCR100(close, timeperiod=10) feature_df["RSI"] = RSI = talib.RSI(close, timeperiod=14) feature_df["ULTOSC"] = ULTOSC = talib.ULTOSC(high, low, close, timeperiod1=7, timeperiod2=14, timeperiod3=28) feature_df["WILLR"] = WILLR = talib.WILLR(high, low, close, timeperiod=14) feature_df = feature_df.fillna(0.0) # Exclude columns you don't want feature_df = feature_df[feature_df.columns[ ~feature_df.columns.isin(['Open', 'High', 'Low', 'Close'])]] matrix = feature_df.values return feature_df, matrix
def indicators(data): """ :param data: data :return: np array of each indicator """ cmo = talib.CMO(np.array(data), timeperiod=10).reshape(-1, 1) roc = talib.ROC(np.array(data), timeperiod=5).reshape(-1, 1) rsi = talib.RSI(np.array(data), timeperiod=5).reshape(-1, 1) wma = talib.WMA(np.array(data), timeperiod=20).reshape(-1, 1) ppo = talib.PPO(np.array(data), fastperiod=10, slowperiod=20, matype=0).reshape(-1, 1) return cmo, roc, rsi, wma, ppo
def ppo(self, fast_period: int, slow_period: int, matype: int = 0, array: bool = False) -> Union[float, np.ndarray]: """ PPO. """ result = talib.PPO(self.close, fast_period, slow_period, matype) if array: return result return result[-1]
def get_momentum_studies(open, low, high, close, volume, df): # Momentum studies # https://mrjbq7.github.io/ta-lib/func_groups/momentum_indicators.html df['MACD'], df['MACD_SIGN'], df['MACD_HIST'] = talib.MACD( close, fastperiod=12, slowperiod=26, signalperiod=9) df['STOCH-SLOW-K'], df['STOCH-SLOW-D'] = talib.STOCH(high, low, close, fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0) df['STOCH-FAST-K'], df['STOCH-FAST-D'] = talib.STOCHF(high, low, close, fastk_period=5, fastd_period=3, fastd_matype=0) df['STOCH-RSI-K'], df['STOCH-RSI-D'] = talib.STOCHRSI(close, timeperiod=14, fastk_period=5, fastd_period=3, fastd_matype=0) df['AROON-DOWN'], df['AROON-UP'] = talib.AROON(high, low, timeperiod=14) df["MINUS_DI"] = talib.MINUS_DI(high, low, close, timeperiod=14) df["MINUS_DM"] = talib.MINUS_DM(high, low, timeperiod=14) df["PLUS_DI"] = talib.PLUS_DI(high, low, close, timeperiod=14) df["PLUS_DM"] = talib.PLUS_DM(high, low, timeperiod=14) df["MOM"] = talib.MOM(close, timeperiod=10) df["MFI"] = talib.MFI(high, low, close, volume, timeperiod=14) df["ADX"] = talib.ADX(high, low, close, timeperiod=14) df["ADXR"] = talib.ADXR(high, low, close, timeperiod=14) df["APO"] = talib.APO(close, fastperiod=12, slowperiod=26, matype=0) df["AROONOSC"] = talib.AROONOSC(high, low, timeperiod=14) df["BOP"] = talib.BOP(open, high, low, close) df["CCI"] = talib.CCI(high, low, close, timeperiod=14) df["CMO"] = talib.CMO(close, timeperiod=14) df["DX"] = talib.DX(high, low, close, timeperiod=14) df["PPO"] = talib.PPO(close, fastperiod=12, slowperiod=26, matype=0) df["ROC"] = talib.ROC(close, timeperiod=10) df["RSI"] = talib.RSI(close, timeperiod=14) df["TRIX"] = talib.TRIX(close, timeperiod=30) df["ULT"] = talib.ULTOSC(high, low, close, timeperiod1=7, timeperiod2=14, timeperiod3=28) df["WILLR"] = talib.WILLR(high, low, close, timeperiod=14)
def PPO(close, fastperiod=12, slowperiod=26, matype=0): ''' Percentage Price Oscillator 价格震荡百分比指数 分组: Momentum Indicator 动量指标 简介: 价格震荡百分比指标(PPO)是一个和MACD指标非常接近的指标。 PPO标准设定和MACD设定非常相似:12,26,9和PPO, 和MACD一样说明了两条移动平均线的差距, 但是它们有一个差别是PPO是用百分比说明。 real = PPO(close, fastperiod=12, slowperiod=26, matype=0) ''' return talib.PPO(close, fastperiod, slowperiod, matype)
def test_ppo(self): result = pandas_ta.ppo(self.close) self.assertIsInstance(result, DataFrame) self.assertEqual(result.name, 'PPO_12_26_9') try: expected = tal.PPO(self.close) pdt.assert_series_equal(result['PPO_12_26_9'], expected, check_names=False) except AssertionError as ae: try: corr = pandas_ta.utils.df_error_analysis(result['PPO_12_26_9'], expected, col=CORRELATION) self.assertGreater(corr, CORRELATION_THRESHOLD) except Exception as ex: error_analysis(result['PPO_12_26_9'], CORRELATION, ex)
def OnBar(self, bar): if self.bar_count >= 41: close = np.array(self.array.close) signal = ta.PPO(close, fastperiod=12, slowperiod=26, matype=0) if signal[-1] > 0 and self.pos == 0: self.buy(99999, 5000) elif signal[-1] < 0 and self.pos > 0: self.sell(1, self.pos) elif signal[-1] > 0 and self.pos < 0: self.buy_to_cover(99999, -self.pos) elif signal[-1] < 0 and self.pos == 0: self.sell_short(1, 5000) self.strategy_output(self.pos)
def generate_tech_data_default(stock, open_name, close_name, high_name, low_name, volume_name='vol'): open_price = stock[open_name].values close_price = stock[close_name].values low_price = stock[low_name].values high_price = stock[high_name].values volume = stock[volume_name].values data = stock.copy() data['MOM'] = talib.MOM(close_price) data['HT_DCPERIOD'] = talib.HT_DCPERIOD(close_price) data['HT_DCPHASE'] = talib.HT_DCPHASE(close_price) data['sine'], data['leadsine'] = talib.HT_SINE(close_price) data['inphase'], data['quadrature'] = talib.HT_PHASOR(close_price) data['ADXR'] = talib.ADXR(high_price, low_price, close_price) data['APO'] = talib.APO(close_price) data['AROON_UP'], _ = talib.AROON(high_price, low_price) data['CCI'] = talib.CCI(high_price, low_price, close_price) data['PLUS_DI'] = talib.PLUS_DI(high_price, low_price, close_price) data['PPO'] = talib.PPO(close_price) data['macd'], data['macd_sig'], data['macd_hist'] = talib.MACD(close_price) data['CMO'] = talib.CMO(close_price) data['ROCP'] = talib.ROCP(close_price) data['fastk'], data['fastd'] = talib.STOCHF(high_price, low_price, close_price) data['TRIX'] = talib.TRIX(close_price) data['ULTOSC'] = talib.ULTOSC(high_price, low_price, close_price) data['WILLR'] = talib.WILLR(high_price, low_price, close_price) data['NATR'] = talib.NATR(high_price, low_price, close_price) data['MFI'] = talib.MFI(high_price, low_price, close_price, volume) data['RSI'] = talib.RSI(close_price) data['AD'] = talib.AD(high_price, low_price, close_price, volume) data['OBV'] = talib.OBV(close_price, volume) data['EMA'] = talib.EMA(close_price) data['SAREXT'] = talib.SAREXT(high_price, low_price) data['TEMA'] = talib.EMA(close_price) #data = data.drop([open_name, close_name, high_name, low_name, volume_name, 'amount', 'count'], axis=1) data = drop_columns(data, [ open_name, close_name, high_name, low_name, volume_name, 'amount', 'count' ]) data = data.dropna().astype(np.float32) return data
def init_state(indata, test=False): openn = indata['open'].values close = indata['close'].values high = indata['high'].values low = indata['low'].values volume = indata['volume'].values diff = np.diff(close) diff = np.insert(diff, 0, 0) sma30 = talib.SMA(close, 30) sma60 = talib.SMA(close, timeperiod=60) rsi = talib.RSI(close, timeperiod=14) atr = talib.ATR(high, low, close, timeperiod=14) trange = talib.TRANGE(high, low, close) macd, macdsignal, macdhist = talib.MACD(close, 12, 26, 9) upper, middle, lower = talib.BBANDS(close, 20, 2, 2) ema = talib.EMA(close, 30) ma = talib.MA(close, 30) wma = talib.WMA(close, timeperiod=30) tema = talib.TEMA(close, 30) obv = talib.OBV(close, np.asarray(volume, dtype='float')) adx = talib.ADX(high, low, close, 14) apo = talib.APO(close, 12, 2, 0) bop = talib.BOP(openn, high, low, close) mom = talib.MOM(close,10) ppo = talib.PPO(close, 12, 26, 0) slowk, slowd = talib.STOCH(high, low, close, fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0) ad = talib.AD(high, low, close, np.asarray(volume, dtype='float')) wcl = talib.WCLPRICE(high, low, close) #--- Preprocess data xdata = np.column_stack((close, diff, sma30, sma60, rsi, atr, macd, macdsignal, macdhist, lower, middle, upper, ema, ma, wma, adx, apo, bop, mom, ppo, slowk, slowd, trange, wcl)) xdata = np.nan_to_num(xdata) if test == False: scaler = preprocessing.StandardScaler() xdata = np.expand_dims(scaler.fit_transform(xdata), axis=1) joblib.dump(scaler, 'data/scaler.pkl') elif test == True: scaler = joblib.load('data/scaler.pkl') xdata = np.expand_dims(scaler.fit_transform(xdata), axis=1) state = xdata[0:1, 0:1, :] return state, xdata, close
def test_PPO(self): self.env.add_operator('ppo', { 'operator': OperatorPPO, }) string = 'ppo(12, 26, open)' gene = self.env.parse_string(string) self.assertRaises(IndexError, gene.eval, self.env, self.dates[24], self.dates[-1]) df = gene.eval(self.env, self.dates[25], self.dates[26]) ser0, ser1 = df.iloc[0], df.iloc[1] o = self.env.get_data_value('open').values res0, res1 = [], [] for i in ser0.index: res = talib.PPO(o[:26 + 1, i], fastperiod=12, slowperiod=26) val0, val1 = res[-2], res[-1] res0.append(ser0[i] == val0) res1.append(ser1[i] == val1) self.assertTrue(all(res0) and all(res1))
def add_PPO(self, fastperiod=12, slowperiod=26, matype=0, type='line', color='secondary', **kwargs): """Percent Price Oscillator.""" if not self.has_close: raise Exception() utils.kwargs_check(kwargs, VALID_TA_KWARGS) if 'kind' in kwargs: kwargs['type'] = kwargs['kind'] name = 'PPO({},{})'.format(str(fastperiod), str(slowperiod)) self.ind[name] = talib.PPO(self.df[self.cl].values, fastperiod, slowperiod, matype)
def onMarketDataUpdate(self, market, code, md): # The following time is not allowed to trade. Only trade from 9:30 am to 12:00 am, and from 13:00 to 16:00 time_info = md.timestamp.split('_') if int(time_info[1][:4]) not in (range(930, 1201) + range(1300, 1601)) or md.lastPrice < 1 < self.mean_average: return # For open price record last day close price if time_info[0] != self.last_date: self.last_date = time_info[0] if len(self.close_price) > max(self.rsi_period, self.ma_period, self.ppo_slow_period) + 1: self.close_price.pop(0) if self.last_price: self.close_price.append(self.last_price) if len(self.close_price) < max(self.rsi_period, self.ma_period): self.last_price = md.lastPrice return self.mean_average = talib.MA(numpy.array(self.close_price), self.ma_period)[-1] self.standard_dev = talib.STDDEV(numpy.array(self.close_price), self.ma_period)[-1] self.ppo = talib.PPO(numpy.array(self.close_price), fastperiod=self.ppo_fast_period, slowperiod=self.ppo_slow_period) if self.buy_volume and self.ppo[-1] < self.ppo_sell_threshold: self.short_security(md.timestamp, code, md.bidPrice1) self.rsix = talib.RSI(numpy.array(self.close_price + [md.lastPrice]), timeperiod=self.rsi_period) if self.rsix[-1] < self.rsi_buy_bound: self.std_factor = 2 else: self.std_factor = 1.2 if (md.askPrice1 + self.std_factor * self.standard_dev < self.mean_average and self.ppo[-1] > self.ppo_buy_threshold): self.long_security(md.timestamp, code, md.askPrice1) elif self.buy_volume and md.bidPrice1 >= self.mean_average: self.short_security(md.timestamp, code, md.bidPrice1) self.last_price = md.lastPrice