我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用talib.STOCH。
def getStrategy_STOCH(start_trading_day,end_trading_day,_time,_close,_max,_min): point = [] iday = _time.index(start_trading_day) eday = _time.index(end_trading_day) slowk,slowd = ta.STOCH(np.array(_max, dtype='f8'),np.array(_min, dtype='f8'),np.array(_close, dtype='f8'), fastk_period = 5,slowk_period=3,slowd_period=3) slowk = slowk[iday:eday] slowd = slowd[iday:eday] point.append(0) for i in range(1,len(slowk)): if (slowk[i-1] <= slowd[i-1]) and (slowk[i] >= slowd[i]) and (slowk[i] <= 30): point.append(1) elif (slowk[i-1] <= 50) and (slowk[i] >= 50): point.append(-1) else: point.append(0) return point
def compute(self, today, assets, out, close, high, low): stoch_list = [] for col_c, col_h, col_l in zip(close.T, high.T, low.T): try: _, slowd = talib.STOCH(col_h, col_l, col_c, fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0) stoch_list.append(slowd[-1]) # if error calculating except: stoch_list.append(np.nan) out[:] = stoch_list # 5-day Money Flow / Volume
def STOCH(df, n = 14, slowk_period = 3, slowd_period = 3): fastk, fastd = talib.STOCHF(df['high'].values, df['low'].values, df['close'].values, fastk_period = n, fastd_period=slowk_period) slowk, slowd = talib.STOCH(df['high'].values, df['low'].values, df['close'].values, fastk_period = n, slowk_period=slowk_period, slowd_period=slowd_period) fk = pd.Series(fastk, index = df.index, name = "STOCHFK_%s_%s_%s" % (str(n), str(slowk_period), str(slowd_period))) sk = pd.Series(slowk, index = df.index, name = "STOCHSK_%s_%s_%s" % (str(n), str(slowk_period), str(slowd_period))) sd = pd.Series(slowd, index = df.index, name = "STOCHSD_%s_%s_%s" % (str(n), str(slowk_period), str(slowd_period))) return pd.concat([fk, sk, sd], join='outer', axis=1)
def add_STOCH(self, fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0, types=['line', 'line'], colors=['primary', 'tertiary'], **kwargs): """Slow Stochastic Oscillator. Note that the first argument of types and colors refers to Slow Stoch %K, while second argument refers to Slow Stoch %D (signal line of %K obtained by MA). """ if not (self.has_high and self.has_low and self.has_close): raise Exception() utils.kwargs_check(kwargs, VALID_TA_KWARGS) if 'kind' in kwargs: kwargs['type'] = kwargs['kind'] if 'kinds' in kwargs: types = kwargs['type'] if 'type' in kwargs: types = [kwargs['type']] * 2 if 'color' in kwargs: colors = [kwargs['color']] * 2 name = 'STOCH({},{},{})'.format(str(fastk_period), str(slowk_period), str(slowd_period)) slowk = name + r'[%k]' slowd = name + r'[%d]' self.sec[slowk] = dict(type=types[0], color=colors[0]) self.sec[slowd] = dict(type=types[1], color=colors[1], on=slowk) self.ind[slowk], self.ind[slowd] = talib.STOCH(self.df[self.hi].values, self.df[self.lo].values, self.df[self.cl].values, fastk_period, slowk_period, slowk_matype, slowd_period, slowd_matype)
def calculate_stoch(self, period_name, closing_prices): slowk, slowd = talib.STOCH(self.highs, self.lows, closing_prices, fastk_period=14, slowk_period=2, slowk_matype=0, slowd_period=3, slowd_matype=0) self.current_indicators[period_name]['stoch_slowk'] = slowk[-1] self.current_indicators[period_name]['stoch_slowd'] = slowd[-1]
def get_kdj(sorted_data): close,high,low,ma5,ma10,ma20 = get_case_data(sorted_data) slowk, slowd = ta.STOCH(high,low,close, fastk_period=9, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0) slowkMA5 = ta.MA(slowk, timeperiod=5, matype=0) slowkMA10 = ta.MA(slowk, timeperiod=10, matype=0) slowkMA20 = ta.MA(slowk, timeperiod=20, matype=0) slowdMA5 = ta.MA(slowd, timeperiod=5, matype=0) slowdMA10 = ta.MA(slowd, timeperiod=10, matype=0) slowdMA20 = ta.MA(slowd, timeperiod=20, matype=0) operator = '' #1.K???????——???90?????????10??????D??80???????????D??20??????????? if slowk[-1] >= 90: operator += 'S9' elif slowk[-1] <= 10: operator += 'B1' elif slowd[-1] >=80: operator += 'S8' elif slowd[-1] <= 20: operator += 'B2' #2.??????K???D??K?????D???????? if slowk[-1] > slowd[-1] and slowk[-2] <= slowd[-2]: operator += 'BX' elif slowk[-1] < slowd[-1] and slowk[-2] >= slowd[-2]: operator += 'SX' #3.??????????????????????? if ma5[-1] >= ma10[-1] and ma10[-1] >= ma20[-1]: #k??? if (slowkMA5[-1] <= slowkMA10[-1] and slowkMA10[-1] <= slowkMA20[-1]) or (slowdMA5[-1] <= slowdMA10[-1] and slowdMA10[-1] <= slowdMA20[-1]): operator += 'S><' elif ma5[-1] <= ma10[-1] and ma10[-1] <= ma20[-1]: #k??? if (slowkMA5[-1] >= slowkMA10[-1] and slowkMA10[-1] >= slowkMA20[-1]) or (slowdMA5[-1] >= slowdMA10[-1] and slowdMA10[-1] >= slowdMA20[-1]): operator += 'B><' return operator
def Get_KDJ(df): #??9,3,3 slowk, slowd = ta.STOCH(np.array(df['high']), np.array(df['low']), np.array(df['close']), fastk_period=9, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0) slowkMA5 = ta.MA(slowk, timeperiod=5, matype=0) slowkMA10 = ta.MA(slowk, timeperiod=10, matype=0) slowkMA20 = ta.MA(slowk, timeperiod=20, matype=0) slowdMA5 = ta.MA(slowd, timeperiod=5, matype=0) slowdMA10 = ta.MA(slowd, timeperiod=10, matype=0) slowdMA20 = ta.MA(slowd, timeperiod=20, matype=0) #16-17 K,D df['slowk']=pd.Series(slowk,index=df.index) #K df['slowd']=pd.Series(slowd,index=df.index)#D dflen = df.shape[0] MAlen = len(slowkMA5) operate = 0 #1.K???????——???90?????????10??????D??80???????????D??20??????????? if df.iat[(dflen-1),16]>=90: operate = operate + 3 elif df.iat[(dflen-1),16]<=10: operate = operate - 3 if df.iat[(dflen-1),17]>=80: operate = operate + 3 elif df.iat[(dflen-1),17]<=20: operate = operate - 3 #2.??????K???D??K?????D?????????#??? if df.iat[(dflen-1),16]> df.iat[(dflen-1),17] and df.iat[(dflen-2),16]<=df.iat[(dflen-2),17]: operate = operate + 10 #??????K??D?K?????D?????????#??? elif df.iat[(dflen-1),16]< df.iat[(dflen-1),17] and df.iat[(dflen-2),16]>=df.iat[(dflen-2),17]: operate = operate - 10 #3.??????????????????????? if df.iat[(dflen-1),7]>=df.iat[(dflen-1),8] and df.iat[(dflen-1),8]>=df.iat[(dflen-1),9]:#K??? if (slowkMA5[MAlen-1]<=slowkMA10[MAlen-1] and slowkMA10[MAlen-1]<=slowkMA20[MAlen-1]) or \ (slowdMA5[MAlen-1]<=slowdMA10[MAlen-1] and slowdMA10[MAlen-1]<=slowdMA20[MAlen-1]): #K,D?? operate = operate - 1 elif df.iat[(dflen-1),7]<=df.iat[(dflen-1),8] and df.iat[(dflen-1),8]<=df.iat[(dflen-1),9]:#K??? if (slowkMA5[MAlen-1]>=slowkMA10[MAlen-1] and slowkMA10[MAlen-1]>=slowkMA20[MAlen-1]) or \ (slowdMA5[MAlen-1]>=slowdMA10[MAlen-1] and slowdMA10[MAlen-1]>=slowdMA20[MAlen-1]): #K,D?? operate = operate + 1 return (df,operate) #??RSI??????