Exemple #1
0
    def test_STOCHSTOCHRSI(self):
        class MySTOCHRSI(OperatorSTOCHRSI):
            def __init__(self, name, **kwargs):
                super(MySTOCHRSI, self).__init__(100, name, **kwargs)

        self.env.add_operator('stochrsi', {
            'operator': MySTOCHRSI,
        })
        string = 'stochrsi(0, 14, 5, 3, open)'
        gene = self.env.parse_string(string)
        self.assertRaises(IndexError, gene.eval, self.env, self.dates[98],
                          self.dates[-1])
        df = gene.eval(self.env, self.dates[99], self.dates[100])
        ser0, ser1 = df.iloc[0], df.iloc[1]
        o = self.env.get_data_value('open').values
        res0, res1, res = [], [], []
        for i, val in ser0.iteritems():
            res0.append(
                talib.STOCHRSI(
                    o[:100, i], timeperiod=14, fastk_period=5, fastd_period=3)
                [0][-1] == val)
        for i, val in ser1.iteritems():
            res1.append(
                talib.STOCHRSI(o[1:100 + 1, i],
                               timeperiod=14,
                               fastk_period=5,
                               fastd_period=3)[0][-1] == val)
            res.append(
                talib.STOCHRSI(o[:100 + 1, i],
                               timeperiod=14,
                               fastk_period=5,
                               fastd_period=3)[0][-1] != val)
        self.assertTrue(all(res0) and all(res1) and any(res))
def stochrsi(close_ts, timeperiod=14, fastk_period=5, fastd_period=3, fastd_matype=0):
    import talib
    close_np = close_ts.cpu().detach().numpy()
    close_df = pd.DataFrame(close_np)
    fastk = close_df.apply(lambda x: talib.STOCHRSI(x, timeperiod=14, fastk_period=5, fastd_period=3, fastd_matype=0)[0])
    fastd = close_df.apply(lambda x: talib.STOCHRSI(x, timeperiod=14, fastk_period=5, fastd_period=3, fastd_matype=0)[1])
    fastk_ts = torch.tensor(fastk.values, dtype=close_ts.dtype, device=close_ts.device)
    fastd_ts = torch.tensor(fastd.values, dtype=close_ts.dtype, device=close_ts.device)
    return fastk_ts, fastd_ts
 def stoch_lb_test(self):
     fastk, fastd = talib.STOCHRSI(self.close, timeperiod=ind_duration)
     li = []
     for i in range(ind_duration, len(fastd)):
             if (fastd[i] < lowerbound):
                 li.append(i)
     return li
Exemple #4
0
 def calculations(self):
     '''calculations'''
     self.df['rsi'] = ta.RSI(self.df['close'], timeperiod=5)
     self.df['apo'] = ta.APO(self.df['close'],
                             fastperiod=10,
                             slowperiod=5,
                             matype=0)
     self.df['upperband'], self.df['middleband'], self.df[
         'lowerband'] = ta.BBANDS(self.df['close'],
                                  timeperiod=5,
                                  nbdevup=2,
                                  nbdevdn=2,
                                  matype=0)
     self.df['ema'] = ta.EMA(self.df['close'], timeperiod=5)
     self.df['ma'] = ta.MA(self.df['close'], timeperiod=5, matype=0)
     self.df['sma'] = ta.MA(self.df['close'], timeperiod=5)
     self.df['t3'] = ta.T3(self.df['close'], timeperiod=5, vfactor=0)
     self.df['wma'] = ta.WMA(self.df['close'], timeperiod=5)
     self.df['aroonosc'] = ta.AROONOSC(self.df['high'],
                                       self.df['low'],
                                       timeperiod=5)
     self.df['cci'] = ta.CCI(self.df['high'],
                             self.df['low'],
                             self.df['close'],
                             timeperiod=5)
     self.df['cmo'] = ta.CMO(self.df['close'], timeperiod=14)
     self.df['macd'], self.df['macdsignal'], self.df[
         'macdhist'] = ta.MACDEXT(self.df['close'],
                                  fastperiod=12,
                                  fastmatype=0,
                                  slowperiod=26,
                                  slowmatype=0,
                                  signalperiod=9,
                                  signalmatype=0)
     self.df['slowk'], self.df['slowd'] = ta.STOCH(self.df['high'],
                                                   self.df['low'],
                                                   self.df['close'],
                                                   fastk_period=5,
                                                   slowk_period=3,
                                                   slowk_matype=0,
                                                   slowd_period=3,
                                                   slowd_matype=0)
     self.df['fastk'], self.df['fastd'] = ta.STOCHRSI(self.df['close'],
                                                      timeperiod=5,
                                                      fastk_period=5,
                                                      fastd_period=3,
                                                      fastd_matype=0)
     self.df['ultosc'] = ta.ULTOSC(self.df['high'],
                                   self.df['low'],
                                   self.df['close'],
                                   timeperiod1=7,
                                   timeperiod2=14,
                                   timeperiod3=28)
     self.df['adosc'] = ta.ADOSC(self.df['high'],
                                 self.df['low'],
                                 self.df['close'],
                                 self.df['volume'],
                                 fastperiod=3,
                                 slowperiod=10)
     return self.df
