Пример #1
0
def wclprice(
    client,
    symbol,
    timeframe="6m",
    opencol="open",
    highcol="high",
    lowcol="low",
    closecol="close",
):
    """This will return a dataframe of weighted close price for the given symbol across
    the given timeframe

    Args:
        client (pyEX.Client): Client
        symbol (string): Ticker
        timeframe (string): timeframe to use, for pyEX.chart
        highcol (string): column to use to calculate
        lowcol (string): column to use to calculate
        closecol (string): column to use to calculate

    Returns:
        DataFrame: result
    """
    df = client.chartDF(symbol, timeframe)
    wcl = t.WCLPRICE(df[highcol].values, df[lowcol].values,
                     df[closecol].values)
    return pd.DataFrame({
        highcol: df[highcol].values,
        lowcol: df[lowcol].values,
        closecol: df[closecol].values,
        "wclprice": wcl,
    })
Пример #2
0
def getData(code,start,end):
    data = pandasData.DataReader(code,'yahoo',start,end)
    # print len(data)
    f_ema = ta.EMA(data['Close'].values, timeperiod=30).tolist()
    # print f_ema
    f_ma = ta.MA(data['Close'].values, timeperiod=30, matype=0).tolist()
    f_wma = ta.WMA(data['Close'].values, timeperiod=30).tolist()
    f_momentum = ta.MOM(data['Close'].values, timeperiod=10).tolist()
    f_roc = ta.ROC(data['Close'].values, timeperiod=10).tolist()
    # f_cycle = ta.HT_DCPERIOD(data['Close'].values).tolist()
    f_price = ta.WCLPRICE(data['High'].values, data['Low'].values, data['Close'].values).tolist()
    f_natr = ta.NATR(data['High'].values, data['Low'].values, data['Close'].values, timeperiod=14).tolist()
    f_stddev = ta.STDDEV(data['Close'].values, timeperiod=5, nbdev=1).tolist()
    X = pd.DataFrame(
        pd.np.array([f_ema, f_ma, f_wma, f_momentum, f_roc, f_price, f_natr, f_stddev]).T[32:]
        ,columns=['f_ema','f_ma','f_wma','f_momentum','f_roc','f_price','f_natr','f_stddev'])
    # print X['f_ema'].size
    # print X
    data = data['Close'].tolist()
    finaldata = [[] for i in range(2)]
    for i in range(0, len(data) - 1):
        temp = (data[i + 1] - data[i]) / data[i]
        finaldata[0].append(temp)
        if (temp > 0):
            finaldata[1].append(1)
        else:
            finaldata[1].append(0)
    # print len(data)
    data = data[31:len(data) - 1]
    # print data
    Y = pd.DataFrame(pd.np.array(finaldata).T, columns=['change', 'label'])
    X = X.join(Y)
    return X
Пример #3
0
 def test_wc(self):
     """ 
     Test weighted close.
     """
     wc_qufilab = qufilab.wc(self.high, self.low, self.close)
     wc_talib = talib.WCLPRICE(self.high, self.low, self.close)
     np.testing.assert_allclose(wc_qufilab, wc_talib, rtol=self.tolerance)
Пример #4
0
    def PriceLoc(close, high, low, period = [6, 11, 22, 43, 65, 130]):
        close = np.array(close)
        high = np.array(high)
        low = np.array(low)
        WCP = ta.WCLPRICE(high, low, close)

        sma_cl = []
        for p in period:
            sma_cl.append(ta.SMA(close, timeperiod=p))
        sma_wcl = []
        for p in period:
            sma_wcl.append(ta.SMA(WCP, timeperiod=p))

        period.reverse()
        period_r = np.array([period])

        wsma_cl = np.sum(period_r.T * sma_cl, axis=0) / sum(period)
        wsma_wcl = np.sum(period_r.T * sma_wcl, axis=0) / sum(period)

        space = []
        space.append((WCP - wsma_cl) / wsma_cl)
        space.append((WCP - wsma_wcl) / wsma_wcl)
        for i in range(len(sma_cl)):
            space.append((WCP - sma_cl[i]) / sma_cl[i])
        for i in range(len(sma_wcl)):
            space.append((WCP - sma_wcl[i]) / sma_wcl[i])

        return space
Пример #5
0
 def compWCLPRICE(self):
     wcl = talib.WCLPRICE(self.high,self.low,self.close)
     self.removeNullID(wcl)
     self.rawFeatures['WCLPRICE'] = wcl 
     
     FEATURE_SIZE_DICT['WCLPRICE'] = 1
     return
Пример #6
0
    def update(self, data, N):
        self.clear()
        self.series.attachAxis(self.chart.ax)
        self.series.attachAxis(self.chart.ay)

        price_weighted = talib.WCLPRICE(data[2], data[3], data[4])
        for i, val in enumerate(price_weighted[-N:]):
            self.series.append(i + 0.5, val)
Пример #7
0
def WCLPRICE(high, low, close):
    ''' Weighted Close Price 加权收盘价

    分组: Price Transform 价格指标

    简介:

    real = WCLPRICE(high, low, close)
    '''
    return talib.WCLPRICE(high, low, close)
Пример #8
0
def getPriceTransforms(df):
    high = df['High']
    low = df['Low']
    close = df['Close']
    open = df['Open']
    volume = df['Volume']

    df['AVGPRICE'] = ta.AVGPRICE(open, high, low, close)
    df['MEDPRICE'] = ta.MEDPRICE(high, low)
    df['TYPPRICE'] = ta.TYPPRICE(high, low, close)
    df['WCLPRICE'] = ta.WCLPRICE(high, low, close)
Пример #9
0
def add_technical_indicators(dataframe):

    # Overlap Studies Functions
    dataframe["SMA"] = talib.SMA(dataframe["Close"])
    dataframe["BBANDS_up"], dataframe["BBANDS_md"], dataframe[
        "BBANDS_dw"] = talib.BBANDS(dataframe["Close"],
                                    timeperiod=5,
                                    nbdevup=2,
                                    nbdevdn=2,
                                    matype=0)
    dataframe["EMA"] = talib.EMA(dataframe["Close"], timeperiod=30)
    dataframe["HT_TRENDLINE"] = talib.HT_TRENDLINE(dataframe["Close"])
    dataframe["WMA"] = talib.WMA(dataframe["Close"], timeperiod=30)

    # Momentum Indicator Functions
    dataframe["ADX"] = talib.ADX(dataframe["High"],
                                 dataframe["Low"],
                                 dataframe["Close"],
                                 timeperiod=14)
    dataframe["MACD"], _, _ = talib.MACD(dataframe["Close"],
                                         fastperiod=12,
                                         slowperiod=26,
                                         signalperiod=9)
    dataframe["MOM"] = talib.MOM(dataframe["Close"], timeperiod=5)
    dataframe["RSI"] = talib.RSI(dataframe["Close"], timeperiod=14)

    # Volume Indicator Functions
    # dataframe["OBV"] = talib.OBV(dataframe["Close"], dataframe["Volume"])

    # Volatility Indicator Functions
    dataframe["ATR"] = talib.ATR(dataframe["High"],
                                 dataframe["Low"],
                                 dataframe["Close"],
                                 timeperiod=14)
    dataframe["TRANGE"] = talib.TRANGE(dataframe["High"], dataframe["Low"],
                                       dataframe["Close"])

    # Price Transform Functions
    dataframe["AVGPRICE"] = talib.AVGPRICE(dataframe["Open"],
                                           dataframe["High"], dataframe["Low"],
                                           dataframe["Close"])
    dataframe["MEDPRICE"] = talib.MEDPRICE(dataframe["High"], dataframe["Low"])
    dataframe["WCLPRICE"] = talib.WCLPRICE(dataframe["High"], dataframe["Low"],
                                           dataframe["Close"])

    # Statistic Functions
    dataframe["LINEARREG_SLOPE"] = talib.LINEARREG_SLOPE(dataframe["Close"],
                                                         timeperiod=14)
    dataframe["STDDEV"] = talib.STDDEV(dataframe["Close"],
                                       timeperiod=5,
                                       nbdev=1)

    dataframe = dataframe.dropna()
    return dataframe
