예제 #1
0
 def on_preparing_data(self, symbol, data, context: {}):
     """循环对 `linear_angle` 指定的日期区间,调用 ``talib.LINEARREG_ANGLE`` ,生成角度值。
         所有计算得到的值取绝对值,对没有值的内容填充 `99` (以示角度超大)。
         取到的所有值会放在 ``context`` 中,key值为 `angle` ;
         根据 `max_linear_angle` 设置的值对每隔日期区间的角度制取平均值或中位数。
         取得的值会放在 ``context`` 中,key值为 `max_angles` ;
     """
     angles = []
     max_angles = {}
     for angle in self.linear_angle:
         col = 'angle_{}'.format(angle)
         angles.append(col)
         data[col] = talib.LINEARREG_ANGLE(data['close'],
                                           angle).abs().fillna(99)  # 日均线角度
         if self.max_linear_angle == "MEAN":
             v = talib.LINEARREG_ANGLE(data['close'],
                                       angle).abs().mean()  # 日均线角度平均值
         else:
             v = talib.LINEARREG_ANGLE(data['close'],
                                       angle).abs().median()  # 日均线角度中位数
         if np.isnan(v):
             raise ValueError('{} LINEARREG_ANGLE is NaN'.format(angle))
         max_angles[col] = v
     context['angles'] = angles
     context['max_angles'] = max_angles
예제 #2
0
 def test_LINEARREG_ANGLE(self):
     self.env.add_operator('linearreg', {
         'operator': OperatorLINEARREG_ANGLE,
         })
     string = 'linearreg(14, open)'
     gene = self.env.parse_string(string)
     self.assertRaises(IndexError, gene.eval, self.env, self.dates[12], self.dates[-1])
     df = gene.eval(self.env, self.dates[13], self.dates[14])
     ser0, ser1 = df.iloc[0], df.iloc[1]
     o = self.env.get_data_value('open').values
     res0, res1, res = [], [], []
     for i in df.columns:
         res0.append(talib.LINEARREG_ANGLE(o[:14, i], timeperiod=14)[-1] == ser0[i])
         res1.append(talib.LINEARREG_ANGLE(o[1:14+1, i], timeperiod=14)[-1] == ser1[i])
         res.append(talib.LINEARREG_ANGLE(o[:14+1, i], timeperiod=14)[-1] == ser1[i])
     self.assertTrue(all(res0) and all(res1) and all(res))
def trend(df, target_list):
    for target in target_list:
        trend = talib.LINEARREG_ANGLE(df[target], timeperiod=10)

        df['TREND_' + target] = trend

    return df
예제 #4
0
 def eval(self, environment, gene, date1, date2):
     timeperiod = (gene.next_value(environment, date1, date2))
     date1 = environment.shift_date(date1, -(timeperiod - 1), -1)
     df = gene.next_value(environment, date1, date2)
     res = df.apply(lambda x: pd.Series(talib.LINEARREG_ANGLE(
         x.values, timeperiod=timeperiod),
                                        index=df.index))
     return res.iloc[timeperiod - 1:]
예제 #5
0
def LINEARREG_ANGLE(close, timeperiod=14):
    ''' Linear Regression Angle 线性回归的角度

    分组: Statistic Functions 统计函数

    简介:

    real = LINEARREG_ANGLE(close, timeperiod=14)
    '''
    return talib.LINEARREG_ANGLE(close, timeperiod)
예제 #6
0
def get_angle(ss, p=4, ang_type='degree'):
    fss = np.nan_to_num(ss, 0)
    scale = (max(fss) - min(fss)) / 2 * 1.618
    nfss = fss / scale
    # pdb.set_trace()
    angle = talib.LINEARREG_ANGLE(nfss, timeperiod=p)
    if ang_type == 'degree':
        return np.arctan(angle) / np.pi * 180
    elif ang_type == 'pi':
        return np.arctan(angle)
    else:
        return angle
예제 #7
0
def _talib_LINEARREG_ANGLE(data, n):
    if n <= 1:
        value = np.zeros_like(data)
    else:
        try:
            value = talib.LINEARREG_ANGLE(data, timeperiod=n)
            ss = pd.Series(value).ffill().fillna(0)
            value = ss.values
        except Exception as e:
            raise Exception("[_talib_LINEARREG_ANGLE]", e)
            # print("[WARNING] _talib_LINEARREG_ANGLE: {}".format(e.args[0]))
            value = np.zeros_like(data)
    return value.astype(float)
예제 #8
0
    def test_linreg_angle(self):
        result = self.overlap.linreg(self.close, angle=True)
        self.assertIsInstance(result, Series)
        self.assertEqual(result.name, 'LRa_14')

        try:
            expected = tal.LINEARREG_ANGLE(self.close)
            pdt.assert_series_equal(result, expected, check_names=False)
        except AssertionError as ae:
            try:
                corr = pandas_ta.utils.df_error_analysis(result, expected, col=CORRELATION)
                self.assertGreater(corr, CORRELATION_THRESHOLD)
            except Exception as ex:
                error_analysis(result, CORRELATION, ex)
예제 #9
0
def getStatFunctions(df):
    high = df['High']
    low = df['Low']
    close = df['Close']
    open = df['Open']
    volume = df['Volume']

    df['BETA'] = ta.BETA(high, low, timeperiod=5)
    df['CORREL'] = ta.CORREL(high, low, timeperiod=30)
    df['LINREG'] = ta.LINEARREG(close, timeperiod=14)
    df['LINREGANGLE'] = ta.LINEARREG_ANGLE(close, timeperiod=14)
    df['LINREGINTERCEPT'] = ta.LINEARREG_INTERCEPT(close, timeperiod=14)
    df['LINREGSLOPE'] = ta.LINEARREG_SLOPE(close, timeperiod=14)
    df['STDDEV'] = ta.STDDEV(close, timeperiod=5, nbdev=1)
    df['TSF'] = ta.TSF(close, timeperiod=14)
    df['VAR'] = ta.VAR(close, timeperiod=5, nbdev=1)
예제 #10
0
파일: statistic.py 프로젝트: vkbrihma/pyEX
def linearreg_angle(client, symbol, timeframe="6m", closecol="close", period=14):
    """This will return a dataframe of linear regression angle for the given symbol across
    the given timeframe

    Args:
        client (pyEX.Client): Client
        symbol (string): Ticker
        timeframe (string): timeframe to use, for pyEX.chart
        closecol (string): column to use to calculate
        period (int): period to calculate adx across

    Returns:
        DataFrame: result
    """
    df = client.chartDF(symbol, timeframe)
    linearreg = t.LINEARREG_ANGLE(df[closecol].values, period)
    return pd.DataFrame({closecol: df[closecol].values, "lineearreg_angle": linearreg})