Exemple #5
0
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 preprocess_data(self):
        """calculate returns and percentiles, then removes missing values"""

        self.data['returns'] = self.data.close.pct_change()
        self.data['ret_2'] = self.data.close.pct_change(2)
        self.data['ret_5'] = self.data.close.pct_change(5)
        self.data['ret_10'] = self.data.close.pct_change(10)
        self.data['ret_21'] = self.data.close.pct_change(21)
        self.data['rsi'] = talib.STOCHRSI(self.data.close)[1]
        self.data['macd'] = talib.MACD(self.data.close)[1]
        self.data['atr'] = talib.ATR(self.data.high, self.data.low,
                                     self.data.close)

        slowk, slowd = talib.STOCH(self.data.high, self.data.low,
                                   self.data.close)
        self.data['stoch'] = slowd - slowk
        self.data['atr'] = talib.ATR(self.data.high, self.data.low,
                                     self.data.close)
        self.data['ultosc'] = talib.ULTOSC(self.data.high, self.data.low,
                                           self.data.close)
        self.data = (self.data.replace(
            (np.inf, -np.inf), np.nan).drop(['high', 'low', 'close', 'volume'],
                                            axis=1).dropna())

        r = self.data.returns.copy()
        if self.normalize:
            self.data = pd.DataFrame(scale(self.data),
                                     columns=self.data.columns,
                                     index=self.data.index)
        features = self.data.columns.drop('returns')
        self.data['returns'] = r  # don't scale returns
        self.data = self.data.loc[:, ['returns'] + list(features)]
        log.info(self.data.info())
Exemple #7
0
 def calculate_stochrsi(self):
     self._fastk_list, self._fastd_list = talib.STOCHRSI(np.array(
         self._close_list),
                                                         timeperiod=14,
                                                         fastk_period=3,
                                                         fastd_period=3,
                                                         fastd_matype=0)
Exemple #8
0
 def STOCHRSI(self, timeperiod, fastk_period, fastd_period, kline=None):
     """
     计算STOCHRSI
     :param timeperiod: 参数1
     :param fastk_period:参数2
     :param fastd_period:参数3
     :param kline:回测时传入指定k线数据
     :return: 返回一个字典  {'STOCHRSI': STOCHRSI数组, 'fastk': fastk数组}
     """
     if kline:
         records = kline
     else:
         records = self.__platform.get_kline(self.__time_frame)
         records.reverse()
     kline_length = len(records)
     close_array = np.zeros(kline_length)
     t = 0
     for item in records:
         close_array[t] = item[4]
         t += 1
     result = (talib.STOCHRSI(close_array,
                              timeperiod=timeperiod,
                              fastk_period=fastk_period,
                              fastd_period=fastd_period,
                              fastd_matype=0))
     STOCHRSI = result[1]
     fastk = talib.MA(STOCHRSI, 3)
     dict = {'stochrsi': STOCHRSI, 'fastk': fastk}
     return dict