Пример #10
0
 def get_price_studies(open, low, high, close, df):
     # https://mrjbq7.github.io/ta-lib/func_groups/price_transform.html
     df["AVGPRICE"] = talib.AVGPRICE(open, high, low, close)
     df["MEDPRICE"] = talib.MEDPRICE(high, low)
     df["TYPPRICE"] = talib.TYPPRICE(high, low, close)
     df["WCLPRICE"] = talib.WCLPRICE(high, low, close)
     df["ATR-5"] = talib.ATR(high, low, close, timeperiod=5)
     df["ATR-10"] = talib.ATR(high, low, close, timeperiod=10)
     df["ATR-20"] = talib.ATR(high, low, close, timeperiod=20)
     df["ATR-50"] = talib.ATR(high, low, close, timeperiod=50)
     df["ATR-200"] = talib.ATR(high, low, close, timeperiod=200)
def main():
    # read csv file and transform it to datafeed (df):
    df = pd.read_csv(current_dir+"/"+base_dir+"/"+in_dir+"/"+in_dir+'_'+stock_symbol+'.csv')

    # set numpy datafeed from df:
    df_numpy = {
        'Date': np.array(df['date']),
        'Open': np.array(df['open'], dtype='float'),
        'High': np.array(df['high'], dtype='float'),
        'Low': np.array(df['low'], dtype='float'),
        'Close': np.array(df['close'], dtype='float'),
        'Volume': np.array(df['volume'], dtype='float')
        }

    date = df_numpy['Date']
    openp = df_numpy['Open']
    high = df_numpy['High']
    low = df_numpy['Low']
    close = df_numpy['Close']
    volume = df_numpy['Volume']



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



    #AVGPRICE - Average Price
    avgprice = ta.AVGPRICE(openp, high, low, close)

    #MEDPRICE - Median Price
    medprice = ta.MEDPRICE(high, low)

    #TYPPRICE - Typical Price
    typprice = ta.TYPPRICE(high, low, close)

    #WCLPRICE - Weighted Close Price
    wclprice = ta.WCLPRICE(high, low, close)


    df_save = pd.DataFrame(data ={
        'date': np.array(df['date']),
        'avgprice':avgprice,
        'medprice':medprice,
        'typprice':typprice,
        'wclprice':wclprice
    })

    df_save.to_csv(current_dir+"/"+base_dir+"/"+out_dir+'/'+stock_symbol+"/"+out_dir+'_ta_price_transform_'+stock_symbol+'.csv',index=False)
Пример #12
0
    def test_wcp(self):
        result = pandas_ta.wcp(self.high, self.low, self.close)
        self.assertIsInstance(result, Series)
        self.assertEqual(result.name, "WCP")

        try:
            expected = tal.WCLPRICE(self.high, self.low, 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)
Пример #13
0
def wclprice(candles: np.ndarray,
             sequential: bool = False) -> Union[float, np.ndarray]:
    """
    WCLPRICE - Weighted Close Price

    :param candles: np.ndarray
    :param sequential: bool - default: False

    :return: float | np.ndarray
    """
    candles = slice_candles(candles, sequential)

    res = talib.WCLPRICE(candles[:, 3], candles[:, 4], candles[:, 2])

    return res if sequential else res[-1]
Пример #14
0
def get_additional_factors(open, high, low, close, volume):

    # Overlap Studies Functions
    mat = get_all_factors(open, high, low, close, volume)

    mat = np.column_stack((mat, talib.HT_TRENDLINE(close)))  ## close
    mat = np.column_stack((mat, talib.KAMA(close, timeperiod=30)))  ##close

    #Momentum Indicator Functions
    mat = np.column_stack((mat, talib.ADX(high, low, close, timeperiod=14)))
    mat = np.column_stack((mat, talib.ADXR(high, low, close, timeperiod=14)))
    mat = np.column_stack(
        (mat, talib.APO(close, fastperiod=12, slowperiod=26, matype=0)))
    mat = np.column_stack((mat, talib.AROONOSC(high, low, timeperiod=14)))
    mat = np.column_stack((mat, talib.BOP(open, high, low, close)))
    mat = np.column_stack((mat, talib.MOM(close, timeperiod=10)))

    #Volume Indicator Functions
    mat = np.column_stack((mat, talib.AD(high, low, close, volume)))
    mat = np.column_stack(
        (mat, talib.ADOSC(high,
                          low,
                          close,
                          volume,
                          fastperiod=3,
                          slowperiod=10)))
    mat = np.column_stack((mat, talib.OBV(close, volume)))

    #Volatility Indicator Functions
    mat = np.column_stack((mat, talib.NATR(high, low, close, timeperiod=14)))
    mat = np.column_stack((mat, talib.TRANGE(high, low, close)))

    #Price Transform Functions
    mat = np.column_stack((mat, talib.AVGPRICE(open, high, low, close)))
    mat = np.column_stack((mat, talib.MEDPRICE(high, low)))
    mat = np.column_stack((mat, talib.TYPPRICE(high, low, close)))
    mat = np.column_stack((mat, talib.WCLPRICE(high, low, close)))

    #Cycle Indicator Functions
    mat = np.column_stack((mat, talib.HT_DCPERIOD(close)))
    mat = np.column_stack((mat, talib.HT_DCPHASE(close)))
    mat = np.column_stack((mat, talib.HT_TRENDMODE(close)))

    # 20

    return mat
def init_state(indata, test=False):
    openn = indata['open'].values
    close = indata['close'].values
    high = indata['high'].values
    low = indata['low'].values
    volume = indata['volume'].values
    
    diff = np.diff(close)
    diff = np.insert(diff, 0, 0)
    
    sma30 = talib.SMA(close, 30)
    sma60 = talib.SMA(close, timeperiod=60)
    rsi = talib.RSI(close, timeperiod=14)
    atr = talib.ATR(high, low, close, timeperiod=14)
    trange = talib.TRANGE(high, low, close)
    macd, macdsignal, macdhist = talib.MACD(close, 12, 26, 9)
    upper, middle, lower = talib.BBANDS(close, 20, 2, 2)
    ema = talib.EMA(close, 30)
    ma = talib.MA(close, 30)
    wma = talib.WMA(close, timeperiod=30)
    tema = talib.TEMA(close, 30)
    obv = talib.OBV(close, np.asarray(volume, dtype='float'))
    adx = talib.ADX(high, low, close, 14)
    apo = talib.APO(close, 12, 2, 0)
    bop = talib.BOP(openn, high, low, close)
    mom = talib.MOM(close,10)
    ppo = talib.PPO(close, 12, 26, 0)
    slowk, slowd = talib.STOCH(high, low, close, fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0)
    ad = talib.AD(high, low, close, np.asarray(volume, dtype='float'))
    wcl = talib.WCLPRICE(high, low, close)

    #--- Preprocess data
    xdata = np.column_stack((close, diff, sma30, sma60, rsi, atr, macd, macdsignal, macdhist, lower, middle, upper, ema, ma, wma, adx, apo, bop, mom, ppo, slowk, slowd, trange, wcl))
    
    xdata = np.nan_to_num(xdata)
    if test == False:
        scaler = preprocessing.StandardScaler()
        xdata = np.expand_dims(scaler.fit_transform(xdata), axis=1)
        joblib.dump(scaler, 'data/scaler.pkl')
    elif test == True:
        scaler = joblib.load('data/scaler.pkl')
        xdata = np.expand_dims(scaler.fit_transform(xdata), axis=1)
    state = xdata[0:1, 0:1, :]

    return state, xdata, close
Пример #16
0
def wclprice(candles: np.ndarray, sequential: bool = False) -> Union[float, np.ndarray]:
    """
    WCLPRICE - Weighted Close Price

    :param candles: np.ndarray
    :param sequential: bool - default=False

    :return: float | np.ndarray
    """
    if not sequential and len(candles) > 240:
        candles = candles[-240:]

    res = talib.WCLPRICE(candles[:, 3], candles[:, 4], candles[:, 2])

    if sequential:
        return res
    else:
        return None if np.isnan(res[-1]) else res[-1]