예제 #11
0
def linearreg_angle(candles: np.ndarray, period: int = 14, source_type: str = "close", sequential: bool = False) -> \
        Union[float, np.ndarray]:
    """
    LINEARREG_ANGLE - Linear Regression Angle

    :param candles: np.ndarray
    :param period: int - default: 14
    :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.LINEARREG_ANGLE(source, timeperiod=period)

    return res if sequential else res[-1]
예제 #12
0
def linearreg_angle(candles: np.ndarray, period: int = 14, source_type: str = "close", sequential: bool = False) -> \
        Union[float, np.ndarray]:
    """
    LINEARREG_ANGLE - Linear Regression Angle

    :param candles: np.ndarray
    :param period: int - default: 14
    :param source_type: str - default: "close"
    :param sequential: bool - default=False

    :return: float | np.ndarray
    """
    warmup_candles_num = get_config('env.data.warmup_candles_num', 240)
    if not sequential and len(candles) > warmup_candles_num:
        candles = candles[-warmup_candles_num:]

    source = get_candle_source(candles, source_type=source_type)
    res = talib.LINEARREG_ANGLE(source, timeperiod=period)

    return res if sequential else res[-1]
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()
예제 #14
0
def Stat_Function(dataframe):
	#Statistic Functions
	#BETA - Beta
	df[f'{ratio}_BETA'] = talib.BETA(High, Low, timeperiod=5)
	#CORREL - Pearson's Correlation Coefficient (r)
	df[f'{ratio}_CORREL'] = talib.CORREL(High, Low, timeperiod=30)
	#LINEARREG - Linear Regression
	df[f'{ratio}_LINEARREG'] = talib.LINEARREG(Close, timeperiod=14)
	#LINEARREG_ANGLE - Linear Regression Angle
	df[f'{ratio}_LINEARREG_ANGLE'] = talib.LINEARREG_ANGLE(Close, timeperiod=14)
	#LINEARREG_INTERCEPT - Linear Regression Intercept
	df[f'{ratio}_LINEARREG_INTERCEPT'] = talib.LINEARREG_INTERCEPT(Close, timeperiod=14)
	#LINEARREG_SLOPE - Linear Regression Slope
	df[f'{ratio}_LINEARREG_SLOPE'] = talib.LINEARREG_SLOPE(Close, timeperiod=14)
	#STDDEV - Standard Deviation
	df[f'{ratio}_STDDEV'] = talib.STDDEV(Close, timeperiod=5, nbdev=1)
	#TSF - Time Series Forecast
	df[f'{ratio}_TSF'] = talib.TSF(Close, timeperiod=14)
	#VAR - Variance
	df[f'{ratio}_VAR'] = talib.VAR(Close, timeperiod=5, nbdev=1)

	return
예제 #15
0
    def calculate(self, para):

        self.t = self.inputdata[:, 0]
        self.op = self.inputdata[:, 1]
        self.high = self.inputdata[:, 2]
        self.low = self.inputdata[:, 3]
        self.close = self.inputdata[:, 4]
        #adjusted close
        self.close1 = self.inputdata[:, 5]
        self.volume = self.inputdata[:, 6]
        #Overlap study

        #Overlap Studies
        #Overlap Studies
        if para is 'BBANDS':  #Bollinger Bands
            upperband, middleband, lowerband = ta.BBANDS(self.close,
                                                         timeperiod=self.tp,
                                                         nbdevup=2,
                                                         nbdevdn=2,
                                                         matype=0)
            self.output = [upperband, middleband, lowerband]

        elif para is 'DEMA':  #Double Exponential Moving Average
            self.output = ta.DEMA(self.close, timeperiod=self.tp)

        elif para is 'EMA':  #Exponential Moving Average
            self.output = ta.EMA(self.close, timeperiod=self.tp)

        elif para is 'HT_TRENDLINE':  #Hilbert Transform - Instantaneous Trendline
            self.output = ta.HT_TRENDLINE(self.close)

        elif para is 'KAMA':  #Kaufman Adaptive Moving Average
            self.output = ta.KAMA(self.close, timeperiod=self.tp)

        elif para is 'MA':  #Moving average
            self.output = ta.MA(self.close, timeperiod=self.tp, matype=0)

        elif para is 'MAMA':  #MESA Adaptive Moving Average
            mama, fama = ta.MAMA(self.close, fastlimit=0, slowlimit=0)

        elif para is 'MAVP':  #Moving average with variable period
            self.output = ta.MAVP(self.close,
                                  periods=10,
                                  minperiod=self.tp,
                                  maxperiod=self.tp1,
                                  matype=0)

        elif para is 'MIDPOINT':  #MidPoint over period
            self.output = ta.MIDPOINT(self.close, timeperiod=self.tp)

        elif para is 'MIDPRICE':  #Midpoint Price over period
            self.output = ta.MIDPRICE(self.high, self.low, timeperiod=self.tp)

        elif para is 'SAR':  #Parabolic SAR
            self.output = ta.SAR(self.high,
                                 self.low,
                                 acceleration=0,
                                 maximum=0)

        elif para is 'SAREXT':  #Parabolic SAR - Extended
            self.output = ta.SAREXT(self.high,
                                    self.low,
                                    startvalue=0,
                                    offsetonreverse=0,
                                    accelerationinitlong=0,
                                    accelerationlong=0,
                                    accelerationmaxlong=0,
                                    accelerationinitshort=0,
                                    accelerationshort=0,
                                    accelerationmaxshort=0)

        elif para is 'SMA':  #Simple Moving Average
            self.output = ta.SMA(self.close, timeperiod=self.tp)

        elif para is 'T3':  #Triple Exponential Moving Average (T3)
            self.output = ta.T3(self.close, timeperiod=self.tp, vfactor=0)

        elif para is 'TEMA':  #Triple Exponential Moving Average
            self.output = ta.TEMA(self.close, timeperiod=self.tp)

        elif para is 'TRIMA':  #Triangular Moving Average
            self.output = ta.TRIMA(self.close, timeperiod=self.tp)

        elif para is 'WMA':  #Weighted Moving Average
            self.output = ta.WMA(self.close, timeperiod=self.tp)

        #Momentum Indicators
        elif para is 'ADX':  #Average Directional Movement Index
            self.output = ta.ADX(self.high,
                                 self.low,
                                 self.close,
                                 timeperiod=self.tp)

        elif para is 'ADXR':  #Average Directional Movement Index Rating
            self.output = ta.ADXR(self.high,
                                  self.low,
                                  self.close,
                                  timeperiod=self.tp)

        elif para is 'APO':  #Absolute Price Oscillator
            self.output = ta.APO(self.close,
                                 fastperiod=12,
                                 slowperiod=26,
                                 matype=0)

        elif para is 'AROON':  #Aroon
            aroondown, aroonup = ta.AROON(self.high,
                                          self.low,
                                          timeperiod=self.tp)
            self.output = [aroondown, aroonup]

        elif para is 'AROONOSC':  #Aroon Oscillator
            self.output = ta.AROONOSC(self.high, self.low, timeperiod=self.tp)

        elif para is 'BOP':  #Balance Of Power
            self.output = ta.BOP(self.op, self.high, self.low, self.close)

        elif para is 'CCI':  #Commodity Channel Index
            self.output = ta.CCI(self.high,
                                 self.low,
                                 self.close,
                                 timeperiod=self.tp)

        elif para is 'CMO':  #Chande Momentum Oscillator
            self.output = ta.CMO(self.close, timeperiod=self.tp)

        elif para is 'DX':  #Directional Movement Index
            self.output = ta.DX(self.high,
                                self.low,
                                self.close,
                                timeperiod=self.tp)

        elif para is 'MACD':  #Moving Average Convergence/Divergence
            macd, macdsignal, macdhist = ta.MACD(self.close,
                                                 fastperiod=12,
                                                 slowperiod=26,
                                                 signalperiod=9)
            self.output = [macd, macdsignal, macdhist]
        elif para is 'MACDEXT':  #MACD with controllable MA type
            macd, macdsignal, macdhist = ta.MACDEXT(self.close,
                                                    fastperiod=12,
                                                    fastmatype=0,
                                                    slowperiod=26,
                                                    slowmatype=0,
                                                    signalperiod=9,
                                                    signalmatype=0)
            self.output = [macd, macdsignal, macdhist]
        elif para is 'MACDFIX':  #Moving Average Convergence/Divergence Fix 12/26
            macd, macdsignal, macdhist = ta.MACDFIX(self.close, signalperiod=9)
            self.output = [macd, macdsignal, macdhist]
        elif para is 'MFI':  #Money Flow Index
            self.output = ta.MFI(self.high,
                                 self.low,
                                 self.close,
                                 self.volume,
                                 timeperiod=self.tp)

        elif para is 'MINUS_DI':  #Minus Directional Indicator
            self.output = ta.MINUS_DI(self.high,
                                      self.low,
                                      self.close,
                                      timeperiod=self.tp)

        elif para is 'MINUS_DM':  #Minus Directional Movement
            self.output = ta.MINUS_DM(self.high, self.low, timeperiod=self.tp)

        elif para is 'MOM':  #Momentum
            self.output = ta.MOM(self.close, timeperiod=10)

        elif para is 'PLUS_DI':  #Plus Directional Indicator
            self.output = ta.PLUS_DI(self.high,
                                     self.low,
                                     self.close,
                                     timeperiod=self.tp)

        elif para is 'PLUS_DM':  #Plus Directional Movement
            self.output = ta.PLUS_DM(self.high, self.low, timeperiod=self.tp)

        elif para is 'PPO':  #Percentage Price Oscillator
            self.output = ta.PPO(self.close,
                                 fastperiod=12,
                                 slowperiod=26,
                                 matype=0)

        elif para is 'ROC':  #Rate of change : ((price/prevPrice)-1)*100
            self.output = ta.ROC(self.close, timeperiod=10)

        elif para is 'ROCP':  #Rate of change Percentage: (price-prevPrice)/prevPrice
            self.output = ta.ROCP(self.close, timeperiod=10)

        elif para is 'ROCR':  #Rate of change ratio: (price/prevPrice)
            self.output = ta.ROCR(self.close, timeperiod=10)

        elif para is 'ROCR100':  #Rate of change ratio 100 scale: (price/prevPrice)*100
            self.output = ta.ROCR100(self.close, timeperiod=10)

        elif para is 'RSI':  #Relative Strength Index
            self.output = ta.RSI(self.close, timeperiod=self.tp)

        elif para is 'STOCH':  #Stochastic
            slowk, slowd = ta.STOCH(self.high,
                                    self.low,
                                    self.close,
                                    fastk_period=5,
                                    slowk_period=3,
                                    slowk_matype=0,
                                    slowd_period=3,
                                    slowd_matype=0)
            self.output = [slowk, slowd]

        elif para is 'STOCHF':  #Stochastic Fast
            fastk, fastd = ta.STOCHF(self.high,
                                     self.low,
                                     self.close,
                                     fastk_period=5,
                                     fastd_period=3,
                                     fastd_matype=0)
            self.output = [fastk, fastd]

        elif para is 'STOCHRSI':  #Stochastic Relative Strength Index
            fastk, fastd = ta.STOCHRSI(self.close,
                                       timeperiod=self.tp,
                                       fastk_period=5,
                                       fastd_period=3,
                                       fastd_matype=0)
            self.output = [fastk, fastd]

        elif para is 'TRIX':  #1-day Rate-Of-Change (ROC) of a Triple Smooth EMA
            self.output = ta.TRIX(self.close, timeperiod=self.tp)

        elif para is 'ULTOSC':  #Ultimate Oscillator
            self.output = ta.ULTOSC(self.high,
                                    self.low,
                                    self.close,
                                    timeperiod1=self.tp,
                                    timeperiod2=self.tp1,
                                    timeperiod3=self.tp2)

        elif para is 'WILLR':  #Williams' %R
            self.output = ta.WILLR(self.high,
                                   self.low,
                                   self.close,
                                   timeperiod=self.tp)

        # Volume Indicators    : #
        elif para is 'AD':  #Chaikin A/D Line
            self.output = ta.AD(self.high, self.low, self.close, self.volume)

        elif para is 'ADOSC':  #Chaikin A/D Oscillator
            self.output = ta.ADOSC(self.high,
                                   self.low,
                                   self.close,
                                   self.volume,
                                   fastperiod=3,
                                   slowperiod=10)

        elif para is 'OBV':  #On Balance Volume
            self.output = ta.OBV(self.close, self.volume)

    # Volatility Indicators: #
        elif para is 'ATR':  #Average True Range
            self.output = ta.ATR(self.high,
                                 self.low,
                                 self.close,
                                 timeperiod=self.tp)

        elif para is 'NATR':  #Normalized Average True Range
            self.output = ta.NATR(self.high,
                                  self.low,
                                  self.close,
                                  timeperiod=self.tp)

        elif para is 'TRANGE':  #True Range
            self.output = ta.TRANGE(self.high, self.low, self.close)

        #Price Transform      : #
        elif para is 'AVGPRICE':  #Average Price
            self.output = ta.AVGPRICE(self.op, self.high, self.low, self.close)

        elif para is 'MEDPRICE':  #Median Price
            self.output = ta.MEDPRICE(self.high, self.low)

        elif para is 'TYPPRICE':  #Typical Price
            self.output = ta.TYPPRICE(self.high, self.low, self.close)

        elif para is 'WCLPRICE':  #Weighted Close Price
            self.output = ta.WCLPRICE(self.high, self.low, self.close)

        #Cycle Indicators     : #
        elif para is 'HT_DCPERIOD':  #Hilbert Transform - Dominant Cycle Period
            self.output = ta.HT_DCPERIOD(self.close)

        elif para is 'HT_DCPHASE':  #Hilbert Transform - Dominant Cycle Phase
            self.output = ta.HT_DCPHASE(self.close)

        elif para is 'HT_PHASOR':  #Hilbert Transform - Phasor Components
            inphase, quadrature = ta.HT_PHASOR(self.close)
            self.output = [inphase, quadrature]

        elif para is 'HT_SINE':  #Hilbert Transform - SineWave #2
            sine, leadsine = ta.HT_SINE(self.close)
            self.output = [sine, leadsine]

        elif para is 'HT_TRENDMODE':  #Hilbert Transform - Trend vs Cycle Mode
            self.integer = ta.HT_TRENDMODE(self.close)

        #Pattern Recognition  : #
        elif para is 'CDL2CROWS':  #Two Crows
            self.integer = ta.CDL2CROWS(self.op, self.high, self.low,
                                        self.close)

        elif para is 'CDL3BLACKCROWS':  #Three Black Crows
            self.integer = ta.CDL3BLACKCROWS(self.op, self.high, self.low,
                                             self.close)

        elif para is 'CDL3INSIDE':  #Three Inside Up/Down
            self.integer = ta.CDL3INSIDE(self.op, self.high, self.low,
                                         self.close)

        elif para is 'CDL3LINESTRIKE':  #Three-Line Strike
            self.integer = ta.CDL3LINESTRIKE(self.op, self.high, self.low,
                                             self.close)

        elif para is 'CDL3OUTSIDE':  #Three Outside Up/Down
            self.integer = ta.CDL3OUTSIDE(self.op, self.high, self.low,
                                          self.close)

        elif para is 'CDL3STARSINSOUTH':  #Three Stars In The South
            self.integer = ta.CDL3STARSINSOUTH(self.op, self.high, self.low,
                                               self.close)

        elif para is 'CDL3WHITESOLDIERS':  #Three Advancing White Soldiers
            self.integer = ta.CDL3WHITESOLDIERS(self.op, self.high, self.low,
                                                self.close)

        elif para is 'CDLABANDONEDBABY':  #Abandoned Baby
            self.integer = ta.CDLABANDONEDBABY(self.op,
                                               self.high,
                                               self.low,
                                               self.close,
                                               penetration=0)

        elif para is 'CDLBELTHOLD':  #Belt-hold
            self.integer = ta.CDLBELTHOLD(self.op, self.high, self.low,
                                          self.close)

        elif para is 'CDLBREAKAWAY':  #Breakaway
            self.integer = ta.CDLBREAKAWAY(self.op, self.high, self.low,
                                           self.close)

        elif para is 'CDLCLOSINGMARUBOZU':  #Closing Marubozu
            self.integer = ta.CDLCLOSINGMARUBOZU(self.op, self.high, self.low,
                                                 self.close)

        elif para is 'CDLCONCEALBABYSWALL':  #Concealing Baby Swallow
            self.integer = ta.CDLCONCEALBABYSWALL(self.op, self.high, self.low,
                                                  self.close)

        elif para is 'CDLCOUNTERATTACK':  #Counterattack
            self.integer = ta.CDLCOUNTERATTACK(self.op, self.high, self.low,
                                               self.close)

        elif para is 'CDLDARKCLOUDCOVER':  #Dark Cloud Cover
            self.integer = ta.CDLDARKCLOUDCOVER(self.op,
                                                self.high,
                                                self.low,
                                                self.close,
                                                penetration=0)

        elif para is 'CDLDOJI':  #Doji
            self.integer = ta.CDLDOJI(self.op, self.high, self.low, self.close)

        elif para is 'CDLDOJISTAR':  #Doji Star
            self.integer = ta.CDLDOJISTAR(self.op, self.high, self.low,
                                          self.close)

        elif para is 'CDLDRAGONFLYDOJI':  #Dragonfly Doji
            self.integer = ta.CDLDRAGONFLYDOJI(self.op, self.high, self.low,
                                               self.close)

        elif para is 'CDLENGULFING':  #Engulfing Pattern
            self.integer = ta.CDLENGULFING(self.op, self.high, self.low,
                                           self.close)

        elif para is 'CDLEVENINGDOJISTAR':  #Evening Doji Star
            self.integer = ta.CDLEVENINGDOJISTAR(self.op,
                                                 self.high,
                                                 self.low,
                                                 self.close,
                                                 penetration=0)

        elif para is 'CDLEVENINGSTAR':  #Evening Star
            self.integer = ta.CDLEVENINGSTAR(self.op,
                                             self.high,
                                             self.low,
                                             self.close,
                                             penetration=0)

        elif para is 'CDLGAPSIDESIDEWHITE':  #Up/Down-gap side-by-side white lines
            self.integer = ta.CDLGAPSIDESIDEWHITE(self.op, self.high, self.low,
                                                  self.close)

        elif para is 'CDLGRAVESTONEDOJI':  #Gravestone Doji
            self.integer = ta.CDLGRAVESTONEDOJI(self.op, self.high, self.low,
                                                self.close)

        elif para is 'CDLHAMMER':  #Hammer
            self.integer = ta.CDLHAMMER(self.op, self.high, self.low,
                                        self.close)

        elif para is 'CDLHANGINGMAN':  #Hanging Man
            self.integer = ta.CDLHANGINGMAN(self.op, self.high, self.low,
                                            self.close)

        elif para is 'CDLHARAMI':  #Harami Pattern
            self.integer = ta.CDLHARAMI(self.op, self.high, self.low,
                                        self.close)

        elif para is 'CDLHARAMICROSS':  #Harami Cross Pattern
            self.integer = ta.CDLHARAMICROSS(self.op, self.high, self.low,
                                             self.close)

        elif para is 'CDLHIGHWAVE':  #High-Wave Candle
            self.integer = ta.CDLHIGHWAVE(self.op, self.high, self.low,
                                          self.close)

        elif para is 'CDLHIKKAKE':  #Hikkake Pattern
            self.integer = ta.CDLHIKKAKE(self.op, self.high, self.low,
                                         self.close)

        elif para is 'CDLHIKKAKEMOD':  #Modified Hikkake Pattern
            self.integer = ta.CDLHIKKAKEMOD(self.op, self.high, self.low,
                                            self.close)

        elif para is 'CDLHOMINGPIGEON':  #Homing Pigeon
            self.integer = ta.CDLHOMINGPIGEON(self.op, self.high, self.low,
                                              self.close)

        elif para is 'CDLIDENTICAL3CROWS':  #Identical Three Crows
            self.integer = ta.CDLIDENTICAL3CROWS(self.op, self.high, self.low,
                                                 self.close)

        elif para is 'CDLINNECK':  #In-Neck Pattern
            self.integer = ta.CDLINNECK(self.op, self.high, self.low,
                                        self.close)

        elif para is 'CDLINVERTEDHAMMER':  #Inverted Hammer
            self.integer = ta.CDLINVERTEDHAMMER(self.op, self.high, self.low,
                                                self.close)

        elif para is 'CDLKICKING':  #Kicking
            self.integer = ta.CDLKICKING(self.op, self.high, self.low,
                                         self.close)

        elif para is 'CDLKICKINGBYLENGTH':  #Kicking - bull/bear determined by the longer marubozu
            self.integer = ta.CDLKICKINGBYLENGTH(self.op, self.high, self.low,
                                                 self.close)

        elif para is 'CDLLADDERBOTTOM':  #Ladder Bottom
            self.integer = ta.CDLLADDERBOTTOM(self.op, self.high, self.low,
                                              self.close)

        elif para is 'CDLLONGLEGGEDDOJI':  #Long Legged Doji
            self.integer = ta.CDLLONGLEGGEDDOJI(self.op, self.high, self.low,
                                                self.close)

        elif para is 'CDLLONGLINE':  #Long Line Candle
            self.integer = ta.CDLLONGLINE(self.op, self.high, self.low,
                                          self.close)

        elif para is 'CDLMARUBOZU':  #Marubozu
            self.integer = ta.CDLMARUBOZU(self.op, self.high, self.low,
                                          self.close)

        elif para is 'CDLMATCHINGLOW':  #Matching Low
            self.integer = ta.CDLMATCHINGLOW(self.op, self.high, self.low,
                                             self.close)

        elif para is 'CDLMATHOLD':  #Mat Hold
            self.integer = ta.CDLMATHOLD(self.op,
                                         self.high,
                                         self.low,
                                         self.close,
                                         penetration=0)

        elif para is 'CDLMORNINGDOJISTAR':  #Morning Doji Star
            self.integer = ta.CDLMORNINGDOJISTAR(self.op,
                                                 self.high,
                                                 self.low,
                                                 self.close,
                                                 penetration=0)

        elif para is 'CDLMORNINGSTAR':  #Morning Star
            self.integer = ta.CDLMORNINGSTAR(self.op,
                                             self.high,
                                             self.low,
                                             self.close,
                                             penetration=0)

        elif para is 'CDLONNECK':  #On-Neck Pattern
            self.integer = ta.CDLONNECK(self.op, self.high, self.low,
                                        self.close)

        elif para is 'CDLPIERCING':  #Piercing Pattern
            self.integer = ta.CDLPIERCING(self.op, self.high, self.low,
                                          self.close)

        elif para is 'CDLRICKSHAWMAN':  #Rickshaw Man
            self.integer = ta.CDLRICKSHAWMAN(self.op, self.high, self.low,
                                             self.close)

        elif para is 'CDLRISEFALL3METHODS':  #Rising/Falling Three Methods
            self.integer = ta.CDLRISEFALL3METHODS(self.op, self.high, self.low,
                                                  self.close)

        elif para is 'CDLSEPARATINGLINES':  #Separating Lines
            self.integer = ta.CDLSEPARATINGLINES(self.op, self.high, self.low,
                                                 self.close)

        elif para is 'CDLSHOOTINGSTAR':  #Shooting Star
            self.integer = ta.CDLSHOOTINGSTAR(self.op, self.high, self.low,
                                              self.close)

        elif para is 'CDLSHORTLINE':  #Short Line Candle
            self.integer = ta.CDLSHORTLINE(self.op, self.high, self.low,
                                           self.close)

        elif para is 'CDLSPINNINGTOP':  #Spinning Top
            self.integer = ta.CDLSPINNINGTOP(self.op, self.high, self.low,
                                             self.close)

        elif para is 'CDLSTALLEDPATTERN':  #Stalled Pattern
            self.integer = ta.CDLSTALLEDPATTERN(self.op, self.high, self.low,
                                                self.close)

        elif para is 'CDLSTICKSANDWICH':  #Stick Sandwich
            self.integer = ta.CDLSTICKSANDWICH(self.op, self.high, self.low,
                                               self.close)

        elif para is 'CDLTAKURI':  #Takuri (Dragonfly Doji with very long lower shadow)
            self.integer = ta.CDLTAKURI(self.op, self.high, self.low,
                                        self.close)

        elif para is 'CDLTASUKIGAP':  #Tasuki Gap
            self.integer = ta.CDLTASUKIGAP(self.op, self.high, self.low,
                                           self.close)

        elif para is 'CDLTHRUSTING':  #Thrusting Pattern
            self.integer = ta.CDLTHRUSTING(self.op, self.high, self.low,
                                           self.close)

        elif para is 'CDLTRISTAR':  #Tristar Pattern
            self.integer = ta.CDLTRISTAR(self.op, self.high, self.low,
                                         self.close)

        elif para is 'CDLUNIQUE3RIVER':  #Unique 3 River
            self.integer = ta.CDLUNIQUE3RIVER(self.op, self.high, self.low,
                                              self.close)

        elif para is 'CDLUPSIDEGAP2CROWS':  #Upside Gap Two Crows
            self.integer = ta.CDLUPSIDEGAP2CROWS(self.op, self.high, self.low,
                                                 self.close)

        elif para is 'CDLXSIDEGAP3METHODS':  #Upside/Downside Gap Three Methods
            self.integer = ta.CDLXSIDEGAP3METHODS(self.op, self.high, self.low,
                                                  self.close)

        #Statistic Functions  : #
        elif para is 'BETA':  #Beta
            self.output = ta.BETA(self.high, self.low, timeperiod=5)

        elif para is 'CORREL':  #Pearson's Correlation Coefficient (r)
            self.output = ta.CORREL(self.high, self.low, timeperiod=self.tp)

        elif para is 'LINEARREG':  #Linear Regression
            self.output = ta.LINEARREG(self.close, timeperiod=self.tp)

        elif para is 'LINEARREG_ANGLE':  #Linear Regression Angle
            self.output = ta.LINEARREG_ANGLE(self.close, timeperiod=self.tp)

        elif para is 'LINEARREG_INTERCEPT':  #Linear Regression Intercept
            self.output = ta.LINEARREG_INTERCEPT(self.close,
                                                 timeperiod=self.tp)

        elif para is 'LINEARREG_SLOPE':  #Linear Regression Slope
            self.output = ta.LINEARREG_SLOPE(self.close, timeperiod=self.tp)

        elif para is 'STDDEV':  #Standard Deviation
            self.output = ta.STDDEV(self.close, timeperiod=5, nbdev=1)

        elif para is 'TSF':  #Time Series Forecast
            self.output = ta.TSF(self.close, timeperiod=self.tp)

        elif para is 'VAR':  #Variance
            self.output = ta.VAR(self.close, timeperiod=5, nbdev=1)

        else:
            print('You issued command:' + para)
예제 #16
0
def TALIB_LINEARREG_ANGLE(close, timeperiod=14):
    '''00349,2,1'''
    return talib.LINEARREG_ANGLE(close, timeperiod)
df['ATR3'] = abs(
    np.array(df['Low'].shift(1)) - np.array(df['Adj Close'].shift(1)))
df['AverageTrueRange'] = df[['ATR1', 'ATR2', 'ATR3']].max(axis=1)

# df['EMA']=pd.Series(pd.ewma(df['Adj Close'], span = n, min_periods = n - 1))

# Statistic Functions
df['Beta'] = ta.BETA(np.array(df['High'].shift(1)),
                     np.array(df['Low'].shift(1)),
                     timeperiod=n)
df['CORREL'] = ta.CORREL(np.array(df['High'].shift(1)),
                         np.array(df['Low'].shift(1)),
                         timeperiod=n)
df['LINEARREG'] = ta.LINEARREG(np.array(df['Adj Close'].shift(1)),
                               timeperiod=n)
df['LINEARREG_ANGLE'] = ta.LINEARREG_ANGLE(np.array(df['Adj Close'].shift(1)),
                                           timeperiod=n)
df['LINEARREG_INTERCEPT'] = ta.LINEARREG_INTERCEPT(np.array(
    df['Adj Close'].shift(1)),
                                                   timeperiod=n)
df['LINEARREG_SLOPE'] = ta.LINEARREG_SLOPE(np.array(df['Adj Close'].shift(1)),
                                           timeperiod=n)
df['STDDEV'] = ta.STDDEV(np.array(df['Adj Close'].shift(1)),
                         timeperiod=n,
                         nbdev=1)
df['Time Series Forecast'] = ta.TSF(np.array(df['Adj Close'].shift(1)),
                                    timeperiod=n)
df['VAR'] = ta.VAR(np.array(df['Adj Close'].shift(1)), timeperiod=n, nbdev=1)

# Overlap Studies Functions
df['upperband'], df['middleband'], df['lowerband'] = ta.BBANDS(np.array(
    df['Adj Close'].shift(1)),
예제 #18
0
def LINEARREG_ANGLE(data, **kwargs):
    _check_talib_presence()
    prices = _extract_series(data)
    return talib.LINEARREG_ANGLE(prices, **kwargs)
예제 #19
0
def extract_linear_reg(data):
    data['linear_reg_angle'] = ta.LINEARREG_ANGLE(data['close'].values,
                                                  timeperiod=short_period)
    data['linear_reg_slope'] = ta.LINEARREG_SLOPE(data['close'].values,
                                                  timeperiod=short_period)
    return data
예제 #20
0
def add_ta_features(df, ta_settings):
    """Add technial analysis features from typical financial dataset that
    typically include columns such as "open", "high", "low", "price" and
    "volume".

    http://mrjbq7.github.io/ta-lib/

    Args:
        df(pandas.DataFrame): original DataFrame.
        ta_settings(dict): configuration.
    Returns:
        pandas.DataFrame: DataFrame with new features included.
    """

    open = df['open']
    high = df['high']
    low = df['low']
    close = df['price']
    volume = df['volume']

    if ta_settings['overlap']:

        df['ta_overlap_bbands_upper'], df['ta_overlap_bbands_middle'], df[
            'ta_overlap_bbands_lower'] = ta.BBANDS(close,
                                                   timeperiod=5,
                                                   nbdevup=2,
                                                   nbdevdn=2,
                                                   matype=0)
        df['ta_overlap_dema'] = ta.DEMA(
            close, timeperiod=15)  # NOTE: Changed to avoid a lot of Nan values
        df['ta_overlap_ema'] = ta.EMA(close, timeperiod=30)
        df['ta_overlap_kama'] = ta.KAMA(close, timeperiod=30)
        df['ta_overlap_ma'] = ta.MA(close, timeperiod=30, matype=0)
        df['ta_overlap_mama_mama'], df['ta_overlap_mama_fama'] = ta.MAMA(close)
        period = np.random.randint(10, 20, size=len(close)).astype(float)
        df['ta_overlap_mavp'] = ta.MAVP(close,
                                        period,
                                        minperiod=2,
                                        maxperiod=30,
                                        matype=0)
        df['ta_overlap_midpoint'] = ta.MIDPOINT(close, timeperiod=14)
        df['ta_overlap_midprice'] = ta.MIDPRICE(high, low, timeperiod=14)
        df['ta_overlap_sar'] = ta.SAR(high, low, acceleration=0, maximum=0)
        df['ta_overlap_sarext'] = ta.SAREXT(high,
                                            low,
                                            startvalue=0,
                                            offsetonreverse=0,
                                            accelerationinitlong=0,
                                            accelerationlong=0,
                                            accelerationmaxlong=0,
                                            accelerationinitshort=0,
                                            accelerationshort=0,
                                            accelerationmaxshort=0)
        df['ta_overlap_sma'] = ta.SMA(close, timeperiod=30)
        df['ta_overlap_t3'] = ta.T3(close, timeperiod=5, vfactor=0)
        df['ta_overlap_tema'] = ta.TEMA(
            close, timeperiod=12)  # NOTE: Changed to avoid a lot of Nan values
        df['ta_overlap_trima'] = ta.TRIMA(close, timeperiod=30)
        df['ta_overlap_wma'] = ta.WMA(close, timeperiod=30)

        # NOTE: Commented to avoid a lot of Nan values
        # df['ta_overlap_ht_trendline'] = ta.HT_TRENDLINE(close)

    if ta_settings['momentum']:

        df['ta_momentum_adx'] = ta.ADX(high, low, close, timeperiod=14)
        df['ta_momentum_adxr'] = ta.ADXR(high, low, close, timeperiod=14)
        df['ta_momentum_apo'] = ta.APO(close,
                                       fastperiod=12,
                                       slowperiod=26,
                                       matype=0)
        df['ta_momentum_aroondown'], df['ta_momentum_aroonup'] = ta.AROON(
            high, low, timeperiod=14)
        df['ta_momentum_aroonosc'] = ta.AROONOSC(high, low, timeperiod=14)
        df['ta_momentum_bop'] = ta.BOP(open, high, low, close)
        df['ta_momentum_cci'] = ta.CCI(high, low, close, timeperiod=14)
        df['ta_momentum_cmo'] = ta.CMO(close, timeperiod=14)
        df['ta_momentum_dx'] = ta.DX(high, low, close, timeperiod=14)
        df['ta_momentum_macd_macd'], df['ta_momentum_macd_signal'], df[
            'ta_momentum_macd_hist'] = ta.MACD(close,
                                               fastperiod=12,
                                               slowperiod=26,
                                               signalperiod=9)
        df['ta_momentum_macdext_macd'], df['ta_momentum_macdext_signal'], df[
            'ta_momentum_macdext_hist'] = ta.MACDEXT(close,
                                                     fastperiod=12,
                                                     fastmatype=0,
                                                     slowperiod=26,
                                                     slowmatype=0,
                                                     signalperiod=9,
                                                     signalmatype=0)
        df['ta_momentum_macdfix_macd'], df['ta_momentum_macdfix_signal'], df[
            'ta_momentum_macdfix_hist'] = ta.MACDFIX(close, signalperiod=9)
        df['ta_momentum_mfi'] = ta.MFI(high, low, close, volume, timeperiod=14)
        df['ta_momentum_minus_di'] = ta.MINUS_DI(high,
                                                 low,
                                                 close,
                                                 timeperiod=14)
        df['ta_momentum_minus_dm'] = ta.MINUS_DM(high, low, timeperiod=14)
        df['ta_momentum_mom'] = ta.MOM(close, timeperiod=10)
        df['ta_momentum_plus_di'] = ta.PLUS_DI(high, low, close, timeperiod=14)
        df['ta_momentum_plus_dm'] = ta.PLUS_DM(high, low, timeperiod=14)
        df['ta_momentum_ppo'] = ta.PPO(close,
                                       fastperiod=12,
                                       slowperiod=26,
                                       matype=0)
        df['ta_momentum_roc'] = ta.ROC(close, timeperiod=10)
        df['ta_momentum_rocp'] = ta.ROCP(close, timeperiod=10)
        df['ta_momentum_rocr'] = ta.ROCR(close, timeperiod=10)
        df['ta_momentum_rocr100'] = ta.ROCR100(close, timeperiod=10)
        df['ta_momentum_rsi'] = ta.RSI(close, timeperiod=14)
        df['ta_momentum_slowk'], df['ta_momentum_slowd'] = ta.STOCH(
            high,
            low,
            close,
            fastk_period=5,
            slowk_period=3,
            slowk_matype=0,
            slowd_period=3,
            slowd_matype=0)
        df['ta_momentum_fastk'], df['ta_momentum_fastd'] = ta.STOCHF(
            high, low, close, fastk_period=5, fastd_period=3, fastd_matype=0)
        df['ta_momentum_fastk'], df['ta_momentum_fastd'] = ta.STOCHRSI(
            close,
            timeperiod=14,
            fastk_period=5,
            fastd_period=3,
            fastd_matype=0)
        df['ta_momentum_trix'] = ta.TRIX(
            close, timeperiod=12)  # NOTE: Changed to avoid a lot of Nan values
        df['ta_momentum_ultosc'] = ta.ULTOSC(high,
                                             low,
                                             close,
                                             timeperiod1=7,
                                             timeperiod2=14,
                                             timeperiod3=28)
        df['ta_momentum_willr'] = ta.WILLR(high, low, close, timeperiod=14)

    if ta_settings['volume']:

        df['ta_volume_ad'] = ta.AD(high, low, close, volume)
        df['ta_volume_adosc'] = ta.ADOSC(high,
                                         low,
                                         close,
                                         volume,
                                         fastperiod=3,
                                         slowperiod=10)
        df['ta_volume_obv'] = ta.OBV(close, volume)

    if ta_settings['volatility']:

        df['ta_volatility_atr'] = ta.ATR(high, low, close, timeperiod=14)
        df['ta_volatility_natr'] = ta.NATR(high, low, close, timeperiod=14)
        df['ta_volatility_trange'] = ta.TRANGE(high, low, close)

    if ta_settings['price']:

        df['ta_price_avgprice'] = ta.AVGPRICE(open, high, low, close)
        df['ta_price_medprice'] = ta.MEDPRICE(high, low)
        df['ta_price_typprice'] = ta.TYPPRICE(high, low, close)
        df['ta_price_wclprice'] = ta.WCLPRICE(high, low, close)

    if ta_settings['cycle']:

        df['ta_cycle_ht_dcperiod'] = ta.HT_DCPERIOD(close)
        df['ta_cycle_ht_phasor_inphase'], df[
            'ta_cycle_ht_phasor_quadrature'] = ta.HT_PHASOR(close)
        df['ta_cycle_ht_trendmode'] = ta.HT_TRENDMODE(close)

        # NOTE: Commented to avoid a lot of Nan values
        # df['ta_cycle_ht_dcphase'] = ta.HT_DCPHASE(close)
        # df['ta_cycle_ht_sine_sine'], df['ta_cycle_ht_sine_leadsine'] = ta.HT_SINE(close)

    if ta_settings['pattern']:

        df['ta_pattern_cdl2crows'] = ta.CDL2CROWS(open, high, low, close)
        df['ta_pattern_cdl3blackrows'] = ta.CDL3BLACKCROWS(
            open, high, low, close)
        df['ta_pattern_cdl3inside'] = ta.CDL3INSIDE(open, high, low, close)
        df['ta_pattern_cdl3linestrike'] = ta.CDL3LINESTRIKE(
            open, high, low, close)
        df['ta_pattern_cdl3outside'] = ta.CDL3OUTSIDE(open, high, low, close)
        df['ta_pattern_cdl3starsinsouth'] = ta.CDL3STARSINSOUTH(
            open, high, low, close)
        df['ta_pattern_cdl3whitesoldiers'] = ta.CDL3WHITESOLDIERS(
            open, high, low, close)
        df['ta_pattern_cdlabandonedbaby'] = ta.CDLABANDONEDBABY(open,
                                                                high,
                                                                low,
                                                                close,
                                                                penetration=0)
        df['ta_pattern_cdladvanceblock'] = ta.CDLADVANCEBLOCK(
            open, high, low, close)
        df['ta_pattern_cdlbelthold'] = ta.CDLBELTHOLD(open, high, low, close)
        df['ta_pattern_cdlbreakaway'] = ta.CDLBREAKAWAY(open, high, low, close)
        df['ta_pattern_cdlclosingmarubozu'] = ta.CDLCLOSINGMARUBOZU(
            open, high, low, close)
        df['ta_pattern_cdlconcealbabyswall'] = ta.CDLCONCEALBABYSWALL(
            open, high, low, close)
        df['ta_pattern_cdlcounterattack'] = ta.CDLCOUNTERATTACK(
            open, high, low, close)
        df['ta_pattern_cdldarkcloudcover'] = ta.CDLDARKCLOUDCOVER(
            open, high, low, close, penetration=0)
        df['ta_pattern_cdldoji'] = ta.CDLDOJI(open, high, low, close)
        df['ta_pattern_cdldojistar'] = ta.CDLDOJISTAR(open, high, low, close)
        df['ta_pattern_cdldragonflydoji'] = ta.CDLDRAGONFLYDOJI(
            open, high, low, close)
        df['ta_pattern_cdlengulfing'] = ta.CDLENGULFING(open, high, low, close)
        df['ta_pattern_cdleveningdojistar'] = ta.CDLEVENINGDOJISTAR(
            open, high, low, close, penetration=0)
        df['ta_pattern_cdleveningstar'] = ta.CDLEVENINGSTAR(open,
                                                            high,
                                                            low,
                                                            close,
                                                            penetration=0)
        df['ta_pattern_cdlgapsidesidewhite'] = ta.CDLGAPSIDESIDEWHITE(
            open, high, low, close)
        df['ta_pattern_cdlgravestonedoji'] = ta.CDLGRAVESTONEDOJI(
            open, high, low, close)
        df['ta_pattern_cdlhammer'] = ta.CDLHAMMER(open, high, low, close)
        df['ta_pattern_cdlhangingman'] = ta.CDLHANGINGMAN(
            open, high, low, close)
        df['ta_pattern_cdlharami'] = ta.CDLHARAMI(open, high, low, close)
        df['ta_pattern_cdlharamicross'] = ta.CDLHARAMICROSS(
            open, high, low, close)
        df['ta_pattern_cdlhighwave'] = ta.CDLHIGHWAVE(open, high, low, close)
        df['ta_pattern_cdlhikkake'] = ta.CDLHIKKAKE(open, high, low, close)
        df['ta_pattern_cdlhikkakemod'] = ta.CDLHIKKAKEMOD(
            open, high, low, close)
        df['ta_pattern_cdlhomingpigeon'] = ta.CDLHOMINGPIGEON(
            open, high, low, close)
        df['ta_pattern_cdlidentical3crows'] = ta.CDLIDENTICAL3CROWS(
            open, high, low, close)
        df['ta_pattern_cdlinneck'] = ta.CDLINNECK(open, high, low, close)
        df['ta_pattern_cdlinvertedhammer'] = ta.CDLINVERTEDHAMMER(
            open, high, low, close)
        df['ta_pattern_cdlkicking'] = ta.CDLKICKING(open, high, low, close)
        df['ta_pattern_cdlkickingbylength'] = ta.CDLKICKINGBYLENGTH(
            open, high, low, close)
        df['ta_pattern_cdlladderbottom'] = ta.CDLLADDERBOTTOM(
            open, high, low, close)
        df['ta_pattern_cdllongleggeddoji'] = ta.CDLLONGLEGGEDDOJI(
            open, high, low, close)
        df['ta_pattern_cdllongline'] = ta.CDLLONGLINE(open, high, low, close)
        df['ta_pattern_cdlmarubozu'] = ta.CDLMARUBOZU(open, high, low, close)
        df['ta_pattern_cdlmatchinglow'] = ta.CDLMATCHINGLOW(
            open, high, low, close)
        df['ta_pattern_cdlmathold'] = ta.CDLMATHOLD(open,
                                                    high,
                                                    low,
                                                    close,
                                                    penetration=0)
        df['ta_pattern_cdlmorningdojistar'] = ta.CDLMORNINGDOJISTAR(
            open, high, low, close, penetration=0)
        df['ta_pattern_cdlmorningstar'] = ta.CDLMORNINGSTAR(open,
                                                            high,
                                                            low,
                                                            close,
                                                            penetration=0)
        df['ta_pattern_cdllonneck'] = ta.CDLONNECK(open, high, low, close)
        df['ta_pattern_cdlpiercing'] = ta.CDLPIERCING(open, high, low, close)
        df['ta_pattern_cdlrickshawman'] = ta.CDLRICKSHAWMAN(
            open, high, low, close)
        df['ta_pattern_cdlrisefall3methods'] = ta.CDLRISEFALL3METHODS(
            open, high, low, close)
        df['ta_pattern_cdlseparatinglines'] = ta.CDLSEPARATINGLINES(
            open, high, low, close)
        df['ta_pattern_cdlshootingstar'] = ta.CDLSHOOTINGSTAR(
            open, high, low, close)
        df['ta_pattern_cdlshortline'] = ta.CDLSHORTLINE(open, high, low, close)
        df['ta_pattern_cdlspinningtop'] = ta.CDLSPINNINGTOP(
            open, high, low, close)
        df['ta_pattern_cdlstalledpattern'] = ta.CDLSTALLEDPATTERN(
            open, high, low, close)
        df['ta_pattern_cdlsticksandwich'] = ta.CDLSTICKSANDWICH(
            open, high, low, close)
        df['ta_pattern_cdltakuri'] = ta.CDLTAKURI(open, high, low, close)
        df['ta_pattern_cdltasukigap'] = ta.CDLTASUKIGAP(open, high, low, close)
        df['ta_pattern_cdlthrusting'] = ta.CDLTHRUSTING(open, high, low, close)
        df['ta_pattern_cdltristar'] = ta.CDLTRISTAR(open, high, low, close)
        df['ta_pattern_cdlunique3river'] = ta.CDLUNIQUE3RIVER(
            open, high, low, close)
        df['ta_pattern_cdlupsidegap2crows'] = ta.CDLUPSIDEGAP2CROWS(
            open, high, low, close)
        df['ta_pattern_cdlxsidegap3methods'] = ta.CDLXSIDEGAP3METHODS(
            open, high, low, close)

    if ta_settings['statistic']:

        df['ta_statistic_beta'] = ta.BETA(high, low, timeperiod=5)
        df['ta_statistic_correl'] = ta.CORREL(high, low, timeperiod=30)
        df['ta_statistic_linearreg'] = ta.LINEARREG(close, timeperiod=14)
        df['ta_statistic_linearreg_angle'] = ta.LINEARREG_ANGLE(close,
                                                                timeperiod=14)
        df['ta_statistic_linearreg_intercept'] = ta.LINEARREG_INTERCEPT(
            close, timeperiod=14)
        df['ta_statistic_linearreg_slope'] = ta.LINEARREG_SLOPE(close,
                                                                timeperiod=14)
        df['ta_statistic_stddev'] = ta.STDDEV(close, timeperiod=5, nbdev=1)
        df['ta_statistic_tsf'] = ta.TSF(close, timeperiod=14)
        df['ta_statistic_var'] = ta.VAR(close, timeperiod=5, nbdev=1)

    if ta_settings['math_transforms']:

        df['ta_math_transforms_atan'] = ta.ATAN(close)
        df['ta_math_transforms_ceil'] = ta.CEIL(close)
        df['ta_math_transforms_cos'] = ta.COS(close)
        df['ta_math_transforms_floor'] = ta.FLOOR(close)
        df['ta_math_transforms_ln'] = ta.LN(close)
        df['ta_math_transforms_log10'] = ta.LOG10(close)
        df['ta_math_transforms_sin'] = ta.SIN(close)
        df['ta_math_transforms_sqrt'] = ta.SQRT(close)
        df['ta_math_transforms_tan'] = ta.TAN(close)

    if ta_settings['math_operators']:

        df['ta_math_operators_add'] = ta.ADD(high, low)
        df['ta_math_operators_div'] = ta.DIV(high, low)
        df['ta_math_operators_min'], df['ta_math_operators_max'] = ta.MINMAX(
            close, timeperiod=30)
        df['ta_math_operators_minidx'], df[
            'ta_math_operators_maxidx'] = ta.MINMAXINDEX(close, timeperiod=30)
        df['ta_math_operators_mult'] = ta.MULT(high, low)
        df['ta_math_operators_sub'] = ta.SUB(high, low)
        df['ta_math_operators_sum'] = ta.SUM(close, timeperiod=30)

    return df
예제 #21
0
def LINEARREG_ANGLE(Series, timeperiod=14):
    res = talib.LINEARREG_ANGLE(Series.values, timeperiod)
    return pd.Series(res, index=Series.index)
def LINEARREG_ANGLE(raw_df, timeperiod=14):
    # Linear Regression Angle
    # extract necessary data from raw dataframe (close)
    return ta.LINEARREG_ANGLE(raw_df.Close.values, timeperiod)
예제 #23
0
def get_talib_stock_daily(
        stock_code,
        s,
        e,
        append_ori_close=False,
        norms=['volume', 'amount', 'ht_dcphase', 'obv', 'adosc', 'ad', 'cci']):
    """获取经过talib处理后的股票日线数据"""
    stock_data = QA.QA_fetch_stock_day_adv(stock_code, s, e)
    stock_df = stock_data.to_qfq().data
    if append_ori_close:
        stock_df['o_close'] = stock_data.data['close']
    # stock_df['high_qfq'] = stock_data.to_qfq().data['high']
    # stock_df['low_hfq'] = stock_data.to_hfq().data['low']

    close = np.array(stock_df['close'])
    high = np.array(stock_df['high'])
    low = np.array(stock_df['low'])
    _open = np.array(stock_df['open'])
    _volume = np.array(stock_df['volume'])

    stock_df['dema'] = talib.DEMA(close)
    stock_df['ema'] = talib.EMA(close)
    stock_df['ht_tradeline'] = talib.HT_TRENDLINE(close)
    stock_df['kama'] = talib.KAMA(close)
    stock_df['ma'] = talib.MA(close)
    stock_df['mama'], stock_df['fama'] = talib.MAMA(close)
    # MAVP
    stock_df['midpoint'] = talib.MIDPOINT(close)
    stock_df['midprice'] = talib.MIDPRICE(high, low)
    stock_df['sar'] = talib.SAR(high, low)
    stock_df['sarext'] = talib.SAREXT(high, low)
    stock_df['sma'] = talib.SMA(close)
    stock_df['t3'] = talib.T3(close)
    stock_df['tema'] = talib.TEMA(close)
    stock_df['trima'] = talib.TRIMA(close)
    stock_df['wma'] = talib.WMA(close)

    stock_df['adx'] = talib.ADX(high, low, close)
    stock_df['adxr'] = talib.ADXR(high, low, close)
    stock_df['apo'] = talib.APO(close)

    stock_df['aroondown'], stock_df['aroonup'] = talib.AROON(high, low)
    stock_df['aroonosc'] = talib.AROONOSC(high, low)
    stock_df['bop'] = talib.BOP(_open, high, low, close)
    stock_df['cci'] = talib.CCI(high, low, close)
    stock_df['cmo'] = talib.CMO(close)
    stock_df['dx'] = talib.DX(high, low, close)
    # MACD
    stock_df['macd'], stock_df['macdsignal'], stock_df[
        'macdhist'] = talib.MACDEXT(close)
    # MACDFIX
    stock_df['mfi'] = talib.MFI(high, low, close, _volume)
    stock_df['minus_di'] = talib.MINUS_DI(high, low, close)
    stock_df['minus_dm'] = talib.MINUS_DM(high, low)
    stock_df['mom'] = talib.MOM(close)
    stock_df['plus_di'] = talib.PLUS_DI(high, low, close)
    stock_df['plus_dm'] = talib.PLUS_DM(high, low)
    stock_df['ppo'] = talib.PPO(close)
    stock_df['roc'] = talib.ROC(close)
    stock_df['rocp'] = talib.ROCP(close)
    stock_df['rocr'] = talib.ROCR(close)
    stock_df['rocr100'] = talib.ROCR100(close)
    stock_df['rsi'] = talib.RSI(close)
    stock_df['slowk'], stock_df['slowd'] = talib.STOCH(high, low, close)
    stock_df['fastk'], stock_df['fastd'] = talib.STOCHF(high, low, close)
    # STOCHRSI - Stochastic Relative Strength Index
    stock_df['trix'] = talib.TRIX(close)
    stock_df['ultosc'] = talib.ULTOSC(high, low, close)
    stock_df['willr'] = talib.WILLR(high, low, close)

    stock_df['ad'] = talib.AD(high, low, close, _volume)
    stock_df['adosc'] = talib.ADOSC(high, low, close, _volume)
    stock_df['obv'] = talib.OBV(close, _volume)

    stock_df['ht_dcperiod'] = talib.HT_DCPERIOD(close)
    stock_df['ht_dcphase'] = talib.HT_DCPHASE(close)
    stock_df['inphase'], stock_df['quadrature'] = talib.HT_PHASOR(close)
    stock_df['sine'], stock_df['leadsine'] = talib.HT_PHASOR(close)
    stock_df['ht_trendmode'] = talib.HT_TRENDMODE(close)

    stock_df['avgprice'] = talib.AVGPRICE(_open, high, low, close)
    stock_df['medprice'] = talib.MEDPRICE(high, low)
    stock_df['typprice'] = talib.TYPPRICE(high, low, close)
    stock_df['wclprice'] = talib.WCLPRICE(high, low, close)

    stock_df['atr'] = talib.ATR(high, low, close)
    stock_df['natr'] = talib.NATR(high, low, close)
    stock_df['trange'] = talib.TRANGE(high, low, close)

    stock_df['beta'] = talib.BETA(high, low)
    stock_df['correl'] = talib.CORREL(high, low)
    stock_df['linearreg'] = talib.LINEARREG(close)
    stock_df['linearreg_angle'] = talib.LINEARREG_ANGLE(close)
    stock_df['linearreg_intercept'] = talib.LINEARREG_INTERCEPT(close)
    stock_df['linearreg_slope'] = talib.LINEARREG_SLOPE(close)
    stock_df['stddev'] = talib.STDDEV(close)
    stock_df['tsf'] = talib.TSF(close)
    stock_df['var'] = talib.VAR(close)

    stock_df = stock_df.reset_index().set_index('date')

    if norms:
        x = stock_df[norms].values  # returns a numpy array
        x_scaled = MinMaxScaler().fit_transform(x)
        stock_df = stock_df.drop(columns=norms).join(
            pd.DataFrame(x_scaled, columns=norms, index=stock_df.index))

    # stock_df = stock_df.drop(columns=['code', 'open', 'high', 'low'])
    stock_df = stock_df.dropna()
    stock_df = stock_df.drop(columns=['code'])
    return stock_df
예제 #24
0
def __prepare_one_hold(df, _back_hours, _hold_hour, diff_d=[0.3, 0.5]):
    """ 为一个币种一个持币周期添加所有回溯周期的因子数据,并添加该持币周期的所有offset,返回一个 DataFrame
    :param _pkl_file: 整理好基础数据的一个币种的 pkl 文件路径
    :param _back_hours: 回溯周期列表 [3, 4, 6, 8, 12, 24, 48, 60, 72, 96]  关联因子周期
        skew偏度rolling最小周期为3才有数据 所以最小回溯周期设为3
    :param _hold_hour: 持币周期 '2H', '3H', '4H', '6H', '8H', '12H', '24H', '36H', '48H', '60H', '72H'
        上述周期中之一, 关联 添加 offset 标签列
        如果持币周为 2H offset 为 0, 1; 如果持币周为 3H offset 为 0, 1, 2;
    :return: """

    df['涨跌幅'] = df['close'].pct_change()  # 计算涨跌幅
    df['下个周期_avg_price'] = df['avg_price'].shift(-1)  # 计算下根K线开盘买入涨跌幅
    df.loc[df['volume'] == 0, '是否交易'] = 0  # 找出不交易的周期
    df['是否交易'].fillna(value=1, inplace=True)
    """ ******************** 以下是需要修改的代码 ******************** """
    # =====计算各项选币指标
    extra_agg_dict = dict()

    # =====技术指标
    # --- KDJ ---
    for n in _back_hours:
        # 正常K线数据 计算 KDJ
        low_list = df['low'].rolling(
            n, min_periods=1).min()  # 过去n(含当前行)行数据 最低价的最小值
        high_list = df['high'].rolling(
            n, min_periods=1).max()  # 过去n(含当前行)行数据 最高价的最大值
        rsv = (df['close'] - low_list) / (high_list -
                                          low_list) * 100  # 未成熟随机指标值
        df[f'K_bh_{n}'] = rsv.ewm(com=2).mean().shift(1)  # K
        extra_agg_dict[f'K_bh_{n}'] = 'first'
        df[f'D_bh_{n}'] = df[f'K_bh_{n}'].ewm(com=2).mean()  # D
        extra_agg_dict[f'D_bh_{n}'] = 'first'
        df[f'J_bh_{n}'] = 3 * df[f'K_bh_{n}'] - 2 * df[f'D_bh_{n}']  # J
        extra_agg_dict[f'J_bh_{n}'] = 'first'

        #  差分
        # 使用差分后的K线数据 计算 KDJ  --- 标准差变大,数据更不稳定,放弃
        # 用计算后的KDJ指标,再差分  --- 标准差变小,数据更稳定,采纳
        for _ind in ['K', 'D', 'J']:
            __add_diff(_df=df,
                       _d_list=diff_d,
                       _name=f'{_ind}_bh_{n}',
                       _agg_dict=extra_agg_dict,
                       _agg_type='first')

    # --- RSI ---  在期货市场很有效
    close_dif = df['close'].diff()
    df['up'] = np.where(close_dif > 0, close_dif, 0)
    df['down'] = np.where(close_dif < 0, abs(close_dif), 0)
    for n in _back_hours:
        a = df['up'].rolling(n).sum()
        b = df['down'].rolling(n).sum()
        df[f'RSI_bh_{n}'] = (a / (a + b)).shift(1)  # RSI
        extra_agg_dict[f'RSI_bh_{n}'] = 'first'

        # 差分
        # 用计算后的RSI指标,再差分  --- 标准差变小,数据更稳定,采纳
        __add_diff(_df=df,
                   _d_list=diff_d,
                   _name=f'RSI_bh_{n}',
                   _agg_dict=extra_agg_dict,
                   _agg_type='first')

    del df['up'], df['down']  # 删除过程数据

    # ===常见变量
    # --- 均价 ---  对应低价股策略(预计没什么用)
    # 策略改进思路:以下所有用到收盘价的因子,都可尝试使用均价代替
    for n in _back_hours:
        df[f'均价_bh_{n}'] = (df['quote_volume'].rolling(n).sum() /
                            df['volume'].rolling(n).sum()).shift(1)
        extra_agg_dict[f'均价_bh_{n}'] = 'first'

        # 差分
        __add_diff(_df=df,
                   _d_list=diff_d,
                   _name=f'均价_bh_{n}',
                   _agg_dict=extra_agg_dict,
                   _agg_type='first')

    # --- 涨跌幅 ---
    for n in _back_hours:
        df[f'涨跌幅_bh_{n}'] = df['close'].pct_change(n).shift(1)
        extra_agg_dict[f'涨跌幅_bh_{n}'] = 'first'

        # 差分
        __add_diff(_df=df,
                   _d_list=diff_d,
                   _name=f'涨跌幅_bh_{n}',
                   _agg_dict=extra_agg_dict,
                   _agg_type='first')

    # --- bias ---  涨跌幅更好的表达方式 bias 币价偏离均线的比例。
    for n in _back_hours:
        ma = df['close'].rolling(n, min_periods=1).mean()
        df[f'bias_bh_{n}'] = (df['close'] / ma - 1).shift(1)
        extra_agg_dict[f'bias_bh_{n}'] = 'first'

        # 差分
        __add_diff(_df=df,
                   _d_list=diff_d,
                   _name=f'bias_bh_{n}',
                   _agg_dict=extra_agg_dict,
                   _agg_type='first')

    # --- 振幅 ---  最高价最低价
    for n in _back_hours:
        high = df['high'].rolling(n, min_periods=1).max()
        low = df['low'].rolling(n, min_periods=1).min()
        df[f'振幅_bh_{n}'] = (high / low - 1).shift(1)
        extra_agg_dict[f'振幅_bh_{n}'] = 'first'

        # 差分
        __add_diff(_df=df,
                   _d_list=diff_d,
                   _name=f'振幅_bh_{n}',
                   _agg_dict=extra_agg_dict,
                   _agg_type='first')

    # --- 振幅2 ---  收盘价、开盘价
    high = df[['close', 'open']].max(axis=1)
    low = df[['close', 'open']].min(axis=1)
    for n in _back_hours:
        high = high.rolling(n, min_periods=1).max()
        low = low.rolling(n, min_periods=1).min()
        df[f'振幅2_bh_{n}'] = (high / low - 1).shift(1)
        extra_agg_dict[f'振幅2_bh_{n}'] = 'first'

        # 差分
        __add_diff(_df=df,
                   _d_list=diff_d,
                   _name=f'振幅2_bh_{n}',
                   _agg_dict=extra_agg_dict,
                   _agg_type='first')

    # --- 涨跌幅std ---  振幅的另外一种形式
    change = df['close'].pct_change()
    for n in _back_hours:
        df[f'涨跌幅std_bh_{n}'] = change.rolling(n).std().shift(1)
        extra_agg_dict[f'涨跌幅std_bh_{n}'] = 'first'

        # 差分
        __add_diff(_df=df,
                   _d_list=diff_d,
                   _name=f'涨跌幅std_bh_{n}',
                   _agg_dict=extra_agg_dict,
                   _agg_type='first')

    # --- 涨跌幅skew ---  在商品期货市场有效
    # skew偏度rolling最小周期为3才有数据
    for n in _back_hours:
        df[f'涨跌幅skew_bh_{n}'] = change.rolling(n).skew().shift(1)
        extra_agg_dict[f'涨跌幅skew_bh_{n}'] = 'first'

        # 差分
        __add_diff(_df=df,
                   _d_list=diff_d,
                   _name=f'涨跌幅skew_bh_{n}',
                   _agg_dict=extra_agg_dict,
                   _agg_type='first')

    # --- 成交额 ---  对应小市值概念
    for n in _back_hours:
        df[f'成交额_bh_{n}'] = df['quote_volume'].rolling(
            n, min_periods=1).sum().shift(1)
        extra_agg_dict[f'成交额_bh_{n}'] = 'first'

        # 差分
        __add_diff(_df=df,
                   _d_list=diff_d,
                   _name=f'成交额_bh_{n}',
                   _agg_dict=extra_agg_dict,
                   _agg_type='first')

    # --- 成交额std ---  191选股因子中最有效的因子
    for n in _back_hours:
        df[f'成交额std_bh_{n}'] = df['quote_volume'].rolling(
            n, min_periods=2).std().shift(1)
        extra_agg_dict[f'成交额std_bh_{n}'] = 'first'

        # 差分
        __add_diff(_df=df,
                   _d_list=diff_d,
                   _name=f'成交额std_bh_{n}',
                   _agg_dict=extra_agg_dict,
                   _agg_type='first')

    # --- 资金流入比例 --- 币安独有的数据
    for n in _back_hours:
        volume = df['quote_volume'].rolling(n, min_periods=1).sum()
        buy_volume = df['taker_buy_quote_asset_volume'].rolling(
            n, min_periods=1).sum()
        df[f'资金流入比例_bh_{n}'] = (buy_volume / volume).shift(1)
        extra_agg_dict[f'资金流入比例_bh_{n}'] = 'first'

        # 差分
        __add_diff(_df=df,
                   _d_list=diff_d,
                   _name=f'资金流入比例_bh_{n}',
                   _agg_dict=extra_agg_dict,
                   _agg_type='first')

    # --- 量比 ---
    for n in _back_hours:
        df[f'量比_bh_{n}'] = (
            df['quote_volume'] /
            df['quote_volume'].rolling(n, min_periods=1).mean()).shift(1)
        extra_agg_dict[f'量比_bh_{n}'] = 'first'

        # 差分
        __add_diff(_df=df,
                   _d_list=diff_d,
                   _name=f'量比_bh_{n}',
                   _agg_dict=extra_agg_dict,
                   _agg_type='first')

    # --- 成交笔数 ---
    for n in _back_hours:
        df[f'成交笔数_bh_{n}'] = df['trade_num'].rolling(
            n, min_periods=1).sum().shift(1)
        extra_agg_dict[f'成交笔数_bh_{n}'] = 'first'

        # 差分
        __add_diff(_df=df,
                   _d_list=diff_d,
                   _name=f'成交笔数_bh_{n}',
                   _agg_dict=extra_agg_dict,
                   _agg_type='first')

    # --- 量价相关系数 ---  量价相关选股策略
    for n in _back_hours:
        df[f'量价相关系数_bh_{n}'] = df['close'].rolling(n).corr(
            df['quote_volume']).shift(1)
        extra_agg_dict[f'量价相关系数_bh_{n}'] = 'first'

        # 差分
        __add_diff(_df=df,
                   _d_list=diff_d,
                   _name=f'量价相关系数_bh_{n}',
                   _agg_dict=extra_agg_dict,
                   _agg_type='first')

    # --- Angle ---
    for n in _back_hours:
        column_name = f'Angle_bh_{n}'
        ma = df['close'].rolling(window=n, min_periods=1).mean()
        df[column_name] = ta.LINEARREG_ANGLE(ma, n)
        df[column_name] = df[column_name].shift(1)
        extra_agg_dict[column_name] = 'first'

        # 差分
        __add_diff(_df=df,
                   _d_list=diff_d,
                   _name=column_name,
                   _agg_dict=extra_agg_dict,
                   _agg_type='first')

    # ---- GapTrue ----
    for n in _back_hours:
        ma = df['close'].rolling(window=n, min_periods=1).mean()
        wma = ta.WMA(df['close'], n)
        gap = wma - ma
        column_name = f'GapTrue_bh_{n}'
        df[column_name] = gap / abs(gap).rolling(window=n).sum()
        df[column_name] = df[column_name].shift(1)
        extra_agg_dict[column_name] = 'first'

        # 差分
        __add_diff(_df=df,
                   _d_list=diff_d,
                   _name=column_name,
                   _agg_dict=extra_agg_dict,
                   _agg_type='first')

    # ---- 癞子 ----
    for n in _back_hours:
        diff = df['close'] / df['close'].shift(1) - 1
        column_name = f'癞子_bh_{n}'
        df[column_name] = diff / abs(diff).rolling(window=n).sum()
        df[column_name] = df[column_name].shift(1)
        extra_agg_dict[column_name] = 'first'

        # 差分
        __add_diff(_df=df,
                   _d_list=diff_d,
                   _name=column_name,
                   _agg_dict=extra_agg_dict,
                   _agg_type='first')

    # ---- CCI ----
    for n in _back_hours:
        oma = ta.WMA(df.open, n)
        hma = ta.WMA(df.high, n)
        lma = ta.WMA(df.low, n)
        cma = ta.WMA(df.close, n)
        tp = (hma + lma + cma + oma) / 4
        ma = ta.WMA(tp, n)
        md = ta.WMA(abs(cma - ma), n)

        column_name = f'CCI_bh_{n}'
        df[column_name] = (tp - ma) / md
        df[column_name] = df[column_name].shift(1)
        extra_agg_dict[column_name] = 'first'

        # 差分
        __add_diff(_df=df,
                   _d_list=diff_d,
                   _name=column_name,
                   _agg_dict=extra_agg_dict,
                   _agg_type='first')
    """ ******************** 以上是需要修改的代码 ******************** """
    # ===将数据转化为需要的周期
    # 在数据最前面,增加一行数据,这是为了在对>24h的周期进行resample时,保持数据的一致性。
    df = df.loc[0:0, :].append(df, ignore_index=True)
    df.loc[0, 'candle_begin_time'] = dfr.convert_string_to_local_date(
        '2020-01-01 00:00:00').replace(tzinfo=pytz.utc)

    # 转换周期
    df['周期开始时间'] = df['candle_begin_time']
    df.set_index('candle_begin_time', inplace=True)

    # 必备字段
    agg_dict = {
        'symbol': 'first',
        '周期开始时间': 'first',
        'open': 'first',
        'avg_price': 'first',
        'close': 'last',
        '下个周期_avg_price': 'last',
        'volume': 'sum',
    }
    agg_dict = dict(agg_dict, **extra_agg_dict)  # 需要保留的列 必备字段 + 因子字段

    # 不同的offset,进行resample
    # 不管何种持币周期,将所有 offset 合并为一个 DataFrame后 一天中的数据行数都是相同的
    # 一天的数据行数为 当天的币种数*24,
    # 例如在2020-12-15有77个币在交易,不管持币周期是2H还是3H,合并后的DataFrame数据行数都是1848(24*77) 因为
    # 对于持币周期为2H来说,offset0 offset1 共计两批换仓点
    # offset0 换仓点在 0,2,4,6,8,10,12,14,16,18,20,22
    # offset1 换仓点在 1,3,5,7,9,11,13,15,17,19,21,23
    # 所以在一天中的每个整点都有所有交易对的换仓发生
    # 对于持币周期为3H来说,offset0 offset1 offset2 共计三批换仓点
    # offset0 换仓点在 0,3,6,9,12,15,18,21
    # offset1 换仓点在 1,4,7,10,13,16,19,22
    # offset2 换仓点在 2,5,8,11,14,17,20,23
    # 所以在一天中的每个整点都有所有交易对的换仓发生
    # 对于持币周期大于一天的来说,持币周期内相当于一天,例如48H,还是有48个offset 共计48批换仓点
    # 所以在固定长度日期范围内,所有持币周期都将有相同的数据行数
    # 所以不管何种持币周期,生成的合并 pkl 大小基本相同
    period_df_list = []
    for offset in range(int(_hold_hour[:-1])):
        # 转换持币周期的 df 其列为 必备字段 + 因子字段
        # 开始时间为 2010-01-01 00:00:00  在有交易数据的之前 除candle_begin_time列皆为 NaN

        # 在24h之内 base 和 offset 一样
        # period_df = df.resample(_hold_hour, offset=f'{offset}h').agg(agg_dict)
        period_df = df.resample(_hold_hour, base=offset).agg(agg_dict)

        # 上一行代码对数据转换完周期后,刚开始会有大量的空数据,没有必要对这些数据进行删除,因为后面会删除18年之前的数据。
        # 添加 offset 标签列
        # 如果持币周为 2 offset 为 0, 1; 如果持币周为 3 offset 为 0, 1, 2;
        period_df['offset'] = offset

        # 原版自适应布林通道
        n = 34
        period_df['close_shift'] = period_df['close'].shift(1)
        period_df['median'] = period_df['close_shift'].rolling(window=n).mean()
        period_df['std'] = period_df['close_shift'].rolling(
            n, min_periods=1).std(ddof=0)  # ddof代表标准差自由度
        period_df['z_score'] = abs(period_df['close_shift'] -
                                   period_df['median']) / period_df['std']
        period_df['up'] = period_df['z_score'].rolling(
            window=n, min_periods=1).max().shift(1)
        period_df['dn'] = period_df['z_score'].rolling(
            window=n, min_periods=1).min().shift(1)
        period_df[
            'upper'] = period_df['median'] + period_df['std'] * period_df['up']
        period_df[
            'lower'] = period_df['median'] - period_df['std'] * period_df['up']
        period_df['condition_long'] = period_df['close_shift'] >= period_df[
            'lower']  # 破下轨,不做多
        period_df['condition_short'] = period_df['close_shift'] <= period_df[
            'upper']  # 破上轨,不做空

        period_df.reset_index(inplace=True)

        # 合并数据
        period_df_list.append(period_df)

    # 将不同offset的数据,合并到一张表
    period_df = pd.concat(period_df_list, ignore_index=True)
    period_df.reset_index(inplace=True)

    # 删除一些数据
    period_df = period_df.iloc[24:]  # 刚开始交易前24个周期删除
    period_df = period_df[period_df['candle_begin_time'] >= pd.to_datetime(
        '2020-09-01').tz_localize(pytz.utc)]
    period_df.reset_index(drop=True, inplace=True)
    del period_df['index']
    return period_df
예제 #25
0
def _extract_feature(candle, params, candle_type, target_dt):
    '''
    前に余分に必要なデータ量: {(stockf_fastk_period_l + stockf_fastk_period_l) * 最大分足 (min)} + window_size
    = (12 + 12) * 5 + 5 = 125 (min)
    '''
    o = candle.open
    h = candle.high
    l = candle.low
    c = candle.close
    v = candle.volume

    # OHLCV
    features = pd.DataFrame()
    features['open'] = o
    features['high'] = h
    features['low'] = l
    features['close'] = c
    features['volume'] = v

    ####################################
    #
    # Momentum Indicator Functions
    #
    ####################################

    # ADX = SUM((+DI - (-DI)) / (+DI + (-DI)), N) / N
    # N — 計算期間
    # SUM (..., N) — N期間の合計
    # +DI — プラスの価格変動の値(positive directional index)
    # -DI — マイナスの価格変動の値(negative directional index)
    # rsi_timeperiod_l=30の場合、30分足で、(30 * 30 / 60(min)) = 15時間必要

    features['adx_s'] = ta.ADX(h, l, c, timeperiod=params['adx_timeperiod_s'])
    features['adx_m'] = ta.ADX(h, l, c, timeperiod=params['adx_timeperiod_m'])
    features['adx_l'] = ta.ADX(h, l, c, timeperiod=params['adx_timeperiod_l'])

    features['adxr_s'] = ta.ADXR(h, l, c, timeperiod=params['adxr_timeperiod_s'])
    features['adxr_m'] = ta.ADXR(h, l, c, timeperiod=params['adxr_timeperiod_m'])
    features['adxr_l'] = ta.ADXR(h, l, c, timeperiod=params['adxr_timeperiod_l'])

    # APO = Shorter Period EMA – Longer Period EMA
    features['apo_s'] = ta.APO(c, fastperiod=params['apo_fastperiod_s'], slowperiod=params['apo_slowperiod_s'], matype=ta.MA_Type.EMA)
    features['apo_m'] = ta.APO(c, fastperiod=params['apo_fastperiod_m'], slowperiod=params['apo_slowperiod_m'], matype=ta.MA_Type.EMA)

    # AroonUp = (N - 過去N日間の最高値からの経過期間) ÷ N × 100
    # AroonDown = (N - 過去N日間の最安値からの経過期間) ÷ N × 100
    # aroon_timeperiod_l=30の場合、30分足で、(30 * 30 / 60(min)) = 15時間必要
    #features['aroondown_s'], features['aroonup_s'] = ta.AROON(h, l, timeperiod=params['aroon_timeperiod_s'])
    #features['aroondown_m'], features['aroonup_m'] = ta.AROON(h, l, timeperiod=params['aroon_timeperiod_m'])
    #features['aroondown_l'], features['aroonup_l'] = ta.AROON(h, l, timeperiod=params['aroon_timeperiod_l'])

    # Aronnオシレーター = AroonUp - AroonDown
    # aroonosc_timeperiod_l=30の場合、30分足で、(30 * 30 / 60(min)) = 15時間必要
    features['aroonosc_s'] = ta.AROONOSC(h, l, timeperiod=params['aroonosc_timeperiod_s'])
    features['aroonosc_m'] = ta.AROONOSC(h, l, timeperiod=params['aroonosc_timeperiod_m'])
    features['aroonosc_l'] = ta.AROONOSC(h, l, timeperiod=params['aroonosc_timeperiod_l'])

    # BOP = (close - open) / (high - low)
    features['bop'] = ta.BOP(o, h, l, c)

    # CCI = (TP - MA) / (0.015 * MD)
    # TP: (高値+安値+終値) / 3
    # MA: TPの移動平均
    # MD: 平均偏差 = ((MA - TP1) + (MA - TP2) + ...) / N
    features['cci_s'] = ta.CCI(h, l, c, timeperiod=params['cci_timeperiod_s'])
    features['cci_m'] = ta.CCI(h, l, c, timeperiod=params['cci_timeperiod_m'])
    features['cci_l'] = ta.CCI(h, l, c, timeperiod=params['cci_timeperiod_l'])

    # CMO - Chande Momentum Oscillator
    #features['cmo_s'] = ta.CMO(c, timeperiod=params['cmo_timeperiod_s'])
    #features['cmo_m'] = ta.CMO(c, timeperiod=params['cmo_timeperiod_m'])
    #features['cmo_l'] = ta.CMO(c, timeperiod=params['cmo_timeperiod_l'])

    # DX - Directional Movement Index
    features['dx_s'] = ta.DX(h, l, c, timeperiod=params['dx_timeperiod_s'])
    features['dx_m'] = ta.DX(h, l, c, timeperiod=params['dx_timeperiod_m'])
    features['dx_l'] = ta.DX(h, l, c, timeperiod=params['dx_timeperiod_l'])

    # MACD=基準線-相対線
    # 基準線(EMA):過去12日(週・月)間の終値指数平滑平均
    # 相対線(EMA):過去26日(週・月)間の終値指数平滑平均
    # https://www.sevendata.co.jp/shihyou/technical/macd.html
    # macd_slowperiod_m = 30 の場合30分足で((30 + macd_signalperiod_m) * 30)/ 60 = 16.5時間必要(macd_signalperiod_m=3の時)
    macd, macdsignal, macdhist = ta.MACDEXT(c, fastperiod=params['macd_fastperiod_s'],
                                            slowperiod=params['macd_slowperiod_s'],
                                            signalperiod=params['macd_signalperiod_s'],
                                            fastmatype=ta.MA_Type.EMA, slowmatype=ta.MA_Type.EMA,
                                            signalmatype=ta.MA_Type.EMA)
    change_macd = calc_change(macd, macdsignal)
    change_macd.index = macd.index
    features['macd_s'] = macd
    features['macdsignal_s'] = macdsignal
    features['macdhist_s'] = macdhist
    features['change_macd_s'] = change_macd
    macd, macdsignal, macdhist = ta.MACDEXT(c, fastperiod=params['macd_fastperiod_m'],
                                            slowperiod=params['macd_slowperiod_m'],
                                            signalperiod=params['macd_signalperiod_m'],
                                            fastmatype=ta.MA_Type.EMA, slowmatype=ta.MA_Type.EMA,
                                            signalmatype=ta.MA_Type.EMA)
    change_macd = calc_change(macd, macdsignal)
    change_macd.index = macd.index
    features['macd_m'] = macd
    features['macdsignal_m'] = macdsignal
    features['macdhist_m'] = macdhist
    features['change_macd_m'] = change_macd

    # MFI - Money Flow Index
    features['mfi_s'] = ta.MFI(h, l, c, v, timeperiod=params['mfi_timeperiod_s'])
    features['mfi_m'] = ta.MFI(h, l, c, v, timeperiod=params['mfi_timeperiod_m'])
    features['mfi_l'] = ta.MFI(h, l, c, v, timeperiod=params['mfi_timeperiod_l'])

    # MINUS_DI - Minus Directional Indicator
    features['minus_di_s'] = ta.MINUS_DI(h, l, c, timeperiod=params['minus_di_timeperiod_s'])
    features['minus_di_m'] = ta.MINUS_DI(h, l, c, timeperiod=params['minus_di_timeperiod_m'])
    features['minus_di_l'] = ta.MINUS_DI(h, l, c, timeperiod=params['minus_di_timeperiod_l'])

    # MINUS_DM - Minus Directional Movement
    features['minus_dm_s'] = ta.MINUS_DM(h, l, timeperiod=params['minus_dm_timeperiod_s'])
    features['minus_dm_m'] = ta.MINUS_DM(h, l, timeperiod=params['minus_dm_timeperiod_m'])
    features['minus_dm_l'] = ta.MINUS_DM(h, l, timeperiod=params['minus_dm_timeperiod_l'])

    # MOM - Momentum
    features['mom_s'] = ta.MOM(c, timeperiod=params['mom_timeperiod_s'])
    features['mom_m'] = ta.MOM(c, timeperiod=params['mom_timeperiod_m'])
    features['mom_l'] = ta.MOM(c, timeperiod=params['mom_timeperiod_l'])

    # PLUS_DI - Minus Directional Indicator
    features['plus_di_s'] = ta.PLUS_DI(h, l, c, timeperiod=params['plus_di_timeperiod_s'])
    features['plus_di_m'] = ta.PLUS_DI(h, l, c, timeperiod=params['plus_di_timeperiod_m'])
    features['plus_di_l'] = ta.PLUS_DI(h, l, c, timeperiod=params['plus_di_timeperiod_l'])

    # PLUS_DM - Minus Directional Movement
    features['plus_dm_s'] = ta.PLUS_DM(h, l, timeperiod=params['plus_dm_timeperiod_s'])
    features['plus_dm_m'] = ta.PLUS_DM(h, l, timeperiod=params['plus_dm_timeperiod_m'])
    features['plus_dm_l'] = ta.PLUS_DM(h, l, timeperiod=params['plus_dm_timeperiod_l'])

    # PPO - Percentage Price Oscillator
    #features['ppo_s'] = ta.PPO(c, fastperiod=params['ppo_fastperiod_s'], slowperiod=params['ppo_slowperiod_s'], matype=ta.MA_Type.EMA)
    #features['ppo_m'] = ta.PPO(c, fastperiod=params['ppo_fastperiod_m'], slowperiod=params['ppo_slowperiod_m'], matype=ta.MA_Type.EMA)

    # ROC - Rate of change : ((price/prevPrice)-1)*100
    features['roc_s'] = ta.ROC(c, timeperiod=params['roc_timeperiod_s'])
    features['roc_m'] = ta.ROC(c, timeperiod=params['roc_timeperiod_m'])
    features['roc_l'] = ta.ROC(c, timeperiod=params['roc_timeperiod_l'])

    # ROCP = (price-prevPrice) / prevPrice
    # http://www.tadoc.org/indicator/ROCP.htm
    # rocp_timeperiod_l = 30 の場合、30分足で(30 * 30) / 60 = 15時間必要
    rocp = ta.ROCP(c, timeperiod=params['rocp_timeperiod_s'])
    change_rocp = calc_change(rocp, pd.Series(np.zeros(len(candle)), index=candle.index))
    change_rocp.index = rocp.index
    features['rocp_s'] = rocp
    features['change_rocp_s'] = change_rocp
    rocp = ta.ROCP(c, timeperiod=params['rocp_timeperiod_m'])
    change_rocp = calc_change(rocp, pd.Series(np.zeros(len(candle)), index=candle.index))
    change_rocp.index = rocp.index
    features['rocp_m'] = rocp
    features['change_rocp_m'] = change_rocp
    rocp = ta.ROCP(c, timeperiod=params['rocp_timeperiod_l'])
    change_rocp = calc_change(rocp, pd.Series(np.zeros(len(candle)), index=candle.index))
    change_rocp.index = rocp.index
    features['rocp_l'] = rocp
    features['change_rocp_l'] = change_rocp

    # ROCR - Rate of change ratio: (price/prevPrice)
    features['rocr_s'] = ta.ROCR(c, timeperiod=params['rocr_timeperiod_s'])
    features['rocr_m'] = ta.ROCR(c, timeperiod=params['rocr_timeperiod_m'])
    features['rocr_l'] = ta.ROCR(c, timeperiod=params['rocr_timeperiod_l'])

    # ROCR100 - Rate of change ratio 100 scale: (price/prevPrice)*100
    features['rocr100_s'] = ta.ROCR100(c, timeperiod=params['rocr100_timeperiod_s'])
    features['rocr100_m'] = ta.ROCR100(c, timeperiod=params['rocr100_timeperiod_m'])
    features['rocr100_l'] = ta.ROCR100(c, timeperiod=params['rocr100_timeperiod_l'])

    # RSI = (100 * a) / (a + b) (a: x日間の値上がり幅の合計, b: x日間の値下がり幅の合計)
    # https://www.sevendata.co.jp/shihyou/technical/rsi.html
    # rsi_timeperiod_l=30の場合、30分足で、(30 * 30 / 60(min)) = 15時間必要
    #features['rsi_s'] = ta.RSI(c, timeperiod=params['rsi_timeperiod_s'])
    #features['rsi_m'] = ta.RSI(c, timeperiod=params['rsi_timeperiod_m'])
    #features['rsi_l'] = ta.RSI(c, timeperiod=params['rsi_timeperiod_l'])


    # FASTK(KPeriod) = 100 * (Today's Close - LowestLow) / (HighestHigh - LowestLow)
    # FASTD(FastDperiod) = MA Smoothed FASTK over FastDperiod
    # http://www.tadoc.org/indicator/STOCHF.htm
    # stockf_fastk_period_l=30の場合30分足で、(((30 + 30) * 30) / 60(min)) = 30時間必要 (LowestLowが移動平均の30分余分に必要なので60period余分に計算する)
    fastk, fastd = ta.STOCHF(h, l, c, fastk_period=params['stockf_fastk_period_s'], fastd_period=params['stockf_fastd_period_s'], fastd_matype=ta.MA_Type.EMA)
    change_stockf = calc_change(fastk, fastd)
    change_stockf.index = fastk.index
    features['fastk_s'] = fastk
    features['fastd_s'] = fastd
    features['fast_change_s'] = change_stockf
    fastk, fastd = ta.STOCHF(h, l, c, fastk_period=params['stockf_fastk_period_m'], fastd_period=params['stockf_fastd_period_m'], fastd_matype=ta.MA_Type.EMA)
    change_stockf = calc_change(fastk, fastd)
    change_stockf.index = fastk.index
    features['fastk_m'] = fastk
    features['fastd_m'] = fastd
    features['fast_change_m'] = change_stockf
    fastk, fastd = ta.STOCHF(h, l, c, fastk_period=params['stockf_fastk_period_l'], fastd_period=params['stockf_fastk_period_l'], fastd_matype=ta.MA_Type.EMA)
    change_stockf = calc_change(fastk, fastd)
    change_stockf.index = fastk.index
    features['fastk_l'] = fastk
    features['fastd_l'] = fastd
    features['fast_change_l'] = change_stockf

    # TRIX - 1-day Rate-Of-Change (ROC) of a Triple Smooth EMA
    features['trix_s'] = ta.TRIX(c, timeperiod=params['trix_timeperiod_s'])
    features['trix_m'] = ta.TRIX(c, timeperiod=params['trix_timeperiod_m'])
    features['trix_l'] = ta.TRIX(c, timeperiod=params['trix_timeperiod_l'])

    # ULTOSC - Ultimate Oscillator
    features['ultosc_s'] = ta.ULTOSC(h, l, c, timeperiod1=params['ultosc_timeperiod_s1'], timeperiod2=params['ultosc_timeperiod_s2'], timeperiod3=params['ultosc_timeperiod_s3'])

    # WILLR = (当日終値 - N日間の最高値) / (N日間の最高値 - N日間の最安値)× 100
    # https://inet-sec.co.jp/study/technical-manual/williamsr/
    # willr_timeperiod_l=30の場合30分足で、(30 * 30 / 60) = 15時間必要
    features['willr_s'] = ta.WILLR(h, l, c, timeperiod=params['willr_timeperiod_s'])
    features['willr_m'] = ta.WILLR(h, l, c, timeperiod=params['willr_timeperiod_m'])
    features['willr_l'] = ta.WILLR(h, l, c, timeperiod=params['willr_timeperiod_l'])

    ####################################
    #
    # Volume Indicator Functions
    #
    ####################################

    # Volume Indicator Functions
    # slowperiod_adosc_s = 10の場合、30分足で(10 * 30) / 60 = 5時間必要
    features['ad'] = ta.AD(h, l, c, v)
    features['adosc_s'] = ta.ADOSC(h, l, c, v, fastperiod=params['fastperiod_adosc_s'], slowperiod=params['slowperiod_adosc_s'])
    features['obv'] = ta.OBV(c, v)

    ####################################
    #
    # Volatility Indicator Functions
    #
    ####################################

    # ATR - Average True Range
    features['atr_s'] = ta.ATR(h, l, c, timeperiod=params['atr_timeperiod_s'])
    features['atr_m'] = ta.ATR(h, l, c, timeperiod=params['atr_timeperiod_m'])
    features['atr_l'] = ta.ATR(h, l, c, timeperiod=params['atr_timeperiod_l'])

    # NATR - Normalized Average True Range
    #features['natr_s'] = ta.NATR(h, l, c, timeperiod=params['natr_timeperiod_s'])
    #features['natr_m'] = ta.NATR(h, l, c, timeperiod=params['natr_timeperiod_m'])
    #features['natr_l'] = ta.NATR(h, l, c, timeperiod=params['natr_timeperiod_l'])

    # TRANGE - True Range
    features['trange'] = ta.TRANGE(h, l, c)

    ####################################
    #
    # Price Transform Functions
    #
    ####################################

    features['avgprice'] = ta.AVGPRICE(o, h, l, c)
    features['medprice'] = ta.MEDPRICE(h, l)
    #features['typprice'] = ta.TYPPRICE(h, l, c)
    #features['wclprice'] = ta.WCLPRICE(h, l, c)

    ####################################
    #
    # Cycle Indicator Functions
    #
    ####################################

    #features['ht_dcperiod'] = ta.HT_DCPERIOD(c)
    #features['ht_dcphase'] = ta.HT_DCPHASE(c)
    #features['inphase'], features['quadrature'] = ta.HT_PHASOR(c)
    #features['sine'], features['leadsine'] = ta.HT_SINE(c)
    features['integer'] = ta.HT_TRENDMODE(c)

    ####################################
    #
    # Statistic Functions
    #
    ####################################

    # BETA - Beta

    features['beta_s'] = ta.BETA(h, l, timeperiod=params['beta_timeperiod_s'])
    features['beta_m'] = ta.BETA(h, l, timeperiod=params['beta_timeperiod_m'])
    features['beta_l'] = ta.BETA(h, l, timeperiod=params['beta_timeperiod_l'])

    # CORREL - Pearson's Correlation Coefficient (r)
    #features['correl_s'] = ta.CORREL(h, l, timeperiod=params['correl_timeperiod_s'])
    #features['correl_m'] = ta.CORREL(h, l, timeperiod=params['correl_timeperiod_m'])
    #features['correl_l'] = ta.CORREL(h, l, timeperiod=params['correl_timeperiod_l'])

    # LINEARREG - Linear Regression
    #features['linearreg_s'] = ta.LINEARREG(c, timeperiod=params['linearreg_timeperiod_s'])
    #features['linearreg_m'] = ta.LINEARREG(c, timeperiod=params['linearreg_timeperiod_m'])
    #features['linearreg_l'] = ta.LINEARREG(c, timeperiod=params['linearreg_timeperiod_l'])

    # LINEARREG_ANGLE - Linear Regression Angle
    features['linearreg_angle_s'] = ta.LINEARREG_ANGLE(c, timeperiod=params['linearreg_angle_timeperiod_s'])
    features['linearreg_angle_m'] = ta.LINEARREG_ANGLE(c, timeperiod=params['linearreg_angle_timeperiod_m'])
    features['linearreg_angle_l'] = ta.LINEARREG_ANGLE(c, timeperiod=params['linearreg_angle_timeperiod_l'])

    # LINEARREG_INTERCEPT - Linear Regression Intercept
    features['linearreg_intercept_s'] = ta.LINEARREG_INTERCEPT(c, timeperiod=params['linearreg_intercept_timeperiod_s'])
    features['linearreg_intercept_m'] = ta.LINEARREG_INTERCEPT(c, timeperiod=params['linearreg_intercept_timeperiod_m'])
    features['linearreg_intercept_l'] = ta.LINEARREG_INTERCEPT(c, timeperiod=params['linearreg_intercept_timeperiod_l'])

    # LINEARREG_SLOPE - Linear Regression Slope
    features['linearreg_slope_s'] = ta.LINEARREG_SLOPE(c, timeperiod=params['linearreg_slope_timeperiod_s'])
    features['linearreg_slope_m'] = ta.LINEARREG_SLOPE(c, timeperiod=params['linearreg_slope_timeperiod_m'])
    features['linearreg_slope_l'] = ta.LINEARREG_SLOPE(c, timeperiod=params['linearreg_slope_timeperiod_l'])

    # STDDEV - Standard Deviation
    features['stddev_s'] = ta.STDDEV(c, timeperiod=params['stddev_timeperiod_s'], nbdev=1)
    features['stddev_m'] = ta.STDDEV(c, timeperiod=params['stddev_timeperiod_m'], nbdev=1)
    features['stddev_l'] = ta.STDDEV(c, timeperiod=params['stddev_timeperiod_l'], nbdev=1)

    # TSF - Time Series Forecast
    features['tsf_s'] = ta.TSF(c, timeperiod=params['tsf_timeperiod_s'])
    features['tsf_m'] = ta.TSF(c, timeperiod=params['tsf_timeperiod_m'])
    features['tsf_l'] = ta.TSF(c, timeperiod=params['tsf_timeperiod_l'])

    # VAR - Variance
    #features['var_s'] = ta.VAR(c, timeperiod=params['var_timeperiod_s'], nbdev=1)
    #features['var_m'] = ta.VAR(c, timeperiod=params['var_timeperiod_m'], nbdev=1)
    #features['var_l'] = ta.VAR(c, timeperiod=params['var_timeperiod_l'], nbdev=1)

    # ボリンジャーバンド
    # bbands_timeperiod_l = 30の場合、30分足で(30 * 30) / 60 = 15時間必要
    bb_upper, bb_middle, bb_lower = ta.BBANDS(c, timeperiod=params['bbands_timeperiod_s'],
                                              nbdevup=params['bbands_nbdevup_s'], nbdevdn=params['bbands_nbdevdn_s'],
                                              matype=ta.MA_Type.EMA)
    bb_trend1 = pd.Series(np.zeros(len(candle)), index=candle.index)
    bb_trend1[c > bb_upper] = 1
    bb_trend1[c < bb_lower] = -1
    bb_trend2 = pd.Series(np.zeros(len(candle)), index=candle.index)
    bb_trend2[c > bb_middle] = 1
    bb_trend2[c < bb_middle] = -1
    features['bb_upper_s'] = bb_upper
    features['bb_middle_s'] = bb_middle
    features['bb_lower_s'] = bb_lower
    features['bb_trend1_s'] = bb_trend1
    features['bb_trend2_s'] = bb_trend2
    bb_upper, bb_middle, bb_lower = ta.BBANDS(c, timeperiod=params['bbands_timeperiod_m'],
                                              nbdevup=params['bbands_nbdevup_m'], nbdevdn=params['bbands_nbdevdn_m'],
                                              matype=ta.MA_Type.EMA)
    bb_trend1 = pd.Series(np.zeros(len(candle)), index=candle.index)
    bb_trend1[c > bb_upper] = 1
    bb_trend1[c < bb_lower] = -1
    bb_trend2 = pd.Series(np.zeros(len(candle)), index=candle.index)
    bb_trend2[c > bb_middle] = 1
    bb_trend2[c < bb_middle] = -1
    features['bb_upper_m'] = bb_upper
    features['bb_middle_m'] = bb_middle
    features['bb_lower_m'] = bb_lower
    features['bb_trend1_m'] = bb_trend1
    features['bb_trend2_m'] = bb_trend2
    bb_upper, bb_middle, bb_lower = ta.BBANDS(c, timeperiod=params['bbands_timeperiod_l'],
                                              nbdevup=params['bbands_nbdevup_l'], nbdevdn=params['bbands_nbdevdn_l'],
                                              matype=ta.MA_Type.EMA)
    bb_trend1 = pd.Series(np.zeros(len(candle)), index=candle.index)
    bb_trend1[c > bb_upper] = 1
    bb_trend1[c < bb_lower] = -1
    bb_trend2 = pd.Series(np.zeros(len(candle)), index=candle.index)
    bb_trend2[c > bb_middle] = 1
    bb_trend2[c < bb_middle] = -1
    features['bb_upper_l'] = bb_upper
    features['bb_middle_l'] = bb_middle
    features['bb_lower_l'] = bb_lower
    features['bb_trend1_l'] = bb_trend1
    features['bb_trend2_l'] = bb_trend2

    # ローソク足
    features['CDL2CROWS'] = ta.CDL2CROWS(o, h, l, c)
    features['CDL3BLACKCROWS'] = ta.CDL3BLACKCROWS(o, h, l, c)
    features['CDL3INSIDE'] = ta.CDL3INSIDE(o, h, l, c)
    features['CDL3LINESTRIKE'] = ta.CDL3LINESTRIKE(o, h, l, c)
    features['CDL3OUTSIDE'] = ta.CDL3OUTSIDE(o, h, l, c)
    features['CDL3STARSINSOUTH'] = ta.CDL3STARSINSOUTH(o, h, l, c)
    features['CDL3WHITESOLDIERS'] = ta.CDL3WHITESOLDIERS(o, h, l, c)
    features['CDLABANDONEDBABY'] = ta.CDLABANDONEDBABY(o, h, l, c, penetration=0)
    features['CDLADVANCEBLOCK'] = ta.CDLADVANCEBLOCK(o, h, l, c)
    features['CDLBELTHOLD'] = ta.CDLBELTHOLD(o, h, l, c)
    features['CDLBREAKAWAY'] = ta.CDLBREAKAWAY(o, h, l, c)
    features['CDLCLOSINGMARUBOZU'] = ta.CDLCLOSINGMARUBOZU(o, h, l, c)
    features['CDLCONCEALBABYSWALL'] = ta.CDLCONCEALBABYSWALL(o, h, l, c)
    features['CDLCOUNTERATTACK'] = ta.CDLCOUNTERATTACK(o, h, l, c)
    features['CDLDARKCLOUDCOVER'] = ta.CDLDARKCLOUDCOVER(o, h, l, c, penetration=0)
    #features['CDLDOJI'] = ta.CDLDOJI(o, h, l, c)
    features['CDLDOJISTAR'] = ta.CDLDOJISTAR(o, h, l, c)
    features['CDLDRAGONFLYDOJI'] = ta.CDLDRAGONFLYDOJI(o, h, l, c)
    features['CDLENGULFING'] = ta.CDLENGULFING(o, h, l, c)
    features['CDLEVENINGDOJISTAR'] = ta.CDLEVENINGDOJISTAR(o, h, l, c, penetration=0)
    features['CDLEVENINGSTAR'] = ta.CDLEVENINGSTAR(o, h, l, c, penetration=0)
    #features['CDLGAPSIDESIDEWHITE'] = ta.CDLGAPSIDESIDEWHITE(o, h, l, c)
    features['CDLGRAVESTONEDOJI'] = ta.CDLGRAVESTONEDOJI(o, h, l, c)
    features['CDLHAMMER'] = ta.CDLHAMMER(o, h, l, c)
    #features['CDLHANGINGMAN'] = ta.CDLHANGINGMAN(o, h, l, c)
    features['CDLHARAMI'] = ta.CDLHARAMI(o, h, l, c)
    features['CDLHARAMICROSS'] = ta.CDLHARAMICROSS(o, h, l, c)
    features['CDLHIGHWAVE'] = ta.CDLHIGHWAVE(o, h, l, c)
    #features['CDLHIKKAKE'] = ta.CDLHIKKAKE(o, h, l, c)
    features['CDLHIKKAKEMOD'] = ta.CDLHIKKAKEMOD(o, h, l, c)
    features['CDLHOMINGPIGEON'] = ta.CDLHOMINGPIGEON(o, h, l, c)
    #features['CDLIDENTICAL3CROWS'] = ta.CDLIDENTICAL3CROWS(o, h, l, c)
    features['CDLINNECK'] = ta.CDLINNECK(o, h, l, c)
    #features['CDLINVERTEDHAMMER'] = ta.CDLINVERTEDHAMMER(o, h, l, c)
    features['CDLKICKING'] = ta.CDLKICKING(o, h, l, c)
    features['CDLKICKINGBYLENGTH'] = ta.CDLKICKINGBYLENGTH(o, h, l, c)
    features['CDLLADDERBOTTOM'] = ta.CDLLADDERBOTTOM(o, h, l, c)
    #features['CDLLONGLEGGEDDOJI'] = ta.CDLLONGLEGGEDDOJI(o, h, l, c)
    features['CDLMARUBOZU'] = ta.CDLMARUBOZU(o, h, l, c)
    #features['CDLMATCHINGLOW'] = ta.CDLMATCHINGLOW(o, h, l, c)
    features['CDLMATHOLD'] = ta.CDLMATHOLD(o, h, l, c, penetration=0)
    features['CDLMORNINGDOJISTAR'] = ta.CDLMORNINGDOJISTAR(o, h, l, c, penetration=0)
    features['CDLMORNINGSTAR'] = ta.CDLMORNINGSTAR(o, h, l, c, penetration=0)
    features['CDLONNECK'] = ta.CDLONNECK(o, h, l, c)
    features['CDLPIERCING'] = ta.CDLPIERCING(o, h, l, c)
    features['CDLRICKSHAWMAN'] = ta.CDLRICKSHAWMAN(o, h, l, c)
    features['CDLRISEFALL3METHODS'] = ta.CDLRISEFALL3METHODS(o, h, l, c)
    features['CDLSEPARATINGLINES'] = ta.CDLSEPARATINGLINES(o, h, l, c)
    #features['CDLSHOOTINGSTAR'] = ta.CDLSHOOTINGSTAR(o, h, l, c)
    features['CDLSHORTLINE'] = ta.CDLSHORTLINE(o, h, l, c)
    #features['CDLSPINNINGTOP'] = ta.CDLSPINNINGTOP(o, h, l, c)
    features['CDLSTALLEDPATTERN'] = ta.CDLSTALLEDPATTERN(o, h, l, c)
    features['CDLSTICKSANDWICH'] = ta.CDLSTICKSANDWICH(o, h, l, c)
    features['CDLTAKURI'] = ta.CDLTAKURI(o, h, l, c)
    features['CDLTASUKIGAP'] = ta.CDLTASUKIGAP(o, h, l, c)
    features['CDLTHRUSTING'] = ta.CDLTHRUSTING(o, h, l, c)
    features['CDLTRISTAR'] = ta.CDLTRISTAR(o, h, l, c)
    features['CDLUNIQUE3RIVER'] = ta.CDLUNIQUE3RIVER(o, h, l, c)
    features['CDLUPSIDEGAP2CROWS'] = ta.CDLUPSIDEGAP2CROWS(o, h, l, c)
    features['CDLXSIDEGAP3METHODS'] = ta.CDLXSIDEGAP3METHODS(o, h, l, c)

    '''
    # トレンドライン
    for dt in datetimerange(candle.index[0], candle.index[-1] + timedelta(minutes=1)):
        start_dt = (dt - timedelta(minutes=130)).strftime('%Y-%m-%d %H:%M:%S')
        end_dt = dt.strftime('%Y-%m-%d %H:%M:%S')
        tmp = candle.loc[(start_dt <= candle.index) & (candle.index <= end_dt)]
        for w_size, stride in [(15, 5), (30, 10), (60, 10), (120, 10)]:
        # for w_size, stride in [(120, 10)]:
            trendlines = calc_trendlines(tmp, w_size, stride)
            if len(trendlines) == 0:
                continue
            trendline_feature = calc_trendline_feature(tmp, trendlines)

            print('{}-{} {} {} {}'.format(dt - timedelta(minutes=130), dt, trendline_feature['high_a'], trendline_feature['high_b'], trendline_feature['high_diff']))

            features.loc[features.index == end_dt, 'trendline_high_a_{}'.format(w_size)] = trendline_feature['high_a']
            features.loc[features.index == end_dt, 'trendline_high_b_{}'.format(w_size)] = trendline_feature['high_b']
            features.loc[features.index == end_dt, 'trendline_high_diff_{}'.format(w_size)] = trendline_feature['high_diff']
            features.loc[features.index == end_dt, 'trendline_low_a_{}'.format(w_size)] = trendline_feature['low_a']
            features.loc[features.index == end_dt, 'trendline_low_b_{}'.format(w_size)] = trendline_feature['low_b']
            features.loc[features.index == end_dt, 'trendline_low_diff_{}'.format(w_size)] = trendline_feature['low_diff']
    '''

    window = 5
    features_ext = features
    for w in range(window):
        tmp = features.shift(periods=60 * (w + 1), freq='S')
        tmp.columns = [c + '_' + str(w + 1) + 'w' for c in features.columns]
        features_ext = pd.concat([features_ext, tmp], axis=1)
    
    if candle_type == '5min':
        features_ext = features_ext.shift(periods=300, freq='S')
        features_ext = features_ext.fillna(method='ffill')
    features_ext = features_ext[features_ext.index == target_dt]
    return features_ext
def ta(name, price_h, price_l, price_c, price_v, price_o):
    # function 'MAX'/'MAXINDEX'/'MIN'/'MININDEX'/'MINMAX'/'MINMAXINDEX'/'SUM' is missing
    if name == 'AD':
        return talib.AD(np.array(price_h), np.array(price_l),
                        np.array(price_c), np.asarray(price_v, dtype='float'))
    if name == 'ADOSC':
        return talib.ADOSC(np.array(price_h),
                           np.array(price_l),
                           np.array(price_c),
                           np.asarray(price_v, dtype='float'),
                           fastperiod=2,
                           slowperiod=10)
    if name == 'ADX':
        return talib.ADX(np.array(price_h),
                         np.array(price_l),
                         np.asarray(price_c, dtype='float'),
                         timeperiod=14)
    if name == 'ADXR':
        return talib.ADXR(np.array(price_h),
                          np.array(price_l),
                          np.asarray(price_c, dtype='float'),
                          timeperiod=14)
    if name == 'APO':
        return talib.APO(np.asarray(price_c, dtype='float'),
                         fastperiod=12,
                         slowperiod=26,
                         matype=0)
    if name == 'AROON':
        AROON_DWON, AROON2_UP = talib.AROON(np.array(price_h),
                                            np.asarray(price_l, dtype='float'),
                                            timeperiod=90)
        return (AROON_DWON, AROON2_UP)
    if name == 'AROONOSC':
        return talib.AROONOSC(np.array(price_h),
                              np.asarray(price_l, dtype='float'),
                              timeperiod=14)
    if name == 'ATR':
        return talib.ATR(np.array(price_h),
                         np.array(price_l),
                         np.asarray(price_c, dtype='float'),
                         timeperiod=14)
    if name == 'AVGPRICE':
        return talib.AVGPRICE(np.array(price_o), np.array(price_h),
                              np.array(price_l),
                              np.asarray(price_c, dtype='float'))
    if name == 'BBANDS':
        BBANDS1, BBANDS2, BBANDS3 = talib.BBANDS(np.asarray(price_c,
                                                            dtype='float'),
                                                 matype=MA_Type.T3)
        return BBANDS1
    if name == 'BETA':
        return talib.BETA(np.array(price_h),
                          np.asarray(price_l, dtype='float'),
                          timeperiod=5)
    if name == 'BOP':
        return talib.BOP(np.array(price_o), np.array(price_h),
                         np.array(price_l), np.asarray(price_c, dtype='float'))
    if name == 'CCI':
        return talib.CCI(np.array(price_h),
                         np.array(price_l),
                         np.asarray(price_c, dtype='float'),
                         timeperiod=14)
    if name == 'CDL2CROWS':
        return talib.CDL2CROWS(np.array(price_o), np.array(price_h),
                               np.array(price_l),
                               np.asarray(price_c, dtype='float'))
    if name == 'CDL3BLACKCROWS':
        return talib.CDL3BLACKCROWS(np.array(price_o), np.array(price_h),
                                    np.array(price_l),
                                    np.asarray(price_c, dtype='float'))
    if name == 'CDL3INSIDE':
        return talib.CDL3INSIDE(np.array(price_o), np.array(price_h),
                                np.array(price_l),
                                np.asarray(price_c, dtype='float'))
    if name == 'CDL3LINESTRIKE':
        return talib.CDL3LINESTRIKE(np.array(price_o), np.array(price_h),
                                    np.array(price_l),
                                    np.asarray(price_c, dtype='float'))
    if name == 'CDL3OUTSIDE':
        return talib.CDL3OUTSIDE(np.array(price_o), np.array(price_h),
                                 np.array(price_l),
                                 np.asarray(price_c, dtype='float'))
    if name == 'CDL3STARSINSOUTH':
        return talib.CDL3STARSINSOUTH(np.array(price_o), np.array(price_h),
                                      np.array(price_l),
                                      np.asarray(price_c, dtype='float'))
    if name == 'CDL3WHITESOLDIERS':
        return talib.CDL3WHITESOLDIERS(np.array(price_o), np.array(price_h),
                                       np.array(price_l),
                                       np.asarray(price_c, dtype='float'))
    if name == 'CDLABANDONEDBABY':
        return talib.CDLABANDONEDBABY(np.array(price_o),
                                      np.array(price_h),
                                      np.array(price_l),
                                      np.asarray(price_c, dtype='float'),
                                      penetration=0)
    if name == 'CDLADVANCEBLOCK':
        return talib.CDLADVANCEBLOCK(np.array(price_o), np.array(price_h),
                                     np.array(price_l),
                                     np.asarray(price_c, dtype='float'))
    if name == 'CDLBELTHOLD':
        return talib.CDLBELTHOLD(np.array(price_o), np.array(price_h),
                                 np.array(price_l),
                                 np.asarray(price_c, dtype='float'))
    if name == 'CDLBREAKAWAY':
        return talib.CDLBREAKAWAY(np.array(price_o), np.array(price_h),
                                  np.array(price_l),
                                  np.asarray(price_c, dtype='float'))
    if name == 'CDLCLOSINGMARUBOZU':
        return talib.CDLCLOSINGMARUBOZU(np.array(price_o), np.array(price_h),
                                        np.array(price_l),
                                        np.asarray(price_c, dtype='float'))
    if name == 'CDLCONCEALBABYSWALL':
        return talib.CDLCONCEALBABYSWALL(np.array(price_o), np.array(price_h),
                                         np.array(price_l),
                                         np.asarray(price_c, dtype='float'))
    if name == 'CDLCOUNTERATTACK':
        return talib.CDLCOUNTERATTACK(np.array(price_o), np.array(price_h),
                                      np.array(price_l),
                                      np.asarray(price_c, dtype='float'))
    if name == 'CDLDARKCLOUDCOVER':
        return talib.CDLDARKCLOUDCOVER(np.array(price_o),
                                       np.array(price_h),
                                       np.array(price_l),
                                       np.asarray(price_c, dtype='float'),
                                       penetration=0)
    if name == 'CDLDOJI':
        return talib.CDLDOJI(np.array(price_o), np.array(price_h),
                             np.array(price_l),
                             np.asarray(price_c, dtype='float'))
    if name == 'CDLDOJISTAR':
        return talib.CDLDOJISTAR(np.array(price_o), np.array(price_h),
                                 np.array(price_l),
                                 np.asarray(price_c, dtype='float'))
    if name == 'CDLDRAGONFLYDOJI':
        return talib.CDLDRAGONFLYDOJI(np.array(price_o), np.array(price_h),
                                      np.array(price_l),
                                      np.asarray(price_c, dtype='float'))
    if name == 'CDLENGULFING':
        return talib.CDLENGULFING(np.array(price_o), np.array(price_h),
                                  np.array(price_l),
                                  np.asarray(price_c, dtype='float'))
    if name == 'CDLEVENINGDOJISTAR':
        return talib.CDLEVENINGDOJISTAR(np.array(price_o),
                                        np.array(price_h),
                                        np.array(price_l),
                                        np.asarray(price_c, dtype='float'),
                                        penetration=0)
    if name == 'CDLEVENINGSTAR':
        return talib.CDLEVENINGSTAR(np.array(price_o),
                                    np.array(price_h),
                                    np.array(price_l),
                                    np.asarray(price_c, dtype='float'),
                                    penetration=0)
    if name == 'CDLGAPSIDESIDEWHITE':
        return talib.CDLGAPSIDESIDEWHITE(np.array(price_o), np.array(price_h),
                                         np.array(price_l),
                                         np.asarray(price_c, dtype='float'))
    if name == 'CDLGRAVESTONEDOJI':
        return talib.CDLGRAVESTONEDOJI(np.array(price_o), np.array(price_h),
                                       np.array(price_l),
                                       np.asarray(price_c, dtype='float'))
    if name == 'CDLHAMMER':
        return talib.CDLHAMMER(np.array(price_o), np.array(price_h),
                               np.array(price_l),
                               np.asarray(price_c, dtype='float'))
    if name == 'CDLHANGINGMAN':
        return talib.CDLHANGINGMAN(np.array(price_o), np.array(price_h),
                                   np.array(price_l),
                                   np.asarray(price_c, dtype='float'))
    if name == 'CDLHARAMI':
        return talib.CDLHARAMI(np.array(price_o), np.array(price_h),
                               np.array(price_l),
                               np.asarray(price_c, dtype='float'))
    if name == 'CDLHARAMICROSS':
        return talib.CDLHARAMICROSS(np.array(price_o), np.array(price_h),
                                    np.array(price_l),
                                    np.asarray(price_c, dtype='float'))
    if name == 'CDLHIGHWAVE':
        return talib.CDLHIGHWAVE(np.array(price_o), np.array(price_h),
                                 np.array(price_l),
                                 np.asarray(price_c, dtype='float'))
    if name == 'CDLHIKKAKE':
        return talib.CDLHIKKAKE(np.array(price_o), np.array(price_h),
                                np.array(price_l),
                                np.asarray(price_c, dtype='float'))
    if name == 'CDLHIKKAKEMOD':
        return talib.CDLHIKKAKEMOD(np.array(price_o), np.array(price_h),
                                   np.array(price_l),
                                   np.asarray(price_c, dtype='float'))
    if name == 'CDLHOMINGPIGEON':
        return talib.CDLHOMINGPIGEON(np.array(price_o), np.array(price_h),
                                     np.array(price_l),
                                     np.asarray(price_c, dtype='float'))
    if name == 'CDLIDENTICAL3CROWS':
        return talib.CDLIDENTICAL3CROWS(np.array(price_o), np.array(price_h),
                                        np.array(price_l),
                                        np.asarray(price_c, dtype='float'))
    if name == 'CDLINNECK':
        return talib.CDLINNECK(np.array(price_o), np.array(price_h),
                               np.array(price_l),
                               np.asarray(price_c, dtype='float'))
    if name == 'CDLINVERTEDHAMMER':
        return talib.CDLINVERTEDHAMMER(np.array(price_o), np.array(price_h),
                                       np.array(price_l),
                                       np.asarray(price_c, dtype='float'))
    if name == 'CDLKICKING':
        return talib.CDLKICKING(np.array(price_o), np.array(price_h),
                                np.array(price_l),
                                np.asarray(price_c, dtype='float'))
    if name == 'CDLKICKINGBYLENGTH':
        return talib.CDLKICKINGBYLENGTH(np.array(price_o), np.array(price_h),
                                        np.array(price_l),
                                        np.asarray(price_c, dtype='float'))

    if name == 'CDLLADDERBOTTOM':
        return talib.CDLLADDERBOTTOM(np.array(price_o), np.array(price_h),
                                     np.array(price_l),
                                     np.asarray(price_c, dtype='float'))
    if name == 'CDLLONGLEGGEDDOJI':
        return talib.CDLLONGLEGGEDDOJI(np.array(price_o), np.array(price_h),
                                       np.array(price_l),
                                       np.asarray(price_c, dtype='float'))
    if name == 'CDLLONGLINE':
        return talib.CDLLONGLINE(np.array(price_o), np.array(price_h),
                                 np.array(price_l),
                                 np.asarray(price_c, dtype='float'))
    if name == 'CDLMARUBOZU':
        return talib.CDLMARUBOZU(np.array(price_o), np.array(price_h),
                                 np.array(price_l),
                                 np.asarray(price_c, dtype='float'))
    if name == 'CDLMATCHINGLOW':
        return talib.CDLMATCHINGLOW(np.array(price_o), np.array(price_h),
                                    np.array(price_l),
                                    np.asarray(price_c, dtype='float'))
    if name == 'CDLMATHOLD':
        return talib.CDLMATHOLD(np.array(price_o), np.array(price_h),
                                np.array(price_l),
                                np.asarray(price_c, dtype='float'))
    if name == 'CDLMORNINGDOJISTAR':
        return talib.CDLMORNINGDOJISTAR(np.array(price_o),
                                        np.array(price_h),
                                        np.array(price_l),
                                        np.asarray(price_c, dtype='float'),
                                        penetration=0)
    if name == 'CDLMORNINGSTAR':
        return talib.CDLMORNINGSTAR(np.array(price_o),
                                    np.array(price_h),
                                    np.array(price_l),
                                    np.asarray(price_c, dtype='float'),
                                    penetration=0)
    if name == 'CDLONNECK':
        return talib.CDLONNECK(np.array(price_o), np.array(price_h),
                               np.array(price_l),
                               np.asarray(price_c, dtype='float'))
    if name == 'CDLPIERCING':
        return talib.CDLPIERCING(np.array(price_o), np.array(price_h),
                                 np.array(price_l),
                                 np.asarray(price_c, dtype='float'))
    if name == 'CDLRICKSHAWMAN':
        return talib.CDLRICKSHAWMAN(np.array(price_o), np.array(price_h),
                                    np.array(price_l),
                                    np.asarray(price_c, dtype='float'))
    if name == 'CDLRISEFALL3METHODS':
        return talib.CDLRISEFALL3METHODS(np.array(price_o), np.array(price_h),
                                         np.array(price_l),
                                         np.asarray(price_c, dtype='float'))
    if name == 'CDLSEPARATINGLINES':
        return talib.CDLSEPARATINGLINES(np.array(price_o), np.array(price_h),
                                        np.array(price_l),
                                        np.asarray(price_c, dtype='float'))
    if name == 'CDLSHOOTINGSTAR':
        return talib.CDLSHOOTINGSTAR(np.array(price_o), np.array(price_h),
                                     np.array(price_l),
                                     np.asarray(price_c, dtype='float'))
    if name == 'CDLSHORTLINE':
        return talib.CDLSHORTLINE(np.array(price_o), np.array(price_h),
                                  np.array(price_l),
                                  np.asarray(price_c, dtype='float'))
    if name == 'CDLSPINNINGTOP':
        return talib.CDLSPINNINGTOP(np.array(price_o), np.array(price_h),
                                    np.array(price_l),
                                    np.asarray(price_c, dtype='float'))
    if name == 'CDLSTALLEDPATTERN':
        return talib.CDLSTALLEDPATTERN(np.array(price_o), np.array(price_h),
                                       np.array(price_l),
                                       np.asarray(price_c, dtype='float'))
    if name == 'CDLSTICKSANDWICH':
        return talib.CDLSTICKSANDWICH(np.array(price_o), np.array(price_h),
                                      np.array(price_l),
                                      np.asarray(price_c, dtype='float'))
    if name == 'CDLTAKURI':
        return talib.CDLTAKURI(np.array(price_o), np.array(price_h),
                               np.array(price_l),
                               np.asarray(price_c, dtype='float'))
    if name == 'CDLTASUKIGAP':
        return talib.CDLTASUKIGAP(np.array(price_o), np.array(price_h),
                                  np.array(price_l),
                                  np.asarray(price_c, dtype='float'))
    if name == 'CDLTHRUSTING':
        return talib.CDLTHRUSTING(np.array(price_o), np.array(price_h),
                                  np.array(price_l),
                                  np.asarray(price_c, dtype='float'))
    if name == 'CDLTRISTAR':
        return talib.CDLTRISTAR(np.array(price_o), np.array(price_h),
                                np.array(price_l),
                                np.asarray(price_c, dtype='float'))
    if name == 'CDLUNIQUE3RIVER':
        return talib.CDLUNIQUE3RIVER(np.array(price_o), np.array(price_h),
                                     np.array(price_l),
                                     np.asarray(price_c, dtype='float'))
    if name == 'CDLUPSIDEGAP2CROWS':
        return talib.CDLUPSIDEGAP2CROWS(np.array(price_o), np.array(price_h),
                                        np.array(price_l),
                                        np.asarray(price_c, dtype='float'))
    if name == 'CDLXSIDEGAP3METHODS':
        return talib.CDLXSIDEGAP3METHODS(np.array(price_o), np.array(price_h),
                                         np.array(price_l),
                                         np.asarray(price_c, dtype='float'))
    if name == 'CMO':
        return talib.CMO(np.asarray(price_c, dtype='float'), timeperiod=14)
    if name == 'CORREL':
        return talib.CORREL(np.array(price_h),
                            np.asarray(price_l, dtype='float'),
                            timeperiod=30)
    if name == 'DEMA':
        return talib.DEMA(np.asarray(price_c, dtype='float'), timeperiod=30)
    if name == 'DX':
        return talib.DX(np.array(price_h),
                        np.array(price_l),
                        np.asarray(price_c, dtype='float'),
                        timeperiod=14)
    if name == 'EMA':
        return talib.EMA(np.asarray(price_c, dtype='float'), timeperiod=30)
    if name == 'HT_DCPERIOD':
        return talib.HT_DCPERIOD(np.asarray(price_c, dtype='float'))
    if name == 'HT_DCPHASE':
        return talib.HT_DCPHASE(np.asarray(price_c, dtype='float'))
    if name == 'HT_PHASOR':
        HT_PHASOR1, HT_PHASOR2 = talib.HT_PHASOR(
            np.asarray(price_c, dtype='float')
        )  # use HT_PHASOR1 for the indication of up and down
        return HT_PHASOR1
    if name == 'HT_SINE':
        HT_SINE1, HT_SINE2 = talib.HT_SINE(np.asarray(price_c, dtype='float'))
        return HT_SINE1
    if name == 'HT_TRENDLINE':
        return talib.HT_TRENDLINE(np.asarray(price_c, dtype='float'))
    if name == 'HT_TRENDMODE':
        return talib.HT_TRENDMODE(np.asarray(price_c, dtype='float'))
    if name == 'KAMA':
        return talib.KAMA(np.asarray(price_c, dtype='float'), timeperiod=30)
    if name == 'LINEARREG':
        return talib.LINEARREG(np.asarray(price_c, dtype='float'),
                               timeperiod=14)
    if name == 'LINEARREG_ANGLE':
        return talib.LINEARREG_ANGLE(np.asarray(price_c, dtype='float'),
                                     timeperiod=14)
    if name == 'LINEARREG_INTERCEPT':
        return talib.LINEARREG_INTERCEPT(np.asarray(price_c, dtype='float'),
                                         timeperiod=14)
    if name == 'LINEARREG_SLOPE':
        return talib.LINEARREG_SLOPE(np.asarray(price_c, dtype='float'),
                                     timeperiod=14)
    if name == 'MA':
        return talib.MA(np.asarray(price_c, dtype='float'),
                        timeperiod=30,
                        matype=0)
    if name == 'MACD':
        MACD1, MACD2, MACD3 = talib.MACD(np.asarray(price_c, dtype='float'),
                                         fastperiod=12,
                                         slowperiod=26,
                                         signalperiod=9)
        return MACD1
    if nam == 'MACDEXT':
        return talib.MACDEXT(np.asarray(price_c, dtype='float'),
                             fastperiod=12,
                             fastmatype=0,
                             slowperiod=26,
                             slowmatype=0,
                             signalperiod=9,
                             signalmatype=0)
    if name == 'MACDFIX':
        MACDFIX1, MACDFIX2, MACDFIX3 = talib.MACDFIX(np.asarray(price_c,
                                                                dtype='float'),
                                                     signalperiod=9)
        return MACDFIX1
    if name == 'MAMA':
        MAMA1, MAMA2 = talib.MAMA(np.asarray(price_c, dtype='float'),
                                  fastlimit=0,
                                  slowlimit=0)
        return MAMA1
    if name == 'MEDPRICE':
        return talib.MEDPRICE(np.array(price_h),
                              np.asarray(price_l, dtype='float'))
    if name == 'MINUS_DI':
        return talib.MINUS_DI(np.array(price_h),
                              np.array(price_l),
                              np.asarray(price_c, dtype='float'),
                              timeperiod=14)
    if name == 'MINUS_DM':
        return talib.MINUS_DM(np.array(price_h),
                              np.asarray(price_l, dtype='float'),
                              timeperiod=14)
    if name == 'MOM':
        return talib.MOM(np.asarray(price_c, dtype='float'), timeperiod=10)
    if name == 'NATR':
        return talib.NATR(np.array(price_h),
                          np.array(price_l),
                          np.asarray(price_c, dtype='float'),
                          timeperiod=14)
    if name == 'OBV':
        return talib.OBV(np.array(price_c), np.asarray(price_v, dtype='float'))
    if name == 'PLUS_DI':
        return talib.PLUS_DI(np.array(price_h),
                             np.array(price_l),
                             np.asarray(price_c, dtype='float'),
                             timeperiod=14)
    if name == 'PLUS_DM':
        return talib.PLUS_DM(np.array(price_h),
                             np.asarray(price_l, dtype='float'),
                             timeperiod=14)
    if name == 'PPO':
        return talib.PPO(np.asarray(price_c, dtype='float'),
                         fastperiod=12,
                         slowperiod=26,
                         matype=0)
    if name == 'ROC':
        return talib.ROC(np.asarray(price_c, dtype='float'), timeperiod=10)
    if name == 'ROCP':
        return talib.ROCP(np.asarray(price_c, dtype='float'), timeperiod=10)
    if name == 'ROCR100':
        return talib.ROCR100(np.asarray(price_c, dtype='float'), timeperiod=10)
    if name == 'RSI':
        return talib.RSI(np.asarray(price_c, dtype='float'), timeperiod=14)
    if name == 'SAR':
        return talib.SAR(np.array(price_h),
                         np.asarray(price_l, dtype='float'),
                         acceleration=0,
                         maximum=0)
    if name == 'SAREXT':
        return talib.SAREXT(np.array(price_h),
                            np.asarray(price_l, dtype='float'),
                            startvalue=0,
                            offsetonreverse=0,
                            accelerationinitlong=0,
                            accelerationlong=0,
                            accelerationmaxlong=0,
                            accelerationinitshort=0,
                            accelerationshort=0,
                            accelerationmaxshort=0)
    if name == 'SMA':
        return talib.SMA(np.asarray(price_c, dtype='float'), timeperiod=30)
    if name == 'STDDEV':
        return talib.STDDEV(np.asarray(price_c, dtype='float'),
                            timeperiod=5,
                            nbdev=1)
    if name == 'STOCH':
        STOCH1, STOCH2 = talib.STOCH(np.array(price_h),
                                     np.array(price_l),
                                     np.asarray(price_c, dtype='float'),
                                     fastk_period=5,
                                     slowk_period=3,
                                     slowk_matype=0,
                                     slowd_period=3,
                                     slowd_matype=0)
        return STOCH1
    if name == 'STOCHF':
        STOCHF1, STOCHF2 = talib.STOCHF(np.array(price_h),
                                        np.array(price_l),
                                        np.asarray(price_c, dtype='float'),
                                        fastk_period=5,
                                        fastd_period=3,
                                        fastd_matype=0)
        return STOCHF1
    if name == 'STOCHRSI':
        STOCHRSI1, STOCHRSI2 = talib.STOCHRSI(np.asarray(price_c,
                                                         dtype='float'),
                                              timeperiod=14,
                                              fastk_period=5,
                                              fastd_period=3,
                                              fastd_matype=0)
        return STOCHRSI1
    if name == 'T3':
        return talib.T3(np.asarray(price_c, dtype='float'),
                        timeperiod=5,
                        vfactor=0)
    if name == 'TEMA':
        return talib.TEMA(np.asarray(price_c, dtype='float'), timeperiod=30)
    if name == 'TRANGE':
        return talib.TRANGE(np.array(price_h), np.array(price_l),
                            np.asarray(price_c, dtype='float'))
    if name == 'TRIMA':
        return talib.TRIMA(np.asarray(price_c, dtype='float'), timeperiod=30)
    if name == 'TRIX':
        return talib.TRIX(np.asarray(price_c, dtype='float'), timeperiod=30)
    if name == 'TSF':
        return talib.TSF(np.asarray(price_c, dtype='float'), timeperiod=14)
    if name == 'TYPPRICE':
        return talib.TYPPRICE(np.array(price_h), np.array(price_l),
                              np.asarray(price_c, dtype='float'))
    if name == 'ULTOSC':
        return talib.ULTOSC(np.array(price_h),
                            np.array(price_l),
                            np.asarray(price_c, dtype='float'),
                            timeperiod1=7,
                            timeperiod2=14,
                            timeperiod3=28)
    if name == 'VAR':
        return talib.VAR(np.asarray(price_c, dtype='float'),
                         timeperiod=5,
                         nbdev=1)
    if name == 'WCLPRICE':
        return talib.WCLPRICE(np.array(price_h), np.array(price_l),
                              np.asarray(price_c, dtype='float'))
    if name == 'WILLR':
        return talib.WILLR(np.array(price_h),
                           np.array(price_l),
                           np.asarray(price_c, dtype='float'),
                           timeperiod=14)
    if name == 'WMA':
        return talib.WMA(np.asarray(price_c, dtype='float'), timeperiod=30)
예제 #27
0
async def indicators(exchange: str,
                     symbol: str,
                     interval: str = '30m',
                     limit: int = 100):
    exchange = getattr(ccxt, exchange)()
    kline = exchange.fetch_ohlcv(symbol, interval, limit=int(limit))

    df = pd.DataFrame(
        np.array(kline),
        columns=['open_time', 'open', 'high', 'low', 'close', 'volume'],
        dtype='float64')

    op = df['open']
    hi = df['high']
    lo = df['low']
    cl = df['close']
    vl = df['volume']

    adx = talib.ADX(hi.values, lo.values, cl.values, timeperiod=14)
    rsi = talib.RSI(cl.values, timeperiod=14)
    plus_di = talib.PLUS_DI(hi.values, lo.values, cl.values, timeperiod=14)
    minus_di = talib.MINUS_DI(hi.values, lo.values, cl.values, timeperiod=14)
    sma = talib.SMA(cl.values, timeperiod=30)
    sma_5 = talib.SMA(cl.values, timeperiod=5)
    sma_10 = talib.SMA(cl.values, timeperiod=10)
    sma_dir = convert_number(round(sma[-1], 8) - round(sma_10[-1], 8))
    macd, macdsignal, macdhist = talib.MACD(cl.values,
                                            fastperiod=12,
                                            slowperiod=26,
                                            signalperiod=14)
    macd = convert_number(macd[-1])
    macdsignal = convert_number(macdsignal[-1])
    ma_50 = convert_number(talib.MA(cl.values, timeperiod=50, matype=0)[-1])
    ma_100 = convert_number(talib.MA(cl.values, timeperiod=100, matype=0)[-1])
    obv = talib.OBV(cl.values, vl.values)
    rsi_obv = convert_number(talib.RSI(obv, timeperiod=14)[-1])
    linear_regression = talib.LINEARREG(cl.values, timeperiod=14)[-1]
    linear_angle = convert_number(
        talib.LINEARREG_ANGLE(cl.values, timeperiod=14)[-1])
    linear_intercept = convert_number(
        talib.LINEARREG_INTERCEPT(cl.values, timeperiod=14)[-1])
    linear_slope = convert_number(
        talib.LINEARREG_SLOPE(cl.values, timeperiod=14)[-1])

    return {
        "adx": adx[-1],
        "rsi": rsi[-1],
        "plus_di": plus_di[-1],
        "minus_di": minus_di[-1],
        "sma": round(sma[-1], 8),
        "sma_10": round(sma_10[-1], 8),
        "sma_5": round(sma_5[-1], 8),
        "sma_dir": sma_dir,
        "macd": macd,
        "macdsignal": macdsignal,
        "ma_50": ma_50,
        "ma_100": ma_100,
        "rsi_obv": rsi_obv,
        "linear_regression": linear_regression,
        "linear_angle": linear_angle,
        "linear_intercept": linear_intercept,
        "linear_slope": linear_slope
    }
예제 #28
0
def LINEARREG_ANGLE(Series, N=14):
    res = talib.LINEARREG_ANGLE(Series.values, N)
    return pd.Series(res, index=Series.index)
예제 #29
0
def calc_features(df):
    open = df['op']
    high = df['hi']
    low = df['lo']
    close = df['cl']
    volume = df['volume']

    orig_columns = df.columns

    hilo = (df['hi'] + df['lo']) / 2
    df['BBANDS_upperband'], df['BBANDS_middleband'], df[
        'BBANDS_lowerband'] = talib.BBANDS(close,
                                           timeperiod=5,
                                           nbdevup=2,
                                           nbdevdn=2,
                                           matype=0)
    df['BBANDS_upperband'] -= hilo
    df['BBANDS_middleband'] -= hilo
    df['BBANDS_lowerband'] -= hilo
    df['DEMA'] = talib.DEMA(close, timeperiod=30) - hilo
    df['EMA'] = talib.EMA(close, timeperiod=30) - hilo
    df['HT_TRENDLINE'] = talib.HT_TRENDLINE(close) - hilo
    df['KAMA'] = talib.KAMA(close, timeperiod=30) - hilo
    df['MA'] = talib.MA(close, timeperiod=30, matype=0) - hilo
    df['MIDPOINT'] = talib.MIDPOINT(close, timeperiod=14) - hilo
    df['SMA'] = talib.SMA(close, timeperiod=30) - hilo
    df['T3'] = talib.T3(close, timeperiod=5, vfactor=0) - hilo
    df['TEMA'] = talib.TEMA(close, timeperiod=30) - hilo
    df['TRIMA'] = talib.TRIMA(close, timeperiod=30) - hilo
    df['WMA'] = talib.WMA(close, timeperiod=30) - hilo

    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['AROON_aroondown'], df['AROON_aroonup'] = talib.AROON(high,
                                                             low,
                                                             timeperiod=14)
    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['DX'] = talib.DX(high, low, close, timeperiod=14)
    df['MACD_macd'], df['MACD_macdsignal'], df['MACD_macdhist'] = talib.MACD(
        close, fastperiod=12, slowperiod=26, signalperiod=9)
    # skip MACDEXT MACDFIX たぶん同じなので
    df['MFI'] = talib.MFI(high, low, close, volume, timeperiod=14)
    df['MINUS_DI'] = talib.MINUS_DI(high, low, close, timeperiod=14)
    df['MINUS_DM'] = talib.MINUS_DM(high, low, timeperiod=14)
    df['MOM'] = talib.MOM(close, timeperiod=10)
    df['PLUS_DI'] = talib.PLUS_DI(high, low, close, timeperiod=14)
    df['PLUS_DM'] = talib.PLUS_DM(high, low, timeperiod=14)
    df['RSI'] = talib.RSI(close, timeperiod=14)
    df['STOCH_slowk'], df['STOCH_slowd'] = talib.STOCH(high,
                                                       low,
                                                       close,
                                                       fastk_period=5,
                                                       slowk_period=3,
                                                       slowk_matype=0,
                                                       slowd_period=3,
                                                       slowd_matype=0)
    df['STOCHF_fastk'], df['STOCHF_fastd'] = talib.STOCHF(high,
                                                          low,
                                                          close,
                                                          fastk_period=5,
                                                          fastd_period=3,
                                                          fastd_matype=0)
    df['STOCHRSI_fastk'], df['STOCHRSI_fastd'] = talib.STOCHRSI(close,
                                                                timeperiod=14,
                                                                fastk_period=5,
                                                                fastd_period=3,
                                                                fastd_matype=0)
    df['TRIX'] = talib.TRIX(close, timeperiod=30)
    df['ULTOSC'] = talib.ULTOSC(high,
                                low,
                                close,
                                timeperiod1=7,
                                timeperiod2=14,
                                timeperiod3=28)
    df['WILLR'] = talib.WILLR(high, low, close, timeperiod=14)

    df['AD'] = talib.AD(high, low, close, volume)
    df['ADOSC'] = talib.ADOSC(high,
                              low,
                              close,
                              volume,
                              fastperiod=3,
                              slowperiod=10)
    df['OBV'] = talib.OBV(close, volume)

    df['ATR'] = talib.ATR(high, low, close, timeperiod=14)
    df['NATR'] = talib.NATR(high, low, close, timeperiod=14)
    df['TRANGE'] = talib.TRANGE(high, low, close)

    df['HT_DCPERIOD'] = talib.HT_DCPERIOD(close)
    df['HT_DCPHASE'] = talib.HT_DCPHASE(close)
    df['HT_PHASOR_inphase'], df['HT_PHASOR_quadrature'] = talib.HT_PHASOR(
        close)
    df['HT_SINE_sine'], df['HT_SINE_leadsine'] = talib.HT_SINE(close)
    df['HT_TRENDMODE'] = talib.HT_TRENDMODE(close)

    df['BETA'] = talib.BETA(high, low, timeperiod=5)
    df['CORREL'] = talib.CORREL(high, low, timeperiod=30)
    df['LINEARREG'] = talib.LINEARREG(close, timeperiod=14) - close
    df['LINEARREG_ANGLE'] = talib.LINEARREG_ANGLE(close, timeperiod=14)
    df['LINEARREG_INTERCEPT'] = talib.LINEARREG_INTERCEPT(
        close, timeperiod=14) - close
    df['LINEARREG_SLOPE'] = talib.LINEARREG_SLOPE(close, timeperiod=14)
    df['STDDEV'] = talib.STDDEV(close, timeperiod=5, nbdev=1)

    return df
예제 #30
0
turnoverx = np.asarray(datax['turnover'])

roc1 = ta.ROC(closex, 1)
roc5 = ta.ROC(closex, 5)
roc10 = ta.ROC(closex, 10)
rsi = ta.RSI(closex, 14)
ultosc = ta.ULTOSC(highx, lowx, closex)
slowk, slowd = ta.STOCH(highx, lowx, closex)
slow_diff = slowk - slowd
adx = ta.ADX(highx, lowx, closex, 14)
ppo = ta.PPO(closex)
willr = ta.WILLR(highx, lowx, closex)
cci = ta.CCI(highx, lowx, closex)
bop = ta.BOP(openx, highx, lowx, closex)
std_p20 = ta.STDDEV(closex, 20)
angle = ta.LINEARREG_ANGLE(closex)
mfi = ta.MFI(highx, lowx, closex, volumex)
adosc = ta.ADOSC(highx, lowx, closex, turnoverx)

## obtian training data

start_day = '20150105'
end_day = '20150714'
st = (datax['date'] == start_day).nonzero()[0][0]
ed = (datax['date'] == end_day).nonzero()[0][0]

features = np.column_stack((roc1, roc5, roc10,rsi,ultosc,slowk,slow_diff,adx,ppo,willr,\
                            cci,bop,std_p20,angle,mfi,adosc))

features2 = np.float64(features[st:ed, :])