def technical_index(context):
    #EMA,RSI指标
    context.var.ts['EMA_5min'] = talib.EMA(np.array(context.var.ts.close),
                                           timeperiod=5)
    context.var.ts['EMA_10min'] = talib.EMA(np.array(context.var.ts.close),
                                            timeperiod=10)
    context.var.ts['EMA_15min'] = talib.EMA(np.array(context.var.ts.close),
                                            timeperiod=15)
    context.var.ts['EMA_20min'] = talib.EMA(np.array(context.var.ts.close),
                                            timeperiod=20)
    context.var.ts['RSI'] = talib.RSI(np.array(context.var.ts.close))
    STOCHRSI_usual = talib.STOCHRSI(np.array(context.var.ts.close))

    # BOLL-BAND指标
    BBANDS_usual = talib.BBANDS(np.array(context.var.ts.close))
    upperband, middleband, lowerband = BBANDS_usual
    context.var.ts['upperband'] = upperband
    context.var.ts['middleband'] = middleband
    context.var.ts['lowerband'] = lowerband

    arrClose = np.array(context.var.ts.close)
    arrHigh = np.array(context.var.ts.high)
    arrLow = np.array(context.var.ts.low)
    arrVolume = np.array(context.var.ts.volume, dtype=np.float)
    # MACD指标
    MACD_usual = talib.MACD(arrClose)
    macd, macdsignal, macdhist = MACD_usual
    context.var.ts['macd'] = macd
    context.var.ts['macdsignal'] = macdsignal
    context.var.ts['macdhist'] = macdhist

    # KDJ指标
    KDJ_usual = talib.STOCH(arrHigh, arrLow, arrClose)
    slowk, slowd = KDJ_usual
    context.var.ts['slowk'] = slowk
    context.var.ts['slowd'] = slowd

    # ATR指标
    ATR_usual = talib.ATR(arrHigh, arrLow, arrClose)
    context.var.ts['ATR'] = ATR_usual

    # WILLR指标
    WILLR_usual = talib.WILLR(arrHigh, arrLow, arrClose)
    context.var.ts['WILLR'] = WILLR_usual

    # BOV指标
    OBV_usual = talib.OBV(arrClose, arrVolume)
    context.var.ts['OBV'] = OBV_usual

    # SAR指标
    SAR_usual = talib.SAR(arrHigh, arrLow)
    context.var.ts['SAR'] = SAR_usual

    # DEMA指标
    DEMA_usual = talib.DEMA(arrClose)
    context.var.ts['DEMA'] = DEMA_usual

    #MOM指标
    MOM_usual = talib.MOM(arrClose)
    context.var.ts['MOM'] = MOM_usual
Exemple #10
0
def StochRSI(data):
    fastk, fastd = ta.STOCHRSI(data,
                               timeperiod=14,
                               fastk_period=5,
                               fastd_period=3,
                               fastd_matype=0)
    return fastk, fastd
Exemple #11
0
	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)
Exemple #12
0
def stochrsi(close, graph=False, **kwargs):
    '''
    STOCHRSI - Stochastic Relative Strength Index
    '''
    result = talib.STOCHRSI(close, **kwargs)
    df = pd.concat([pd.DataFrame(close), pd.DataFrame(result).T], axis=1)
    df.columns = ['close', 'fastk', 'fastd']
    if graph:
        title = 'STOCHRSI - Stochastic Relative Strength Index'
        fname = '20_stochrsi.png'

        fig, axes = plt.subplots(nrows=2,
                                 sharex=True,
                                 gridspec_kw={'height_ratios': [2, 1]})
        fig.suptitle(title)
        axes[0].plot(df['close'], label='close')
        axes[1].plot(df['fastk'], label='fastk')
        axes[1].plot(df['fastd'], label='fastd')
        for ax in axes:
            ax.legend(loc='upper left')
        plt.savefig(fname)
        if SHOW_GRAPH:
            plt.show()
        plt.close()
    return df