Пример #17
0
def add_price_transform_indicators(data_list):
    for data in data_list:
        # 1) AVGPRICE - Average Price
        real = talib.AVGPRICE(data.Open, data.High, data.Low, data.Close)
        data['AVERAGE'] = real

        # 2) MEDPRICE - Median Price
        real = talib.MEDPRICE(data.High, data.Low)
        data['MEDPRICE'] = real

        # 3) TYPPRICE - Typical Price
        real = talib.TYPPRICE(data.High, data.Low, data.Close)
        data['TYPPRICE'] = real

        # 4) WCLPRICE - Weighted Close Price
        real = talib.WCLPRICE(data.High, data.Low, data.Close)
        data['WCLPRICE'] = real

    return data_list
Пример #18
0
def Price_Transform(dataframe):
	"""
	Price Transform

	AVGPRICE             Average Price
	MEDPRICE             Median Price
	TYPPRICE             Typical Price
	WCLPRICE             Weighted Close Price

	"""
	#Price Transform Functions
	#AVGPRICE - Average Price
	df[f'{ratio}_AVGPRICE'] = talib.AVGPRICE(Open, High, Low, Close)
	#MEDPRICE - Median Price
	df[f'{ratio}_MEDPRICE'] = talib.MEDPRICE(High, Low)
	#TYPPRICE - Typical Price
	df[f'{ratio}_TYPPRICE'] = talib.TYPPRICE(High, Low, Close)
	#WCLPRICE - Weighted Close Price
	df[f'{ratio}_WCLPRICE'] = talib.WCLPRICE(High, Low, Close)
	return
