我们从Python开源项目中,提取了以下16个代码示例,用于说明如何使用talib.BBANDS。
def boll(): #??tushare?????? df=ts.get_k_data('300580',start='2017-01-12',end='2017-05-26') #????? closed=df['close'].values upper,middle,lower=talib.BBANDS(closed,matype=talib.MA_Type.SMA) print upper,middle,lower plt.plot(upper) plt.plot(middle) plt.plot(lower) plt.grid() plt.show() diff1=upper-middle diff2=middle-lower print diff1 print diff2
def BOLL_CN(close,timeperiod=20, nbdev=2, isDEV=False): stddev = nbdev if not isDEV: devfix = np.sqrt(1.0*timeperiod/(timeperiod-1)) stddev = nbdev * devfix bollUPPER, bollMIDDLE, bollLOWER = tl.BBANDS( #close narray close, #time default 20 timeperiod=timeperiod, # number of non-biased standard deviations from the mean nbdevup=stddev, nbdevdn=stddev, # Moving average type: simple moving average here matype=0) return bollUPPER, bollMIDDLE, bollLOWER #WR%
def Bollinger_Bands(security_list, timeperiod=5, nbdevup=2, nbdevdn=2): # ???????????? if isinstance(security_list, str): security_list = [security_list] # ?? Bollinger Bands security_data = history(timeperiod * 2, '1d', 'close', security_list, df=False, skip_paused=True) upperband = {} middleband = {} lowerband = {} for stock in security_list: upperband[stock], middleband[stock], lowerband[stock] = talib.BBANDS( security_data[stock], timeperiod, nbdevup, nbdevdn) return upperband, middleband, lowerband # ?????
def __recountBoll(self): """????""" if self.inputBollLen < EMPTY_INT: return l = len(self.lineBar) if l < min(7, self.inputBollLen)+1: self.debugCtaLog(u'?????,??Bar?????{0}???Boll???{1}'. format(len(self.lineBar), min(7, self.inputBollLen)+1)) return if l < self.inputBollLen+2: bollLen = l-1 else: bollLen = self.inputBollLen # ????????Bar listClose=[x.close for x in self.lineBar[-bollLen - 1:-1]] # upper, middle, lower = ta.BBANDS(numpy.array(listClose, dtype=float), timeperiod=bollLen, nbdevup=self.inputBollStdRate, nbdevdn=self.inputBollStdRate, matype=0) self.lineUpperBand.append(upper[-1]) self.lineMiddleBand.append(middle[-1]) self.lineLowerBand.append(lower[-1]) # ----------------------------------------------------------------------
def cycle_process(event): print(event.widget.get()) cycle = event.widget.get() upperband, middleband, lowerband = ta.BBANDS(close, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0) fig, axes = plt.subplots(2, 1, sharex=True) ax1, ax2 = axes[0], axes[1] axes[0].plot(close, 'rd-', markersize=3) axes[0].plot(upperband, 'y-') axes[0].plot(middleband, 'b-') axes[0].plot(lowerband, 'y-') axes[0].set_title(cycle, fontproperties="SimHei") if cycle == '??????——???????': real = ta.HT_DCPERIOD(close) axes[1].plot(real, 'r-') elif cycle == '??????,??????????': real = ta.HT_DCPHASE(close) axes[1].plot(real, 'r-') elif cycle == '??????——????': inphase, quadrature = ta.HT_PHASOR(close) axes[1].plot(inphase, 'r-') axes[1].plot(quadrature, 'g-') elif cycle == '??????——????': sine, leadsine = ta.HT_SINE(close) axes[1].plot(sine, 'r-') axes[1].plot(leadsine, 'g-') elif cycle == '??????——???????': integer = ta.HT_TRENDMODE(close) axes[1].plot(integer, 'r-') plt.show() # ????
def statistic_process(event): print(event.widget.get()) statistic = event.widget.get() upperband, middleband, lowerband = ta.BBANDS(close, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0) fig, axes = plt.subplots(2, 1, sharex=True) ax1, ax2 = axes[0], axes[1] axes[0].plot(close, 'rd-', markersize=3) axes[0].plot(upperband, 'y-') axes[0].plot(middleband, 'b-') axes[0].plot(lowerband, 'y-') axes[0].set_title(statistic, fontproperties="SimHei") if statistic == '????': real = ta.LINEARREG(close, timeperiod=14) axes[1].plot(real, 'r-') elif statistic == '??????': real = ta.LINEARREG_ANGLE(close, timeperiod=14) axes[1].plot(real, 'r-') elif statistic == '??????': real = ta.LINEARREG_INTERCEPT(close, timeperiod=14) axes[1].plot(real, 'r-') elif statistic == '??????': real = ta.LINEARREG_SLOPE(close, timeperiod=14) axes[1].plot(real, 'r-') elif statistic == '???': real = ta.STDDEV(close, timeperiod=5, nbdev=1) axes[1].plot(real, 'r-') elif statistic == '??????': real = ta.TSF(close, timeperiod=14) axes[1].plot(real, 'r-') elif statistic == '??': real = ta.VAR(close, timeperiod=5, nbdev=1) axes[1].plot(real, 'r-') plt.show() # ????
def math_operator_process(event): print(event.widget.get()) math_operator = event.widget.get() upperband, middleband, lowerband = ta.BBANDS(close, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0) fig, axes = plt.subplots(2, 1, sharex=True) ax1, ax2 = axes[0], axes[1] axes[0].plot(close, 'rd-', markersize=3) axes[0].plot(upperband, 'y-') axes[0].plot(middleband, 'b-') axes[0].plot(lowerband, 'y-') axes[0].set_title(math_operator, fontproperties="SimHei") if math_operator == '?????????': real = ta.MAX(close, timeperiod=30) axes[1].plot(real, 'r-') elif math_operator == '????????????': integer = ta.MAXINDEX(close, timeperiod=30) axes[1].plot(integer, 'r-') elif math_operator == '?????????': real = ta.MIN(close, timeperiod=30) axes[1].plot(real, 'r-') elif math_operator == '????????????': integer = ta.MININDEX(close, timeperiod=30) axes[1].plot(integer, 'r-') elif math_operator == '????????????': min, max = ta.MINMAX(close, timeperiod=30) axes[1].plot(min, 'r-') axes[1].plot(max, 'r-') elif math_operator == '???????????????': minidx, maxidx = ta.MINMAXINDEX(close, timeperiod=30) axes[1].plot(minidx, 'r-') axes[1].plot(maxidx, 'r-') elif math_operator == '??': real = ta.SUM(close, timeperiod=30) axes[1].plot(real, 'r-') plt.show()
def expected_bbands(self, window_length, k, closes): """Compute the expected data (without adjustments) for the given window, k, and closes array. This uses talib.BBANDS to generate the expected data. """ lower_cols = [] middle_cols = [] upper_cols = [] ndates, nassets = closes.shape for n in range(nassets): close_col = closes[:, n] if np.isnan(close_col).all(): # ta-lib doesn't deal well with all nans. upper, middle, lower = [np.full(ndates, np.nan)] * 3 else: upper, middle, lower = talib.BBANDS( close_col, window_length, k, k, ) upper_cols.append(upper) middle_cols.append(middle) lower_cols.append(lower) # Stack all of our uppers, middles, lowers into three 2d arrays # whose columns are the sids. After that, slice off only the # rows we care about. where = np.s_[window_length - 1:] uppers = np.column_stack(upper_cols)[where] middles = np.column_stack(middle_cols)[where] lowers = np.column_stack(lower_cols)[where] return uppers, middles, lowers
def add_BBANDS(self, timeperiod=20, nbdevup=2, nbdevdn=2, matype=0, types=['line_dashed_thin', 'line_dashed_thin'], colors=['tertiary', 'grey_strong'], **kwargs): """Bollinger Bands. Note that the first argument of types and colors refers to upper and lower bands while second argument refers to middle band. (Upper and lower are symmetrical arguments, hence only 2 needed.) """ if not 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 = 'BBANDS({},{},{})'.format(str(timeperiod), str(nbdevup), str(nbdevdn)) ubb = name + '[Upper]' bb = name lbb = name + '[Lower]' self.pri[ubb] = dict(type='line_' + types[0][5:], color=colors[0]) self.pri[bb] = dict(type='area_' + types[1][5:], color=colors[1], fillcolor='fill') self.pri[lbb] = dict(type='area_' + types[0][5:], color=colors[0], fillcolor='fill') (self.ind[ubb], self.ind[bb], self.ind[lbb]) = talib.BBANDS(self.df[self.cl].values, timeperiod, nbdevup, nbdevdn, matype)
def _boll(self, data, n): """ ??????????? """ data = transform2ndarray(data) upper, middle, lower = talib.BBANDS(data, n, 2, 2) return (upper, middle, lower)
def calculate_bbands(self, period_name, close): timeperiod = 20 upperband_1, middleband_1, lowerband_1 = talib.BBANDS(close, timeperiod=timeperiod, nbdevup=1, nbdevdn=1, matype=0) self.current_indicators[period_name]['bband_upper_1'] = upperband_1[-1] self.current_indicators[period_name]['bband_lower_1'] = lowerband_1[-1] upperband_2, middleband_2, lowerband_2 = talib.BBANDS(close, timeperiod=timeperiod, nbdevup=2, nbdevdn=2, matype=0) self.current_indicators[period_name]['bband_upper_2'] = upperband_2[-1] self.current_indicators[period_name]['bband_lower_2'] = lowerband_2[-1]
def BOLL(closes, matype=MA_Type.EMA): """??? matype: ???????? ????????????? EMA??2??SMA?4????????? return upper, middle, lower""" closes = np.array(closes) return talib.BBANDS(closes, timeperiod=20, matype=matype)
def bbands(df): if len(df) > 100: close = df.close.values upperband, middleband, lowerband = tl.BBANDS(close, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0) return upperband, middleband, lowerband
def get_bbands_info(df): """ calculate bbands quotation. :param df: :return: close, upperband, middleband, lowerband """ close = get_close_data(df) upperband, middleband, lowerband = ta.BBANDS(close, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0) return pd.DataFrame({u'close': df.close[-view_days:], u'upperband': upperband[-view_days:], u'middleband': middleband[-view_days:], u'lowerband': lowerband[-view_days:]})
def get_bbands(sorted_data): close,high,low,ma5,ma10,ma20 = get_case_data(sorted_data) upperband, middleband, lowerband = ta.BBANDS(close, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0) #??%b??????????? operator = '' # score = 0 index_b = (close[-1]-lowerband[-1]) / (upperband[-1]-lowerband[-1]) # print "%b is : " + str(index_b) if index_b > 1: operator += 'S%' # score -= 10 elif index_b <= 0 : operator += 'B%' # score += 10 #?????????????? up = upperband[-1] - upperband[-2] down = lowerband[-2] -lowerband[-1] if up > 0 and down > 0: if up > down: operator += 'BO' elif up < down: operator += 'SO' if ma5[-1] > ma10[-1] and ma10[-1] > ma20[-1]: if upperband[-1] < upperband[-2]: operator += 'S!' return (operator,index_b) #initial the date
def overlap_process(event): print(event.widget.get()) overlap = event.widget.get() upperband, middleband, lowerband = ta.BBANDS(close, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0) fig, axes = plt.subplots(2, 1, sharex=True) ax1, ax2 = axes[0], axes[1] axes[0].plot(close, 'rd-', markersize=3) axes[0].plot(upperband, 'y-') axes[0].plot(middleband, 'b-') axes[0].plot(lowerband, 'y-') axes[0].set_title(overlap, fontproperties="SimHei") if overlap == u'???': pass elif overlap == u'????????': real = ta.DEMA(close, timeperiod=30) axes[1].plot(real, 'r-') elif overlap == u'??????? ': real = ta.EMA(close, timeperiod=30) axes[1].plot(real, 'r-') elif overlap == u'??????——?????': real = ta.HT_TRENDLINE(close) axes[1].plot(real, 'r-') elif overlap == u'???????????': real = ta.KAMA(close, timeperiod=30) axes[1].plot(real, 'r-') elif overlap == u'?????': real = ta.MA(close, timeperiod=30, matype=0) axes[1].plot(real, 'r-') elif overlap == u'MESA???????': mama, fama = ta.MAMA(close, fastlimit=0, slowlimit=0) axes[1].plot(mama, 'r-') axes[1].plot(fama, 'g-') elif overlap == u'????????': real = ta.MAVP(close, periods, minperiod=2, maxperiod=30, matype=0) axes[1].plot(real, 'r-') elif overlap == u'???????': real = ta.SMA(close, timeperiod=30) axes[1].plot(real, 'r-') elif overlap == u'????????(T3)': real = ta.T3(close, timeperiod=5, vfactor=0) axes[1].plot(real, 'r-') elif overlap == u'????????': real = ta.TEMA(close, timeperiod=30) axes[1].plot(real, 'r-') elif overlap == u'?????? ': real = ta.TRIMA(close, timeperiod=30) axes[1].plot(real, 'r-') elif overlap == u'???????': real = ta.WMA(close, timeperiod=30) axes[1].plot(real, 'r-') plt.show() # ????