Exemple #13
0
 def STOCHRSI(self, timeperiod, fastk_period, fastd_period):
     """
     计算STOCHRSI
     :param self.platform: 交易所
     :param self.instrument_id: 合约ID
     :param self.time_frame: k线周期
     :return: 返回一个字典  {'STOCHRSI': STOCHRSI数组, 'fastk': fastk数组}
     """
     records = self.platform.get_kline(self.time_frame)
     records.reverse()
     kline_length = len(records)
     close_array = np.zeros(kline_length)
     t = 0
     for item in records:
         close_array[t] = item[4]
         t += 1
     result = (talib.STOCHRSI(close_array,
                              timeperiod=timeperiod,
                              fastk_period=fastk_period,
                              fastd_period=fastd_period,
                              fastd_matype=0))
     STOCHRSI = result[1]
     fastk = talib.MA(STOCHRSI, 3)
     dict = {'stochrsi': STOCHRSI, 'fastk': fastk}
     return dict
Exemple #14
0
def add_features(df):
    # add MACD, CCI, ATR, BOLL, EMA20, MA5, MA10, MOM6, MOM12, ROC, RSI, WR, KDJ
    open_ = df['open'].values
    high = df['high'].values
    low = df['low'].values
    close = df['close'].values

    macd, macdsignal, macdhist = tb.MACD(close,
                                         fastperiod=12,
                                         slowperiod=26,
                                         signalperiod=9)
    cci = tb.CCI(high, low, close, timeperiod=14)
    atr = tb.ATR(high, low, close, timeperiod=14)
    boll_up, boll_mid, boll_low = tb.BBANDS(close,
                                            timeperiod=5,
                                            nbdevup=2,
                                            nbdevdn=2,
                                            matype=0)
    ema20 = tb.EMA(close, timeperiod=20)
    ma5 = tb.SMA(close, timeperiod=5)
    ma10 = tb.SMA(close, timeperiod=10)
    mom6 = tb.MOM(close, timeperiod=6)
    mom12 = tb.MOM(close, timeperiod=12)
    roc = tb.ROC(close, timeperiod=14)
    rsi_k, rsi_d = tb.STOCHRSI(close,
                               timeperiod=14,
                               fastk_period=5,
                               fastd_period=3,
                               fastd_matype=0)
    wr = tb.WILLR(high, low, close, timeperiod=14)
    kdj_j = 3 * rsi_k - 2 * rsi_d

    feat_list = [
        macd, macdsignal, macdhist, cci, atr, boll_up, boll_mid, boll_low,
        ema20, ma5, ma10, mom6, mom12, roc, rsi_k, rsi_d, wr, kdj_j
    ]

    handle_nan(feat_list)

    df.insert(len(df.columns), 'macd', macd)
    df.insert(len(df.columns), 'macdsignal', macdsignal)
    df.insert(len(df.columns), 'macdhist', macdhist)
    df.insert(len(df.columns), 'cci', cci)
    df.insert(len(df.columns), 'atr', atr)
    df.insert(len(df.columns), 'boll_up', boll_up)
    df.insert(len(df.columns), 'boll_mid', boll_mid)
    df.insert(len(df.columns), 'boll_low', boll_low)
    df.insert(len(df.columns), 'ema20', ema20)
    df.insert(len(df.columns), 'ma5', ma5)
    df.insert(len(df.columns), 'ma10', ma10)
    df.insert(len(df.columns), 'mom6', mom6)
    df.insert(len(df.columns), 'mom12', mom12)
    df.insert(len(df.columns), 'roc', roc)
    df.insert(len(df.columns), 'rsi_k', rsi_k)
    df.insert(len(df.columns), 'rsi_d', rsi_d)
    df.insert(len(df.columns), 'wr', wr)
    df.insert(len(df.columns), 'kdj_j', kdj_j)

    return df