Пример #19
0
def wclprice(candles: np.ndarray,
             sequential: bool = False) -> Union[float, np.ndarray]:
    """
    WCLPRICE - Weighted Close Price

    :param candles: np.ndarray
    :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:]

    res = talib.WCLPRICE(candles[:, 3], candles[:, 4], candles[:, 2])

    if sequential:
        return res
    else:
        return None if np.isnan(res[-1]) else res[-1]
Пример #20
0
def handle_price_transform(args, kax, klines_df, close_times, display_count):

    os_key = 'AVGPRICE'
    if args.AVGPRICE:
        real = talib.AVGPRICE(klines_df["open"], klines_df["high"],
                              klines_df["low"], klines_df["close"])
        kax.plot(close_times, real[-display_count:], "y", label=os_key)

    os_key = 'MEDPRICE'
    if args.MEDPRICE:
        real = talib.MEDPRICE(klines_df["high"], klines_df["low"])
        kax.plot(close_times, real[-display_count:], "y", label=os_key)

    os_key = 'TYPPRICE'
    if args.TYPPRICE:
        real = talib.TYPPRICE(klines_df["high"], klines_df["low"],
                              klines_df["close"])
        kax.plot(close_times, real[-display_count:], "y", label=os_key)

    os_key = 'WCLPRICE'
    if args.WCLPRICE:
        real = talib.WCLPRICE(klines_df["high"], klines_df["low"],
                              klines_df["close"])
        kax.plot(close_times, real[-display_count:], "y", label=os_key)
Пример #21
0
def WCLPRICE(data, **kwargs):
    _check_talib_presence()
    popen, phigh, plow, pclose, pvolume = _extract_ohlc(data)
    return talib.WCLPRICE(phigh, plow, pclose, **kwargs)
Пример #22
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)
Пример #23
0
def main():
    ohlcv = api_ohlcv('20191017')
    open, high, low, close, volume, timestamp = [], [], [], [], [], []

    for i in ohlcv:
        open.append(int(i[0]))
        high.append(int(i[1]))
        low.append(int(i[2]))
        close.append(int(i[3]))
        volume.append(float(i[4]))
        time_str = str(i[5])
        timestamp.append(
            datetime.fromtimestamp(int(
                time_str[:10])).strftime('%Y/%m/%d %H:%M:%M'))

    date_time_index = pd.to_datetime(
        timestamp)  # convert to DateTimeIndex type
    df = pd.DataFrame(
        {
            'open': open,
            'high': high,
            'low': low,
            'close': close,
            'volume': volume
        },
        index=date_time_index)
    # df.index += pd.offsets.Hour(9) # adjustment for JST if required
    print(df.shape)
    print(df.columns)

    # pct_change
    f = lambda x: 1 if x > 0.0001 else -1 if x < -0.0001 else 0 if -0.0001 <= x <= 0.0001 else np.nan
    y = df.rename(columns={
        'close': 'y'
    }).loc[:, 'y'].pct_change(1).shift(-1).fillna(0)
    X = df.copy()
    y_ = pd.DataFrame(y.map(f), columns=['y'])
    y = df.rename(columns={'close': 'y'}).loc[:, 'y'].pct_change(1).fillna(0)
    df_ = pd.concat([X, y_], axis=1)

    # check the shape
    print(
        '----------------------------------------------------------------------------------------'
    )
    print('X shape: (%i,%i)' % X.shape)
    print('y shape: (%i,%i)' % y_.shape)
    print(
        '----------------------------------------------------------------------------------------'
    )
    print(y_.groupby('y').size())
    print('y=1 up, y=0 stay, y=-1 down')
    print(
        '----------------------------------------------------------------------------------------'
    )

    # feature calculation
    open = pd.Series(df['open'])
    high = pd.Series(df['high'])
    low = pd.Series(df['low'])
    close = pd.Series(df['close'])
    volume = pd.Series(df['volume'])

    # pct_change for new column
    X['diff'] = y

    # Exponential Moving Average
    ema = talib.EMA(close, timeperiod=3)
    ema = ema.fillna(ema.mean())

    # Momentum
    momentum = talib.MOM(close, timeperiod=5)
    momentum = momentum.fillna(momentum.mean())

    # RSI
    rsi = talib.RSI(close, timeperiod=14)
    rsi = rsi.fillna(rsi.mean())

    # ADX
    adx = talib.ADX(high, low, close, timeperiod=14)
    adx = adx.fillna(adx.mean())

    # ADX change
    adx_change = adx.pct_change(1).shift(-1)
    adx_change = adx_change.fillna(adx_change.mean())

    # AD
    ad = talib.AD(high, low, close, volume)
    ad = ad.fillna(ad.mean())

    X_ = pd.concat([X, ema, momentum, rsi, adx_change, ad],
                   axis=1).drop(['open', 'high', 'low', 'close'], axis=1)
    X_.columns = ['volume', 'diff', 'ema', 'momentum', 'rsi', 'adx', 'ad']
    X_.join(y_).head(10)

    # default parameter models
    X_train, X_test, y_train, y_test = train_test_split(X_,
                                                        y_,
                                                        test_size=0.33,
                                                        random_state=42)
    print('X_train shape: {}'.format(X_train.shape))
    print('X_test shape: {}'.format(X_test.shape))
    print('y_train shape: {}'.format(y_train.shape))
    print('y_test shape: {}'.format(y_test.shape))

    pipe_knn = Pipeline([('scl', StandardScaler()),
                         ('est', KNeighborsClassifier(n_neighbors=3))])
    pipe_logistic = Pipeline([('scl', StandardScaler()),
                              ('est',
                               LogisticRegression(solver='lbfgs',
                                                  multi_class='multinomial',
                                                  random_state=39))])
    pipe_rf = Pipeline([('scl', StandardScaler()),
                        ('est', RandomForestClassifier(random_state=39))])
    pipe_gb = Pipeline([('scl', StandardScaler()),
                        ('est', GradientBoostingClassifier(random_state=39))])

    pipe_names = ['KNN', 'Logistic', 'RandomForest', 'GradientBoosting']
    pipe_lines = [pipe_knn, pipe_logistic, pipe_rf, pipe_gb]

    for (i, pipe) in enumerate(pipe_lines):
        pipe.fit(X_train, y_train.values.ravel())
        print(pipe)
        print('%s: %.3f' %
              (pipe_names[i] + ' Train Accuracy',
               accuracy_score(y_train.values.ravel(), pipe.predict(X_train))))
        print('%s: %.3f' %
              (pipe_names[i] + ' Test Accuracy',
               accuracy_score(y_test.values.ravel(), pipe.predict(X_test))))
        print('%s: %.3f' % (pipe_names[i] + ' Train F1 Score',
                            f1_score(y_train.values.ravel(),
                                     pipe.predict(X_train),
                                     average='micro')))
        print('%s: %.3f' % (pipe_names[i] + ' Test F1 Score',
                            f1_score(y_test.values.ravel(),
                                     pipe.predict(X_test),
                                     average='micro')))

    for (i, pipe) in enumerate(pipe_lines):
        predict = pipe.predict(X_test)
        cm = confusion_matrix(y_test.values.ravel(),
                              predict,
                              labels=[-1, 0, 1])
        print('{} Confusion Matrix'.format(pipe_names[i]))
        print(cm)

    ## Overlap Studies Functions
    # DEMA - Double Exponential Moving Average
    dema = talib.DEMA(close, timeperiod=3)
    dema = dema.fillna(dema.mean())
    print('DEMA - Double Exponential Moving Average shape: {}'.format(
        dema.shape))

    # EMA - Exponential Moving Average
    ema = talib.EMA(close, timeperiod=3)
    ema = ema.fillna(ema.mean())
    print('EMA - Exponential Moving Average shape: {}'.format(ema.shape))

    # HT_TRENDLINE - Hilbert Transform - Instantaneous Trendline
    hilbert = talib.HT_TRENDLINE(close)
    hilbert = hilbert.fillna(hilbert.mean())
    print(
        'HT_TRENDLINE - Hilbert Transform - Instantaneous Trendline shape: {}'.
        format(hilbert.shape))

    # KAMA - Kaufman Adaptive Moving Average
    kama = talib.KAMA(close, timeperiod=3)
    kama = kama.fillna(kama.mean())
    print('KAMA - Kaufman Adaptive Moving Average shape: {}'.format(
        kama.shape))

    # MA - Moving average
    ma = talib.MA(close, timeperiod=3, matype=0)
    ma = ma.fillna(ma.mean())
    print('MA - Moving average shape: {}'.format(kama.shape))

    # MIDPOINT - MidPoint over period
    midpoint = talib.MIDPOINT(close, timeperiod=7)
    midpoint = midpoint.fillna(midpoint.mean())
    print('MIDPOINT - MidPoint over period shape: {}'.format(midpoint.shape))

    # MIDPRICE - Midpoint Price over period
    midprice = talib.MIDPRICE(high, low, timeperiod=7)
    midprice = midprice.fillna(midprice.mean())
    print('MIDPRICE - Midpoint Price over period shape: {}'.format(
        midprice.shape))

    # SAR - Parabolic SAR
    sar = talib.SAR(high, low, acceleration=0, maximum=0)
    sar = sar.fillna(sar.mean())
    print('SAR - Parabolic SAR shape: {}'.format(sar.shape))

    # SAREXT - Parabolic SAR - Extended
    sarext = talib.SAREXT(high,
                          low,
                          startvalue=0,
                          offsetonreverse=0,
                          accelerationinitlong=0,
                          accelerationlong=0,
                          accelerationmaxlong=0,
                          accelerationinitshort=0,
                          accelerationshort=0,
                          accelerationmaxshort=0)
    sarext = sarext.fillna(sarext.mean())
    print('SAREXT - Parabolic SAR - Extended shape: {}'.format(sarext.shape))

    # SMA - Simple Moving Average
    sma = talib.SMA(close, timeperiod=3)
    sma = sma.fillna(sma.mean())
    print('SMA - Simple Moving Average shape: {}'.format(sma.shape))

    # T3 - Triple Exponential Moving Average (T3)
    t3 = talib.T3(close, timeperiod=5, vfactor=0)
    t3 = t3.fillna(t3.mean())
    print('T3 - Triple Exponential Moving Average shape: {}'.format(t3.shape))

    # TEMA - Triple Exponential Moving Average
    tema = talib.TEMA(close, timeperiod=3)
    tema = tema.fillna(tema.mean())
    print('TEMA - Triple Exponential Moving Average shape: {}'.format(
        tema.shape))

    # TRIMA - Triangular Moving Average
    trima = talib.TRIMA(close, timeperiod=3)
    trima = trima.fillna(trima.mean())
    print('TRIMA - Triangular Moving Average shape: {}'.format(trima.shape))

    # WMA - Weighted Moving Average
    wma = talib.WMA(close, timeperiod=3)
    wma = wma.fillna(wma.mean())
    print('WMA - Weighted Moving Average shape: {}'.format(wma.shape))

    ## Momentum Indicator Functions
    # ADX - Average Directional Movement Index
    adx = talib.ADX(high, low, close, timeperiod=14)
    adx = adx.fillna(adx.mean())
    print('ADX - Average Directional Movement Index shape: {}'.format(
        adx.shape))

    # ADXR - Average Directional Movement Index Rating
    adxr = talib.ADXR(high, low, close, timeperiod=7)
    adxr = adxr.fillna(adxr.mean())
    print('ADXR - Average Directional Movement Index Rating shape: {}'.format(
        adxr.shape))

    # APO - Absolute Price Oscillator
    apo = talib.APO(close, fastperiod=12, slowperiod=26, matype=0)
    apo = apo.fillna(apo.mean())
    print('APO - Absolute Price Oscillator shape: {}'.format(apo.shape))

    # AROONOSC - Aroon Oscillator
    aroon = talib.AROONOSC(high, low, timeperiod=14)
    aroon = aroon.fillna(aroon.mean())
    print('AROONOSC - Aroon Oscillator shape: {}'.format(apo.shape))

    # BOP - Balance Of Power
    bop = talib.BOP(open, high, low, close)
    bop = bop.fillna(bop.mean())
    print('BOP - Balance Of Power shape: {}'.format(apo.shape))

    # CCI - Commodity Channel Index
    cci = talib.CCI(high, low, close, timeperiod=7)
    cci = cci.fillna(cci.mean())
    print('CCI - Commodity Channel Index shape: {}'.format(cci.shape))

    # CMO - Chande Momentum Oscillator
    cmo = talib.CMO(close, timeperiod=7)
    cmo = cmo.fillna(cmo.mean())
    print('CMO - Chande Momentum Oscillator shape: {}'.format(cmo.shape))

    # DX - Directional Movement Index
    dx = talib.DX(high, low, close, timeperiod=7)
    dx = dx.fillna(dx.mean())
    print('DX - Directional Movement Index shape: {}'.format(dx.shape))

    # MFI - Money Flow Index
    mfi = talib.MFI(high, low, close, volume, timeperiod=7)
    mfi = mfi.fillna(mfi.mean())
    print('MFI - Money Flow Index shape: {}'.format(mfi.shape))

    # MINUS_DI - Minus Directional Indicator
    minusdi = talib.MINUS_DI(high, low, close, timeperiod=14)
    minusdi = minusdi.fillna(minusdi.mean())
    print('MINUS_DI - Minus Directional Indicator shape: {}'.format(
        minusdi.shape))

    # MINUS_DM - Minus Directional Movement
    minusdm = talib.MINUS_DM(high, low, timeperiod=14)
    minusdm = minusdm.fillna(minusdm.mean())
    print('MINUS_DM - Minus Directional Movement shape: {}'.format(
        minusdm.shape))

    # MOM - Momentum
    mom = talib.MOM(close, timeperiod=5)
    mom = mom.fillna(mom.mean())
    print('MOM - Momentum shape: {}'.format(mom.shape))

    # PLUS_DI - Plus Directional Indicator
    plusdi = talib.PLUS_DI(high, low, close, timeperiod=14)
    plusdi = plusdi.fillna(plusdi.mean())
    print('PLUS_DI - Plus Directional Indicator shape: {}'.format(
        plusdi.shape))

    # PLUS_DM - Plus Directional Movement
    plusdm = talib.PLUS_DM(high, low, timeperiod=14)
    plusdm = plusdm.fillna(plusdm.mean())
    print('PLUS_DM - Plus Directional Movement shape: {}'.format(plusdi.shape))

    # PPO - Percentage Price Oscillator
    ppo = talib.PPO(close, fastperiod=12, slowperiod=26, matype=0)
    ppo = ppo.fillna(ppo.mean())
    print('PPO - Percentage Price Oscillator shape: {}'.format(ppo.shape))

    # ROC - Rate of change:((price/prevPrice)-1)*100
    roc = talib.ROC(close, timeperiod=10)
    roc = roc.fillna(roc.mean())
    print('ROC - Rate of change : ((price/prevPrice)-1)*100 shape: {}'.format(
        roc.shape))

    # RSI - Relative Strength Index
    rsi = talib.RSI(close, timeperiod=14)
    rsi = rsi.fillna(rsi.mean())
    print('RSI - Relative Strength Index shape: {}'.format(rsi.shape))

    # TRIX - 1-day Rate-Of-Change (ROC) of a Triple Smooth EMA
    trix = talib.TRIX(close, timeperiod=30)
    trix = trix.fillna(trix.mean())
    print('TRIX - 1-day Rate-Of-Change (ROC) of a Triple Smooth EMA shape: {}'.
          format(trix.shape))

    # ULTOSC - Ultimate Oscillator
    ultosc = talib.ULTOSC(high,
                          low,
                          close,
                          timeperiod1=7,
                          timeperiod2=14,
                          timeperiod3=28)
    ultosc = ultosc.fillna(ultosc.mean())
    print('ULTOSC - Ultimate Oscillator shape: {}'.format(ultosc.shape))

    # WILLR - Williams'%R
    willr = talib.WILLR(high, low, close, timeperiod=7)
    willr = willr.fillna(willr.mean())
    print("WILLR - Williams'%R shape: {}".format(willr.shape))

    ## Volume Indicator Functions
    # AD - Chaikin A/D Line
    ad = talib.AD(high, low, close, volume)
    ad = ad.fillna(ad.mean())
    print('AD - Chaikin A/D Line shape: {}'.format(ad.shape))

    # ADOSC - Chaikin A/D Oscillator
    adosc = talib.ADOSC(high, low, close, volume, fastperiod=3, slowperiod=10)
    adosc = adosc.fillna(adosc.mean())
    print('ADOSC - Chaikin A/D Oscillator shape: {}'.format(adosc.shape))

    # OBV - On Balance Volume
    obv = talib.OBV(close, volume)
    obv = obv.fillna(obv.mean())
    print('OBV - On Balance Volume shape: {}'.format(obv.shape))

    ## Volatility Indicator Functions
    # ATR - Average True Range
    atr = talib.ATR(high, low, close, timeperiod=7)
    atr = atr.fillna(atr.mean())
    print('ATR - Average True Range shape: {}'.format(atr.shape))

    # NATR - Normalized Average True Range
    natr = talib.NATR(high, low, close, timeperiod=7)
    natr = natr.fillna(natr.mean())
    print('NATR - Normalized Average True Range shape: {}'.format(natr.shape))

    # TRANGE - True Range
    trange = talib.TRANGE(high, low, close)
    trange = trange.fillna(trange.mean())
    print('TRANGE - True Range shape: {}'.format(natr.shape))

    ## Price Transform Functions
    # AVGPRICE - Average Price
    avg = talib.AVGPRICE(open, high, low, close)
    avg = avg.fillna(avg.mean())
    print('AVGPRICE - Average Price shape: {}'.format(natr.shape))

    # MEDPRICE - Median Price
    medprice = talib.MEDPRICE(high, low)
    medprice = medprice.fillna(medprice.mean())
    print('MEDPRICE - Median Price shape: {}'.format(medprice.shape))

    # TYPPRICE - Typical Price
    typ = talib.TYPPRICE(high, low, close)
    typ = typ.fillna(typ.mean())
    print('TYPPRICE - Typical Price shape: {}'.format(typ.shape))

    # WCLPRICE - Weighted Close Price
    wcl = talib.WCLPRICE(high, low, close)
    wcl = wcl.fillna(wcl.mean())
    print('WCLPRICE - Weighted Close Price shape: {}'.format(wcl.shape))

    ## Cycle Indicator Functions
    # HT_DCPERIOD - Hilbert Transform - Dominant Cycle Period
    dcperiod = talib.HT_DCPERIOD(close)
    dcperiod = dcperiod.fillna(dcperiod.mean())
    print('HT_DCPERIOD - Hilbert Transform - Dominant Cycle Period shape: {}'.
          format(dcperiod.shape))

    # HT_DCPHASE - Hilbert Transform - Dominant Cycle Phase
    dcphase = talib.HT_DCPHASE(close)
    dcphase = dcphase.fillna(dcphase.mean())
    print('HT_DCPHASE - Hilbert Transform - Dominant Cycle Phase shape: {}'.
          format(dcperiod.shape))

    ## Statistic Functions
    # BETA - Beta
    beta = talib.BETA(high, low, timeperiod=3)
    beta = beta.fillna(beta.mean())
    print('BETA - Beta shape: {}'.format(beta.shape))

    # CORREL - Pearson's Correlation Coefficient(r)
    correl = talib.CORREL(high, low, timeperiod=30)
    correl = correl.fillna(correl.mean())
    print("CORREL - Pearson's Correlation Coefficient(r) shape: {}".format(
        beta.shape))

    # LINEARREG - Linear Regression
    linreg = talib.LINEARREG(close, timeperiod=7)
    linreg = linreg.fillna(linreg.mean())
    print("LINEARREG - Linear Regression shape: {}".format(linreg.shape))

    # STDDEV - Standard Deviation
    stddev = talib.STDDEV(close, timeperiod=5, nbdev=1)
    stddev = stddev.fillna(stddev.mean())
    print("STDDEV - Standard Deviation shape: {}".format(stddev.shape))

    # TSF - Time Series Forecast
    tsf = talib.TSF(close, timeperiod=7)
    tsf = tsf.fillna(tsf.mean())
    print("TSF - Time Series Forecast shape: {}".format(tsf.shape))

    # VAR - Variance
    var = talib.VAR(close, timeperiod=5, nbdev=1)
    var = var.fillna(var.mean())
    print("VAR - Variance shape: {}".format(var.shape))

    ## Feature DataFrame
    X_full = pd.concat([
        X, dema, ema, hilbert, kama, ma, midpoint, midprice, sar, sarext, sma,
        t3, tema, trima, wma, adx, adxr, apo, aroon, bop, cci, cmo, mfi,
        minusdi, minusdm, mom, plusdi, plusdm, ppo, roc, rsi, trix, ultosc,
        willr, ad, adosc, obv, atr, natr, trange, avg, medprice, typ, wcl,
        dcperiod, dcphase, beta, correl, linreg, stddev, tsf, var
    ],
                       axis=1).drop(['open', 'high', 'low', 'close'], axis=1)
    X_full.columns = [
        'volume', 'diff', 'dema', 'ema', 'hilbert', 'kama', 'ma', 'midpoint',
        'midprice', 'sar', 'sarext', 'sma', 't3', 'tema', 'trima', 'wma',
        'adx', 'adxr', 'apo', 'aroon', 'bop', 'cci', 'cmo', 'mfi', 'minusdi',
        'minusdm', 'mom', 'plusdi', 'plusdm', 'ppo', 'roc', 'rsi', 'trix',
        'ultosc', 'willr', 'ad', 'adosc', 'obv', 'atr', 'natr', 'trange',
        'avg', 'medprice', 'typ', 'wcl', 'dcperiod', 'dcphase', 'beta',
        'correl', 'linreg', 'stddev', 'tsf', 'var'
    ]
    X_full.join(y_).head(10)

    # full feature models
    X_train_full, X_test_full, y_train_full, y_test_full = train_test_split(
        X_full, y_, test_size=0.33, random_state=42)
    print('X_train shape: {}'.format(X_train_full.shape))
    print('X_test shape: {}'.format(X_test_full.shape))
    print('y_train shape: {}'.format(y_train_full.shape))
    print('y_test shape: {}'.format(y_test_full.shape))

    pipe_knn_full = Pipeline([('scl', StandardScaler()),
                              ('est', KNeighborsClassifier(n_neighbors=3))])
    pipe_logistic_full = Pipeline([
        ('scl', StandardScaler()),
        ('est',
         LogisticRegression(solver='lbfgs',
                            multi_class='multinomial',
                            random_state=39))
    ])
    pipe_rf_full = Pipeline([('scl', StandardScaler()),
                             ('est', RandomForestClassifier(random_state=39))])
    pipe_gb_full = Pipeline([('scl', StandardScaler()),
                             ('est',
                              GradientBoostingClassifier(random_state=39))])

    pipe_names = ['KNN', 'Logistic', 'RandomForest', 'GradientBoosting']
    pipe_lines_full = [
        pipe_knn_full, pipe_logistic_full, pipe_rf_full, pipe_gb_full
    ]

    for (i, pipe) in enumerate(pipe_lines_full):
        pipe.fit(X_train_full, y_train_full.values.ravel())
        print(pipe)
        print('%s: %.3f' % (pipe_names[i] + ' Train Accuracy',
                            accuracy_score(y_train_full.values.ravel(),
                                           pipe.predict(X_train_full))))
        print('%s: %.3f' % (pipe_names[i] + ' Test Accuracy',
                            accuracy_score(y_test_full.values.ravel(),
                                           pipe.predict(X_test_full))))
        print('%s: %.3f' % (pipe_names[i] + ' Train F1 Score',
                            f1_score(y_train_full.values.ravel(),
                                     pipe.predict(X_train_full),
                                     average='micro')))
        print('%s: %.3f' % (pipe_names[i] + ' Test F1 Score',
                            f1_score(y_test_full.values.ravel(),
                                     pipe.predict(X_test_full),
                                     average='micro')))

    # Univariate Statistics
    select = SelectPercentile(percentile=25)
    select.fit(X_train_full, y_train_full.values.ravel())
    X_train_selected = select.transform(X_train_full)
    X_test_selected = select.transform(X_test_full)
    # GradientBoost Classifier
    print(
        '--------------------------Without Univariate Statistics-------------------------------------'
    )
    pipe_gb = Pipeline([('scl', StandardScaler()),
                        ('est', GradientBoostingClassifier(random_state=39))])
    pipe_gb.fit(X_train_full, y_train_full.values.ravel())
    print('Train Accuracy: {:.3f}'.format(
        accuracy_score(y_train_full.values.ravel(),
                       pipe_gb.predict(X_train_full))))
    print('Test Accuracy: {:.3f}'.format(
        accuracy_score(y_test_full.values.ravel(),
                       pipe_gb.predict(X_test_full))))
    print('Train F1 Score: {:.3f}'.format(
        f1_score(y_train_full.values.ravel(),
                 pipe_gb.predict(X_train_full),
                 average='micro')))
    print('Test F1 Score: {:.3f}'.format(
        f1_score(y_test_full.values.ravel(),
                 pipe_gb.predict(X_test_full),
                 average='micro')))
    # GradientBoost Cllassifier with Univariate Statistics
    print(
        '---------------------------With Univariate Statistics--------------------------------------'
    )
    pipe_gb_percentile = Pipeline([
        ('scl', StandardScaler()),
        ('est', GradientBoostingClassifier(random_state=39))
    ])
    pipe_gb_percentile.fit(X_train_selected, y_train_full.values.ravel())
    print('Train Accuracy: {:.3f}'.format(
        accuracy_score(y_train_full.values.ravel(),
                       pipe_gb_percentile.predict(X_train_selected))))
    print('Test Accuracy: {:.3f}'.format(
        accuracy_score(y_test_full.values.ravel(),
                       pipe_gb_percentile.predict(X_test_selected))))
    print('Train F1 Score: {:.3f}'.format(
        f1_score(y_train_full.values.ravel(),
                 pipe_gb_percentile.predict(X_train_selected),
                 average='micro')))
    print('Test F1 Score: {:.3f}'.format(
        f1_score(y_test_full.values.ravel(),
                 pipe_gb_percentile.predict(X_test_selected),
                 average='micro')))

    # Model-based Selection
    select = SelectFromModel(RandomForestClassifier(n_estimators=100,
                                                    random_state=42),
                             threshold="1.25*mean")
    select.fit(X_train_full, y_train_full.values.ravel())
    X_train_model = select.transform(X_train_full)
    X_test_model = select.transform(X_test_full)
    # GradientBoost Classifier
    print(
        '--------------------------Without Model-based Selection--------------------------------------'
    )
    pipe_gb = Pipeline([('scl', StandardScaler()),
                        ('est', GradientBoostingClassifier(random_state=39))])
    pipe_gb.fit(X_train_full, y_train_full.values.ravel())
    print('Train Accuracy: {:.3f}'.format(
        accuracy_score(y_train_full.values.ravel(),
                       pipe_gb.predict(X_train_full))))
    print('Test Accuracy: {:.3f}'.format(
        accuracy_score(y_test_full.values.ravel(),
                       pipe_gb.predict(X_test_full))))
    print('Train F1 Score: {:.3f}'.format(
        f1_score(y_train_full.values.ravel(),
                 pipe_gb.predict(X_train_full),
                 average='micro')))
    print('Test F1 Score: {:.3f}'.format(
        f1_score(y_test_full.values.ravel(),
                 pipe_gb.predict(X_test_full),
                 average='micro')))
    # GradientBoost Classifier with Model-based Selection
    print(
        '----------------------------With Model-based Selection--------------------------------------'
    )
    pipe_gb_model = Pipeline([('scl', StandardScaler()),
                              ('est',
                               GradientBoostingClassifier(random_state=39))])
    pipe_gb_model.fit(X_train_model, y_train_full.values.ravel())
    print('Train Accuracy: {:.3f}'.format(
        accuracy_score(y_train_full.values.ravel(),
                       pipe_gb_model.predict(X_train_model))))
    print('Test Accuracy: {:.3f}'.format(
        accuracy_score(y_test_full.values.ravel(),
                       pipe_gb_model.predict(X_test_model))))
    print('Train F1 Score: {:.3f}'.format(
        f1_score(y_train_full.values.ravel(),
                 pipe_gb_model.predict(X_train_model),
                 average='micro')))
    print('Test F1 Score: {:.3f}'.format(
        f1_score(y_test_full.values.ravel(),
                 pipe_gb_model.predict(X_test_model),
                 average='micro')))

    # Recursive Feature Elimination
    select = RFE(RandomForestClassifier(n_estimators=100, random_state=42),
                 n_features_to_select=15)
    select.fit(X_train_full, y_train_full.values.ravel())
    X_train_rfe = select.transform(X_train_full)
    X_test_rfe = select.transform(X_test_full)
    # GradientBoost Classifier
    print(
        '--------------------------Without Recursive Feature Elimination-------------------------------------'
    )
    pipe_gb = Pipeline([('scl', StandardScaler()),
                        ('est', GradientBoostingClassifier(random_state=39))])
    pipe_gb.fit(X_train_full, y_train_full.values.ravel())
    print('Train Accuracy: {:.3f}'.format(
        accuracy_score(y_train_full.values.ravel(),
                       pipe_gb.predict(X_train_full))))
    print('Test Accuracy: {:.3f}'.format(
        accuracy_score(y_test_full.values.ravel(),
                       pipe_gb.predict(X_test_full))))
    print('Train F1 Score: {:.3f}'.format(
        f1_score(y_train_full.values.ravel(),
                 pipe_gb.predict(X_train_full),
                 average='micro')))
    print('Test F1 Score: {:.3f}'.format(
        f1_score(y_test_full.values.ravel(),
                 pipe_gb.predict(X_test_full),
                 average='micro')))
    # GradientBoost Classifier with Recursive Feature Elimination
    print(
        '----------------------------With Recursive Feature Elimination--------------------------------------'
    )
    pipe_gb_rfe = Pipeline([('scl', StandardScaler()),
                            ('est',
                             GradientBoostingClassifier(random_state=39))])
    pipe_gb_rfe.fit(X_train_rfe, y_train_full.values.ravel())
    print('Train Accuracy: {:.3f}'.format(
        accuracy_score(y_train_full.values.ravel(),
                       pipe_gb_rfe.predict(X_train_rfe))))
    print('Test Accuracy: {:.3f}'.format(
        accuracy_score(y_test_full.values.ravel(),
                       pipe_gb_rfe.predict(X_test_rfe))))
    print('Train F1 Score: {:.3f}'.format(
        f1_score(y_train_full.values.ravel(),
                 pipe_gb_rfe.predict(X_train_rfe),
                 average='micro')))
    print('Test F1 Score: {:.3f}'.format(
        f1_score(y_test_full.values.ravel(),
                 pipe_gb_rfe.predict(X_test_rfe),
                 average='micro')))

    cv = cross_val_score(pipe_gb,
                         X_,
                         y_.values.ravel(),
                         cv=StratifiedKFold(n_splits=10,
                                            shuffle=True,
                                            random_state=39))
    print('Cross validation with StratifiedKFold scores: {}'.format(cv))
    print('Cross Validation with StatifiedKFold mean: {}'.format(cv.mean()))

    # GridSearch
    n_features = len(df.columns)
    param_grid = {
        'learning_rate': [0.01, 0.1, 1, 10],
        'n_estimators': [1, 10, 100, 200, 300],
        'max_depth': [1, 2, 3, 4, 5]
    }
    stratifiedcv = StratifiedKFold(n_splits=10, shuffle=True, random_state=39)
    X_train, X_test, y_train, y_test = train_test_split(X_,
                                                        y_,
                                                        test_size=0.33,
                                                        random_state=42)

    grid_search = GridSearchCV(GradientBoostingClassifier(),
                               param_grid,
                               cv=stratifiedcv)
    grid_search.fit(X_train, y_train.values.ravel())
    print('GridSearch Train Accuracy: {:.3f}'.format(
        accuracy_score(y_train.values.ravel(), grid_search.predict(X_train))))
    print('GridSearch Test Accuracy: {:.3f}'.format(
        accuracy_score(y_test.values.ravel(), grid_search.predict(X_test))))
    print('GridSearch Train F1 Score: {:.3f}'.format(
        f1_score(y_train.values.ravel(),
                 grid_search.predict(X_train),
                 average='micro')))
    print('GridSearch Test F1 Score: {:.3f}'.format(
        f1_score(y_test.values.ravel(),
                 grid_search.predict(X_test),
                 average='micro')))

    # GridSearch results
    print("Best params:\n{}".format(grid_search.best_params_))
    print("Best cross-validation score: {:.2f}".format(
        grid_search.best_score_))
    results = pd.DataFrame(grid_search.cv_results_)
    corr_params = results.drop(results.columns[[
        0, 1, 2, 3, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 19, 20
    ]],
                               axis=1)
    corr_params.head()

    # GridSearch in nested
    cv_gb = cross_val_score(grid_search,
                            X_,
                            y_.values.ravel(),
                            cv=StratifiedKFold(n_splits=3,
                                               shuffle=True,
                                               random_state=39))
    print('Grid Search with nested cross validation scores: {}'.format(cv_gb))
    print('Grid Search with nested cross validation mean: {}'.format(
        cv_gb.mean()))
Пример #24
0
def TALIB_WCLPRICE(close):
    '''00395,1,1'''
    return talib.WCLPRICE(close)
Пример #25
0
def get_datasets(asset, currency, granularity, datapoints):
    """Fetch the API and precess the desired pair

    Arguments:
        asset {str} -- First pair
        currency {str} -- Second pair
        granularity {str ['day', 'hour']} -- Granularity
        datapoints {int [100 - 2000]} -- [description]

    Returns:
        pandas.Dataframe -- The OHLCV and indicators dataframe
    """
    df_train_path = 'datasets/bot_train_{}_{}_{}.csv'.format(
        asset + currency, datapoints, granularity)
    df_rollout_path = 'datasets/bot_rollout_{}_{}_{}.csv'.format(
        asset + currency, datapoints, granularity)
    emojis = [
        ':moneybag:', ':yen:', ':dollar:', ':pound:', ':euro:',
        ':credit_card:', ':money_with_wings:', ':gem:'
    ]

    if not os.path.exists(df_rollout_path):
        headers = {
            'User-Agent':
            'Mozilla/5.0',
            'authorization':
            'Apikey 3d7d3e9e6006669ac00584978342451c95c3c78421268ff7aeef69995f9a09ce'
        }

        # OHLC
        # url = 'https://min-api.cryptocompare.com/data/histo{}?fsym={}&tsym={}&e=Binance&limit={}'.format(granularity, asset, currency, datapoints)
        url = 'https://min-api.cryptocompare.com/data/histo{}?fsym={}&tsym={}&limit={}'.format(
            granularity, asset, currency, datapoints)
        # print(emoji.emojize(':dizzy: :large_blue_diamond: :gem: :bar_chart: :crystal_ball: :chart_with_downwards_trend: :chart_with_upwards_trend: :large_orange_diamond: loading...', use_aliases=True))
        print(
            colored(
                emoji.emojize('> ' + random.choice(emojis) + ' downloading ' +
                              asset + '/' + currency,
                              use_aliases=True), 'green'))
        # print(colored('> downloading ' + asset + '/' + currency, 'green'))
        response = requests.get(url, headers=headers)
        json_response = response.json()
        status = json_response['Response']
        if status == "Error":
            print(colored('=== {} ==='.format(json_response['Message']),
                          'red'))
            raise AssertionError()
        result = json_response['Data']
        df = pd.DataFrame(result)
        print(df.tail())
        df['Date'] = pd.to_datetime(df['time'], utc=True, unit='s')
        df.drop('time', axis=1, inplace=True)

        # indicators
        # https://github.com/mrjbq7/ta-lib/blob/master/docs/func.md
        open_price, high, low, close = np.array(df['open']), np.array(
            df['high']), np.array(df['low']), np.array(df['close'])
        volume = np.array(df['volumefrom'])
        # cycle indicators
        df.loc[:, 'HT_DCPERIOD'] = talib.HT_DCPERIOD(close)
        df.loc[:, 'HT_DCPHASE'] = talib.HT_DCPHASE(close)
        df.loc[:,
               'HT_PHASOR_inphase'], df.loc[:,
                                            'HT_PHASOR_quadrature'] = talib.HT_PHASOR(
                                                close)
        df.loc[:, 'HT_SINE_sine'], df.loc[:,
                                          'HT_SINE_leadsine'] = talib.HT_SINE(
                                              close)
        df.loc[:, 'HT_TRENDMODE'] = talib.HT_TRENDMODE(close)
        # momemtum indicators
        df.loc[:, 'ADX'] = talib.ADX(high, low, close, timeperiod=12)
        df.loc[:, 'ADXR'] = talib.ADXR(high, low, close, timeperiod=13)
        df.loc[:, 'APO'] = talib.APO(close,
                                     fastperiod=5,
                                     slowperiod=10,
                                     matype=0)
        df.loc[:,
               'AROON_down'], df.loc[:,
                                     'AROON_up'] = talib.AROON(high,
                                                               low,
                                                               timeperiod=15)
        df.loc[:, 'AROONOSC'] = talib.AROONOSC(high, low, timeperiod=13)
        df.loc[:, 'BOP'] = talib.BOP(open_price, high, low, close)
        df.loc[:, 'CCI'] = talib.CCI(high, low, close, timeperiod=13)
        df.loc[:, 'CMO'] = talib.CMO(close, timeperiod=14)
        df.loc[:, 'DX'] = talib.DX(high, low, close, timeperiod=10)
        df['MACD'], df['MACD_signal'], df['MACD_hist'] = talib.MACD(
            close, fastperiod=5, slowperiod=10, signalperiod=20)
        df.loc[:, 'MFI'] = talib.MFI(high, low, close, volume, timeperiod=12)
        df.loc[:, 'MINUS_DI'] = talib.MINUS_DI(high, low, close, timeperiod=10)
        df.loc[:, 'MINUS_DM'] = talib.MINUS_DM(high, low, timeperiod=14)
        df.loc[:, 'MOM'] = talib.MOM(close, timeperiod=20)
        df.loc[:, 'PPO'] = talib.PPO(close,
                                     fastperiod=17,
                                     slowperiod=35,
                                     matype=2)
        df.loc[:, 'ROC'] = talib.ROC(close, timeperiod=12)
        df.loc[:, 'RSI'] = talib.RSI(close, timeperiod=25)
        df.loc[:, 'STOCH_k'], df.loc[:,
                                     'STOCH_d'] = talib.STOCH(high,
                                                              low,
                                                              close,
                                                              fastk_period=35,
                                                              slowk_period=12,
                                                              slowk_matype=0,
                                                              slowd_period=7,
                                                              slowd_matype=0)
        df.loc[:,
               'STOCHF_k'], df.loc[:,
                                   'STOCHF_d'] = talib.STOCHF(high,
                                                              low,
                                                              close,
                                                              fastk_period=28,
                                                              fastd_period=14,
                                                              fastd_matype=0)
        df.loc[:, 'STOCHRSI_K'], df.loc[:, 'STOCHRSI_D'] = talib.STOCHRSI(
            close,
            timeperiod=35,
            fastk_period=12,
            fastd_period=10,
            fastd_matype=1)
        df.loc[:, 'TRIX'] = talib.TRIX(close, timeperiod=30)
        df.loc[:, 'ULTOSC'] = talib.ULTOSC(high,
                                           low,
                                           close,
                                           timeperiod1=14,
                                           timeperiod2=28,
                                           timeperiod3=35)
        df.loc[:, 'WILLR'] = talib.WILLR(high, low, close, timeperiod=35)
        # overlap studies
        df.loc[:,
               'BBANDS_upper'], df.loc[:,
                                       'BBANDS_middle'], df.loc[:,
                                                                'BBANDS_lower'] = talib.BBANDS(
                                                                    close,
                                                                    timeperiod=
                                                                    12,
                                                                    nbdevup=2,
                                                                    nbdevdn=2,
                                                                    matype=0)
        df.loc[:, 'DEMA'] = talib.DEMA(close, timeperiod=30)
        df.loc[:, 'EMA'] = talib.EMA(close, timeperiod=7)
        df.loc[:, 'HT_TRENDLINE'] = talib.HT_TRENDLINE(close)
        df.loc[:, 'KAMA'] = talib.KAMA(close, timeperiod=5)
        df.loc[:, 'MA'] = talib.MA(close, timeperiod=5, matype=0)
        df.loc[:, 'MIDPOINT'] = talib.MIDPOINT(close, timeperiod=20)
        df.loc[:, 'WMA'] = talib.WMA(close, timeperiod=15)
        df.loc[:, 'SMA'] = talib.SMA(close)
        # pattern recoginition
        df.loc[:, 'CDL2CROWS'] = talib.CDL2CROWS(open_price, high, low, close)
        df.loc[:, 'CDL3BLACKCROWS'] = talib.CDL3BLACKCROWS(
            open_price, high, low, close)
        df.loc[:, 'CDL3INSIDE'] = talib.CDL3INSIDE(open_price, high, low,
                                                   close)
        df.loc[:, 'CDL3LINESTRIKE'] = talib.CDL3LINESTRIKE(
            open_price, high, low, close)
        # price transform
        df.loc[:, 'WCLPRICE'] = talib.WCLPRICE(high, low, close)
        # statistic funcitons
        df.loc[:, 'BETA'] = talib.BETA(high, low, timeperiod=20)
        df.loc[:, 'CORREL'] = talib.CORREL(high, low, timeperiod=20)
        df.loc[:, 'STDDEV'] = talib.STDDEV(close, timeperiod=20, nbdev=1)
        df.loc[:, 'TSF'] = talib.TSF(close, timeperiod=20)
        df.loc[:, 'VAR'] = talib.VAR(close, timeperiod=20, nbdev=1)
        # volatility indicators
        df.loc[:, 'ATR'] = talib.ATR(high, low, close, timeperiod=7)
        df.loc[:, 'NATR'] = talib.NATR(high, low, close, timeperiod=20)
        df.loc[:, 'TRANGE'] = talib.TRANGE(high, low, close)
        # volume indicators
        df.loc[:, 'AD'] = talib.AD(high, low, close, volume)
        df.loc[:, 'ADOSC'] = talib.ADOSC(high,
                                         low,
                                         close,
                                         volume,
                                         fastperiod=10,
                                         slowperiod=20)
        df.loc[:, 'OBV'] = talib.OBV(close, volume)

        # df.fillna(df.mean(), inplace=True)
        df.dropna(inplace=True)
        df.set_index('Date', inplace=True)
        print(colored('> caching' + asset + '/' + currency + ':)', 'cyan'))
        train_size = round(
            len(df) *
            DF_TRAIN_SIZE)  # 75% to train -> test with different value
        df_train = df[:train_size]
        df_rollout = df[train_size:]
        df_train.to_csv(df_train_path)
        df_rollout.to_csv(df_rollout_path)
        df_train = pd.read_csv(
            df_train_path)  # re-read to avoid indexing issue w/ Ray
        df_rollout = pd.read_csv(df_rollout_path)
    else:

        print(
            colored(
                emoji.emojize('> ' + random.choice(emojis) + ' feching ' +
                              asset + '/' + currency + ' from cache',
                              use_aliases=True), 'magenta'))

        # print(colored('> feching ' + asset + '/' + currency + ' from cache :)', 'magenta'))
        df_train = pd.read_csv(df_train_path)
        df_rollout = pd.read_csv(df_rollout_path)
        # df_train.set_index('Date', inplace=True)
        # df_rollout.set_index('Date', inplace=True)

    return df_train, df_rollout
                       slowperiod=10)
df['OBV'] = ta.OBV(np.array(df['Adj Close'].shift(1)),
                   np.array(df['Volume'].shift(1)))

# Price Transform Functions
df['AVGPRICE'] = ta.AVGPRICE(np.array(df['Open'].shift(1)),
                             np.array(df['High'].shift(1)),
                             np.array(df['Low'].shift(1)),
                             np.array(df['Adj Close'].shift(1)))
df['MEDPRICE'] = ta.MEDPRICE(np.array(df['High'].shift(1)),
                             np.array(df['Low'].shift(1)))
df['TYPPRICE'] = ta.TYPPRICE(np.array(df['High'].shift(1)),
                             np.array(df['Low'].shift(1)),
                             np.array(df['Adj Close'].shift(1)))
df['WCLPRICE'] = ta.WCLPRICE(np.array(df['High'].shift(1)),
                             np.array(df['Low'].shift(1)),
                             np.array(df['Adj Close'].shift(1)))

# Pattern Recognition Fuction
df['Two_Crows'] = ta.CDL2CROWS(np.array(df['Open']), np.array(df['High']),
                               np.array(df['Low']), np.array(df['Adj Close']))
df['Three_Crows'] = ta.CDL3BLACKCROWS(np.array(df['Open']),
                                      np.array(df['High']),
                                      np.array(df['Low']),
                                      np.array(df['Adj Close']))
df['Three_Inside_Up_Down'] = ta.CDL3INSIDE(np.array(df['Open']),
                                           np.array(df['High']),
                                           np.array(df['Low']),
                                           np.array(df['Adj Close']))
df['Three_Line_Strike'] = ta.CDL3LINESTRIKE(np.array(df['Open']),
                                            np.array(df['High']),
Пример #27
0
def talib_WCLPRICE(DataFrame):
    """WCLPRICE - Weighted Close Price 加权收盘价"""
    res = talib.WCLPRICE(DataFrame.high.values, DataFrame.low.values,
                         DataFrame.close.values)
    return pd.DataFrame({'WCLPRICE': res}, index=DataFrame.index)
Пример #28
0
# volatility indicators -> ATR
# pattern recognition -> CDLBELTHOLD

atr = ta.ATR(high, low, close, timeperiod=period)
stoch_k, stoch_d = ta.STOCH(high, low, close)  # use deafault
adx = ta.ADX(high, low, close, timeperiod=period)

aroon_up, aroon_dn = ta.AROON(high, low, timeperiod=period)
aroon = aroon_up - aroon_dn

adosc = ta.ADOSC(high, low, close, volume)
macd, macdsignal, macdhist = ta.MACD(close)  # use default
mfi = ta.MFI(high, low, close, volume, timeperiod=period)
sar = ta.SAR(high, low)  # use default
ht = ta.HT_DCPERIOD(close)
wcl = ta.WCLPRICE(high, low, close)

#print(len(atr))
#print(len(stoch_d))
#print(len(adx))
#print(len(aroon))
#print(len(ad))
#print(len(macd))
#print(len(mfi))
#print(len(sar))
#print(len(ht))
#print(len(wcl))
attrNames = np.array([
    'atr', 'stoch', 'adx', 'aroon', 'adosc', 'macd', 'mfl', 'sar', 'ht', 'wcl'
])
F = np.column_stack((atr, stoch_d, adx, aroon, adosc, macd, mfi, sar, ht, wcl))
Пример #29
0
 def talib_038(self):
     data_WCLPRICE = copy.deepcopy(self.close)
     for symbol in symbols:
         data_WCLPRICE[symbol] = ta.WCLPRICE(self.high[symbol].values, self.low[symbol].values, self.close[symbol].values)
     return data_WCLPRICE
Пример #30
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