Exemple #15
0
def TALIB_STOCHRSI(close,
                   timeperiod=14,
                   fastk_period=5,
                   fastd_period=3,
                   fastd_matype=talib.MA_Type.SMA):
    '''00384,5,2'''
    return talib.STOCHRSI(close, timeperiod, fastk_period, fastd_period,
                          fastd_matype)
Exemple #16
0
 def calculate_stochrsi(self, period_name, closing_prices):
     fastk, fastd = talib.STOCHRSI(closing_prices,
                                   timeperiod=14,
                                   fastk_period=3,
                                   fastd_period=3,
                                   fastd_matype=0)
     self.current_indicators[period_name]['stochrsi_fastk'] = fastk[-1]
     self.current_indicators[period_name]['stochrsi_fastd'] = fastd[-1]
Exemple #17
0
 def handle_data(self, kl):
     self.kl = kl  #.copy()
     self.data['t'] = kl['t']
     self.data['fastk'], self.data['fastd'] = ta.STOCHRSI(
         kl['c'], **self.params)
     self.data['up'] = pd.Series([80] * kl['c'].size)
     self.data['low'] = pd.Series([20] * kl['c'].size)
     self.data['mid'] = pd.Series([50] * kl['c'].size)
Exemple #18
0
def STOCHRSI(df, n=14, fastk_period=5, fastd_period=3):
    fastk, fastd = talib.STOCHRSI(df['close'].valkues,
                                  timeperiod=n,
                                  fastk_period=fastk_period,
                                  fastd_period=fastd_period)
    fk = pd.Series(fastk, index=df.index, name="STOCRSI_FK_%s" % (str(n)))
    fd = pd.Series(fastd, index=df.index, name="STOCRSI_FD_%s" % (str(n)))
    return pd.concat([fk, fd], join='outer', axis=1)
Exemple #19
0
def get_stochrsi(ohlc):
    stochrsi_fastk, stochrsi_fastd = ta.STOCHRSI(ohlc['4_close'], timeperiod=14, fastk_period=5, fastd_period=3,
                                                 fastd_matype=0)

    ohlc = ohlc.assign(
        stochrsi_fastk=stochrsi_fastk
        , stochrsi_fastd=stochrsi_fastd
    )
    return ohlc
def STOCHRSI(close,
             timeperiod=14,
             fastk_period=5,
             fastd_period=3,
             fastd_matype=0):
    # STOCHASTIC RSI
    fastk, fastd = ta.STOCHRSI(close, timeperiod, fastk_period, fastd_period,
                               fastd_matype)
    return fastk, fastd
Exemple #21
0
def STOCHRSI(Series,
             timeperiod=14,
             fastk_period=5,
             fastd_period=3,
             fastd_matype=0):
    fastk, fastd = talib.STOCHRSI(Series.values, fastk_period, fastd_period,
                                  fastd_matype)
    return pd.Series(fastk, index=Series.index), pd.Series(fastd,
                                                           index=Series.index)
Exemple #22
0
def AddStochRSI(df, indicator_list, askbidmid, timeperiod=14):
    df['rsi_fastk'], df['rsi_fastd'] = talib.STOCHRSI(
        df['{}_c'.format(askbidmid)].values,
        timeperiod=14,
        fastk_period=3,
        fastd_period=3,
        fastd_matype=1)
    indicator_list.extend(['rsi_fastk', 'rsi_fastd'])
    return df, indicator_list
Exemple #23
0
def stochastic_rsi(close, n, fast_period1=5, fast_period2=3, fastd_matype=0):
    try:
        fastk_data, fastd_data = talib.STOCHRSI(close, n, fast_period1,
                                                fast_period2, fastd_matype)
        df = pd.DataFrame()
        df['fast_period1'] = fastk_data
        df['fast_period2'] = fastd_data
        return df
    except Exception as e:
        raise (e)
Exemple #24
0
 def trade(self):
     rsi,k,d = talib.STOCHRSI(self.bar.close, self.rsi_time, self.rsi_fastk, self.rsi_fastd, self.rsi_fastd_matype);
     if self.trend():
         if crossover(k,d) and k <50:
             price=self.bar.close[-1]
             self.broker.buy(self.symbol,self.qty,price)
     else:
         if crossunder(k, d) and k > 50:
             price = self.bar.close[-1]
             self.broker.sell(self.symbol,self.qty,price)
Exemple #25
0
 def add_indicators(df):
     df['rsi'] = talib.RSI(np.array(df['close']))
     df['adx'] = talib.ADX(np.array(df['high']), np.array(df['low']),
                           np.array(df['close']))
     df['stochrsi_k'], df['stochrsi_d'] = talib.STOCHRSI(
         np.array(df['close']))
     df['macd'], df['macd_sig'], _ = talib.MACD(np.array(df['close']))
     df['boll_up'], df['boll_mid'], df['boll_low'] = talib.BBANDS(
         df['close'])
     df['datetime'] = df['datetime'].apply(datetime.datetime.fromtimestamp)
     return df
Exemple #26
0
 def STOCHRSI(Close,
              timeperiod=14,
              fastk_period=5,
              fastd_period=3,
              fastd_matype=0):
     fastk, fastd = pd.DataFrame(), pd.DataFrame()
     for i in Close.columns:
         fastk[i], fastd[i] = ta.STOCHRSI(Close[i], timeperiod,
                                          fastk_period, fastd_period,
                                          fastd_matype)
     return fastk, fastd
 def STOCHRSI(self, timeperiod=14, fastk_period=5, fastd_period=3, fastd_matype=0):
     real_data = np.array([self.df.close], dtype='f8')
     # fastk, fastd = talib.STOCHRSI(real_data[0], timeperiod=timeperiod, fastk_period=fastk_period,
     #                               fastd_period=fastd_period, fastd_matype=fastd_matype)
     # return go.Scatter(
     #     x=self.df.index,
     #     y=fastk,
     #     name='STOCHRSI'
     # )
     return talib.STOCHRSI(real_data[0], timeperiod=timeperiod, fastk_period=fastk_period,
                           fastd_period=fastd_period, fastd_matype=fastd_matype)
def STOCHRSI(raw_df,
             timeperiod=14,
             fastk_period=5,
             fastd_period=3,
             fastd_matype=0):
    # extract necessary data from raw dataframe (close)
    # returns 2 columns
    fastk, fastd = ta.STOCHRSI(raw_df.Close.values, timeperiod, fastk_period,
                               fastd_period, fastd_matype)
    singleMerged = np.stack((fastk, fastd), axis=-1)
    return singleMerged.tolist()
Exemple #29
0
 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)
Exemple #30
0
def measure_stochrsi(values_n: int, period: int, k_period: int, d_period: int):
    close = numpy.random.random(values_n)

    start = time.time_ns()
    output = StochRSI(period, period, k_period, d_period, close)
    output[-1]
    t1 = (time.time_ns() - start) / 1000000

    start = time.time_ns()
    output = talib.STOCHRSI(close, timeperiod = period, fastk_period=k_period, fastd_period=d_period)
    output[-1]
    t2 = (time.time_ns() - start) / 1000000

    t3 = 0.0
    output = None
    for i in range(period, values_n):
        close = numpy.random.random(i)
        start = time.time_ns()
        output = talib.STOCHRSI(close, timeperiod = period, fastk_period=k_period, fastd_period=d_period)
        t3 += (time.time_ns() - start) / 1000000

    print(f"StochRSI({period},{k_period},{d_period});{values_n:,};{round(t1, 3)};{round(t2, 3)};{round(t3, 3)}")