Exemplo n.º 1
0
def midpoint(candles: np.ndarray,
             period: int = 14,
             source_type: str = "close",
             sequential: bool = False) -> Union[float, np.ndarray]:
    """
    MIDPOINT - MidPoint over period

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

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

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

    if sequential:
        return res
    else:
        return None if np.isnan(res[-1]) else res[-1]
Exemplo n.º 2
0
def midpoint(close_ts, timeperiod=14):
    import talib
    close_np = close_ts.cpu().detach().numpy()
    close_df = pd.DataFrame(close_np)
    midpoint = close_df.apply(lambda x: talib.MIDPOINT(x, timeperiod=14))
    midpoint_ts = torch.tensor(midpoint.values, dtype=close_ts.dtype, device=close_ts.device)
    return midpoint_ts
Exemplo n.º 3
0
def MIDPOINT(close, timeperiod=14):
    ''' MidPoint over period

    分组: Overlap Studies 重叠研究

    简介:

    分析和应用:

    real = MIDPOINT(close, timeperiod=14)
    '''
    return talib.MIDPOINT(close, timeperiod)
Exemplo n.º 4
0
def _talib_MIDPOINT(data, n):
    if n <= 1:
        value = np.zeros_like(data)
    else:
        try:
            value = talib.MIDPOINT(data, timeperiod=n)
            ss = pd.Series(value).ffill().fillna(0)
            value = ss.values
        except Exception as e:
            raise Exception("[_talib_MIDPOINT]", e)
            # print("[WARNING] _talib_MIDPOINT: {}".format(e.args[0]))
            value = np.zeros_like(data)
    return value.astype(float)
Exemplo n.º 5
0
def add_MIDPOINT(self, timeperiod=14, type="line", color="secondary", **kwargs):
    """Midpoint Price over Period."""

    if not self.has_close:
        raise Exception()

    utils.kwargs_check(kwargs, VALID_TA_KWARGS)
    if "kind" in kwargs:
        type = kwargs["kind"]

    name = "MIDPOINT({})".format(str(timeperiod))
    self.pri[name] = dict(type=type, color=color)
    self.ind[name] = talib.MIDPOINT(self.df[self.cl].values)
Exemplo n.º 6
0
def midpoint(close, graph=False, **kwargs):
    '''
    MIDPOINT - MidPoint over period
    '''
    result = talib.MIDPOINT(close, **kwargs)
    df = pd.concat([pd.DataFrame(close), pd.DataFrame(result)], axis=1)
    df.columns = ['close', 'midpoint']
    if graph:
        title = 'MIDPOINT - MidPoint over period'
        style = ['r-'] + ['--'] * (len(df.columns) - 1)
        fname = '09_midpoint.png'
        make_graph(title, df, style=style, fname=fname)
    return df
Exemplo n.º 7
0
    def test_midpoint(self):
        result = self.overlap.midpoint(self.close)
        self.assertIsInstance(result, Series)
        self.assertEqual(result.name, 'MIDPOINT_2')

        try:
            expected = tal.MIDPOINT(self.close, 2)
            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)
 def mid_point(self, df, period):
     """(highest value + lowest value)/2 over period
         Args:
             close: closing price of instrument
             period: number of time periods in the calculation
             feature_dict: Dictionary of added features
         Return:
             midPrice: resulting signal
             feature_dict
     """
     col_name = 'Midpoint_' + str(period)
     current_feature['Latest'] = col_name
     feature_dict[col_name] = 'Keep'
     df[col_name] = ta.MIDPOINT(df.Close, period)
     return df
Exemplo n.º 9
0
	def overlap(self):
		upper, middle, lower = talib.BBANDS(self.close,timeperiod=5,nbdevup=2,nbdevdn=2,matype=0)
		EMA = talib.EMA(self.close,self.period)
		HT_trendline = talib.HT_TRENDLINE(self.close)
		KAMA = talib.KAMA(self.close,self.period)
		MA = talib.MA(self.close,self.period,matype=0)
		mama, fama = talib.MAMA(self.close,fastlimit = 0.5,slowlimit = 0.05)
		mavp = talib.MAVP(self.close, minperiod = 5,maxperiod = 30,matype=0)
		midpoint = talib.MIDPOINT(self.close,self.period)
		midprice = talib.MIDPRICE(self.high,self.low,self.period)
		sar = talib.SAR(self.high,self.low,acceleration = 0, maximum = 0)
		sarext = talib.SAREXT(self.high,self.low,startvalue=0,offsetonreverse=0,accelerationinitlong=0, accelerationlong=0, accelerationmaxlong=0, accelerationinitshort=0, accelerationshort=0, accelerationmaxshort=0)
		sma = talib.SMA(self.close,self.period)
		t3 = talib.T3(self.close, self.period, vfactor = 0)
		tema = talib.TEMA(self.close,self.period)
		trima = talib.TRIMA(self.close,period)
		wma = talib.WMA(self.close, self.period)
Exemplo n.º 10
0
def midpoint(candles: np.ndarray, period: int = 14, source_type: str = "close", sequential: bool = False) -> Union[
    float, np.ndarray]:
    """
    MIDPOINT - MidPoint over period

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

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

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

    return res if sequential else res[-1]
Exemplo n.º 11
0
def ta_MIDPOINT(MIDPOINT_conf, curr_close_price_seq):

    MIDPOINT_seqs = []
    curr_feature_list = []

    MIDPOINT_period_num = len(MIDPOINT_conf["period"])

    for i in range(MIDPOINT_period_num):
        curr_period = MIDPOINT_conf["period"][0]

        curr_feature_list.append("MIDPOINT_" + str(curr_period))

        curr_MIDPOINT_seq = talib.MIDPOINT(curr_close_price_seq,
                                           timeperiod=curr_period)

        MIDPOINT_seqs.append(curr_MIDPOINT_seq.copy())

    return MIDPOINT_seqs, curr_feature_list
Exemplo n.º 12
0
def midpoint(client, symbol, timeframe="6m", col="close", period=14):
    """This will return a dataframe of midpoint over period
     for the given symbol across the given timeframe

    Args:
        client (pyEX.Client); Client
        symbol (string); Ticker
        timeframe (string); timeframe to use, for pyEX.chart
        col (string); column to use to calculate
        period (int); time period for kama

    Returns:
        DataFrame: result
    """
    df = client.chartDF(symbol, timeframe)

    build = {col: df[col].values}
    build["kama-{}".format(col)] = t.MIDPOINT(df[col].values, period)
    return pd.DataFrame(build)
Exemplo n.º 13
0
def get_df(filename):
    tech = pd.read_csv(filename,index_col=0)
    dclose = np.array(tech.close)
    volume = np.array(tech.volume)
    tech['RSI'] = ta.RSI(np.array(tech.close))
    tech['OBV'] = ta.OBV(np.array(tech.close),np.array(tech.volume))
    tech['NATR'] = ta.NATR(np.array(tech.high),np.array(tech.low),np.array(tech.close))
    tech['upper'],tech['middle'],tech['lower'] = ta.BBANDS(np.array(tech.close), timeperiod=10, nbdevup=2, nbdevdn=2, matype=0)
    tech['DEMA'] = ta.DEMA(dclose, timeperiod=30)
    tech['EMA'] = ta.EMA(dclose, timeperiod=30)
    tech['HT_TRENDLINE'] = ta.HT_TRENDLINE(dclose)
    tech['KAMA'] = ta.KAMA(dclose, timeperiod=30)
    tech['MA'] = ta.MA(dclose, timeperiod=30, matype=0)
#    tech['mama'], tech['fama'] = ta.MAMA(dclose, fastlimit=0, slowlimit=0)
    tech['MIDPOINT'] = ta.MIDPOINT(dclose, timeperiod=14)
    tech['SMA'] = ta.SMA(dclose, timeperiod=30)
    tech['T3'] = ta.T3(dclose, timeperiod=5, vfactor=0)
    tech['TEMA'] = ta.TEMA(dclose, timeperiod=30)
    tech['TRIMA'] = ta.TRIMA(dclose, timeperiod=30)
    tech['WMA'] = ta.WMA(dclose, timeperiod=30)
    tech['APO'] = ta.APO(dclose, fastperiod=12, slowperiod=26, matype=0)
    tech['CMO'] = ta.CMO(dclose, timeperiod=14)
    tech['macd'], tech['macdsignal'], tech['macdhist'] = ta.MACD(dclose, fastperiod=12, slowperiod=26, signalperiod=9)
    tech['MOM'] = ta.MOM(dclose, timeperiod=10)
    tech['PPO'] = ta.PPO(dclose, fastperiod=12, slowperiod=26, matype=0)
    tech['ROC'] = ta.ROC(dclose, timeperiod=10)
    tech['ROCR'] = ta.ROCR(dclose, timeperiod=10)
    tech['ROCP'] = ta.ROCP(dclose, timeperiod=10)
    tech['ROCR100'] = ta.ROCR100(dclose, timeperiod=10)
    tech['RSI'] = ta.RSI(dclose, timeperiod=14)
    tech['fastk'], tech['fastd'] = ta.STOCHRSI(dclose, timeperiod=14, fastk_period=5, fastd_period=3, fastd_matype=0)
    tech['TRIX'] = ta.TRIX(dclose, timeperiod=30)
    tech['OBV'] = ta.OBV(dclose,volume)
    tech['HT_DCPHASE'] = ta.HT_DCPHASE(dclose)
    tech['inphase'], tech['quadrature'] = ta.HT_PHASOR(dclose)
    tech['sine'], tech['leadsine'] = ta.HT_SINE(dclose)
    tech['HT_TRENDMODE'] = ta.HT_TRENDMODE(dclose)
    df = tech.fillna(method='bfill')
    return df
Exemplo n.º 14
0
def getOverlapFunctions(df):
    high = df['High']
    low = df['Low']
    close = df['Close']
    open = df['Open']
    volume = df['Volume']
    df['UPPERBB'],df['MIDDLEBB'],df['LOWERBB'] = ta.BBANDS(close, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0)
    df['DEMA'] = ta.DEMA(close,timeperiod=30)
    df['EMA'] = ta.EMA(close, timeperiod=30)
    df['HT_TREND'] = ta.HT_TRENDLINE(close)
    df['KAMA'] = ta.KAMA(close, timeperiod=30)
    df['MA'] = ta.MA(close, timeperiod=30, matype=0)
    #df['MAMA'],df['FAMA'] = ta.MAMA(close, fastlimit=0, slowlimit=0)
    #df['MAVP'] = ta.MAVP(close, periods, minperiod=2, maxperiod=30, matype=0)
    df['MIDPOINT'] = ta.MIDPOINT(close, timeperiod=14)
    df['MIDPRICE'] = ta.MIDPRICE(high, low, timeperiod=14)
    df['SAR'] = ta.SAR(high, low, acceleration=0, maximum=0)
    df['SAREXT'] = ta.SAREXT(high, low, startvalue=0, offsetonreverse=0, accelerationinitlong=0, accelerationlong=0, accelerationmaxlong=0, accelerationinitshort=0, accelerationshort=0, accelerationmaxshort=0)
    df['SMA'] = ta.SMA(close, timeperiod=30)
    df['T3'] = ta.T3(close, timeperiod=5, vfactor=0)
    df['TEMA'] = ta.TEMA(close, timeperiod=30)
    df['TRIMA'] = ta.TRIMA(close, timeperiod=30)
    df['WMA'] = ta.WMA(close, timeperiod=30)
Exemplo n.º 15
0
 def get_average_studies(open, low, high, close, df):
     # https://mrjbq7.github.io/ta-lib/func_groups/overlap_studies.html
     # Bollinger bands
     df['UP_BB'], df['MID_BB'], df['LOW_BB'] = talib.BBANDS(close,
                                                            timeperiod=5,
                                                            nbdevup=2,
                                                            nbdevdn=2,
                                                            matype=0)
     # HT_TRENDLINE - Hilbert Transform - Instantaneous Trendline
     df["HT"] = talib.HT_TRENDLINE(close)
     # SAR - Parabolic SAR
     df["SAR"] = talib.SAR(high, low, acceleration=0, maximum=0)
     #
     periods = [5, 15, 30, 50, 100, 200]
     for period in periods:
         df['SMA-' + str(period)] = talib.SMA(close, timeperiod=period)
         df['DEMA-' + str(period)] = talib.DEMA(close, timeperiod=period)
         df['TEMA-' + str(period)] = talib.TEMA(close, timeperiod=period)
         df['WMA-' + str(period)] = talib.WMA(close, timeperiod=period)
         df['MIDPOINT-' + str(period)] = talib.MIDPOINT(close,
                                                        timeperiod=period)
         df['MIDPRICE-' + str(period)] = talib.MIDPRICE(high,
                                                        low,
                                                        timeperiod=period)
Exemplo n.º 16
0
def TALIB_MIDPOINT(close, timeperiod=14):
    '''00360,2,1'''
    return talib.MIDPOINT(close, timeperiod)
Exemplo n.º 17
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
Exemplo n.º 18
0
 def add_MIDPOINT(self, df, periods=[5, 10, 14, 20, 30, 60, 120]):
     for i in periods:
         period = str(i)
         df['MIDPOINT_' + period] = ta.MIDPOINT(df['close'], timeperiod=i)
     return df
Exemplo n.º 19
0
def handle_overlap_studies(args, kax, klines_df, close_times, display_count):
    all_name = ""
    if args.ABANDS:  # ATR BANDS
        name = 'ABANDS'
        real = talib.ATR(klines_df["high"],
                         klines_df["low"],
                         klines_df["close"],
                         timeperiod=14)
        emas = talib.EMA(klines_df["close"], timeperiod=26)
        kax.plot(close_times, emas[-display_count:], "b--", label=name)

        #cs = ['y', 'c', 'm', 'k']
        for idx, n in enumerate(args.ABANDS):
            """
            if idx >= len(cs):
                break
            c = cs[idx]
            """
            c = 'y'
            cl = c + '--'
            n = int(n)
            kax.plot(close_times, (emas + n * real)[-display_count:],
                     cl,
                     label=name + ' upperband')
            kax.plot(close_times, (emas - n * real)[-display_count:],
                     cl,
                     label=name + ' lowerband')

    if args.BANDS:  # BANDS
        name = 'BANDS'
        emas = talib.EMA(klines_df["close"], timeperiod=26)
        kax.plot(close_times, emas[-display_count:], "b--", label=name)
        r = args.BANDS
        kax.plot(close_times, (1 + r) * emas[-display_count:],
                 'y--',
                 label=name + ' upperband')
        kax.plot(close_times, (1 - r) * emas[-display_count:],
                 'y--',
                 label=name + ' lowerband')

    # talib
    os_key = 'BBANDS'
    if args.BBANDS:
        upperband, middleband, lowerband = talib.BBANDS(klines_df["close"],
                                                        timeperiod=5,
                                                        nbdevup=2,
                                                        nbdevdn=2,
                                                        matype=0)
        kax.plot(close_times,
                 upperband[-display_count:],
                 "y",
                 label=os_key + ' upperband')
        kax.plot(close_times,
                 middleband[-display_count:],
                 "b",
                 label=os_key + ' middleband')
        kax.plot(close_times,
                 lowerband[-display_count:],
                 "y",
                 label=os_key + ' lowerband')

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

    if args.EMA:
        name = 'EMA'
        all_name += "  %s%s" % (name, args.EMA)
        for idx, e_p in enumerate(args.EMA):
            if idx >= len(plot_colors):
                break
            e_p = int(e_p)
            emas = talib.EMA(klines_df["close"], timeperiod=e_p)
            kax.plot(close_times,
                     emas[-display_count:],
                     plot_colors[idx] + '--',
                     label="%sEMA" % (e_p))

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

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

    if args.MA:
        name = 'MA'
        all_name += "  %s%s" % (name, args.MA)
        for idx, e_p in enumerate(args.MA):
            if idx >= len(plot_colors):
                break
            e_p = int(e_p)
            emas = talib.MA(klines_df["close"], timeperiod=e_p)
            kax.plot(close_times,
                     emas[-display_count:],
                     plot_colors[idx],
                     label="%sMA" % (e_p))

    os_key = 'MAMA'
    if args.MAMA:
        mama, fama = talib.MAMA(klines_df["close"], fastlimit=0, slowlimit=0)
        kax.plot(close_times, mama[-display_count:], "b", label=os_key)
        kax.plot(close_times, fama[-display_count:], "c", label=os_key)

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

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

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

    os_key = 'SAREXT'
    if args.SAREXT:
        real = talib.SAREXT(klines_df["high"],
                            klines_df["low"],
                            startvalue=0,
                            offsetonreverse=0,
                            accelerationinitlong=0,
                            accelerationlong=0,
                            accelerationmaxlong=0,
                            accelerationinitshort=0,
                            accelerationshort=0,
                            accelerationmaxshort=0)
        kax.plot(close_times, real[-display_count:], "y", label=os_key)

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

    os_key = 'T3'
    if args.T3:
        real = talib.T3(klines_df["close"], timeperiod=args.T3, vfactor=0)
        kax.plot(close_times, real[-display_count:], "y", label=os_key)

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

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

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

    return all_name
Exemplo n.º 20
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)
# Overlap Studies Functions
df['upperband'], df['middleband'], df['lowerband'] = ta.BBANDS(np.array(
    df['Adj Close'].shift(1)),
                                                               timeperiod=n,
                                                               nbdevup=2,
                                                               nbdevdn=2,
                                                               matype=0)
df['DEMA'] = ta.DEMA(np.array(df['Adj Close'].shift(1)), timeperiod=n)
df['EMA'] = ta.EMA(np.array(df['Adj Close'].shift(1)), timeperiod=n)
df['HT_TRENDLINE'] = ta.HT_TRENDLINE(np.array(df['Adj Close'].shift(1)))
df['KAMA'] = ta.KAMA(np.array(df['Adj Close'].shift(1)), timeperiod=n)
df['MA'] = ta.MA(np.array(df['Adj Close'].shift(1)), timeperiod=n, matype=0)
# df['mama'],df['fama'] = ta.MAMA(np.array(df['Adj Close'].shift(1)), fastlimit=0, slowlimit=0)
# df['MAVP'] =ta.MAVP(np.array(df['Adj Close'].shift(1)),periods=14, minperiod=2, maxperiod=30, matype=0)
df['MIDPOINT'] = ta.MIDPOINT(np.array(df['Adj Close'].shift(1)), timeperiod=n)
df['MIDPRICE'] = ta.MIDPRICE(np.array(df['High'].shift(1)),
                             np.array(df['Low'].shift(1)),
                             timeperiod=n)
df['SAR'] = ta.SAR(np.array(df['High'].shift(1)),
                   np.array(df['Low'].shift(1)),
                   acceleration=0,
                   maximum=0)

df['SAREXT'] = ta.SAREXT(np.array(df['High'].shift(1)),
                         np.array(df['Low'].shift(1)),
                         startvalue=0,
                         offsetonreverse=0,
                         accelerationinitlong=0,
                         accelerationlong=0,
                         accelerationmaxlong=0,
Exemplo n.º 22
0
def talib_MIDPOINT(DataFrame, N=14):
    res = talib.MIDPOINT(DataFrame.close.values, timeperiod=N)
    return pd.DataFrame({'MIDPOINT': res}, index=DataFrame.index)
Exemplo n.º 23
0
def MIDPOINT(raw_df, timeperiod=14):
    # extract necessary data from raw dataframe (close)
    return ta.MIDPOINT(raw_df.Close.values, timeperiod)
Exemplo n.º 24
0
def MIDPOINT(data, **kwargs):
    _check_talib_presence()
    prices = _extract_series(data)
    return talib.MIDPOINT(prices, **kwargs)
Exemplo n.º 25
0
def data_cleaning(df_temp):
    #################################     计算各种指标           ##################################
    #################  close high low volume is "pandas.core.series.Series" #######################
    ################# output MACD_5 is"numpy.ndarray"  ############################################
    df=pd.DataFrame.copy(df_temp)
    close = df['close']
    volume= np.array(df['volume'],dtype='float64')
    high = df['high']
    low = df['low']

    ######################## input  is  "numpy.ndarray"  use .values to convert ###################
    ############## https://zhuanlan.zhihu.com/p/25407061  中文解析 Talib 指标使用 #################
    ma5 = tb.MA(close.values, timeperiod=5, matype=0)
    ma10 = tb.MA(close.values, timeperiod=10, matype=0)
    ma20 = tb.MA(close.values, timeperiod=20, matype=0)
    v_ma5 = tb.MA(volume, timeperiod=5, matype=0)
    v_ma10 = tb.MA(volume, timeperiod=10, matype=0)
    v_ma20 = tb.MA(volume, timeperiod=20, matype=0)
    price_change = get_price_change(close)
    p_change = get_p_change(close)
    MACD_5, MACD_Singal, hist = tb.MACD(close.values,fastperiod=12,slowperiod=26,signalperiod=9)
    EMA_12 = tb.EMA(close.values,timeperiod=12)
    EMA_5 = tb.EMA(close.values,timeperiod=5)
    EMA_20 = tb.EMA(close.values,timeperiod=20)
    RSI_6 = tb.RSI(close.values,timeperiod=6)
    RSI_12 = tb.RSI(close.values,timeperiod=12)
    SMA = tb.SMA(close.values,timeperiod=5)
    
    upper, middle, lower =tb.BBANDS(close.values,timeperiod=5,nbdevup=2,nbdevdn=2,matype=0)
    #matype:
    # 0    SMA – Simple Moving Average
    # 1    EMA – Exponential Average
    # 2    WMA – Weighted Moving Average
    # 3    DEMA – Double Exponential Moving Average
    # 4    TEMA – Triple Exponential Moving Average
    # 5    TRIMA – Triangular Moving Average
    # 6    KAMA – Kaufman Adaptive Moving Average
    # 7    MAMA – MESA Adaptive Moving Average
    # 8    T3 – Triple Exponential Moving Average
    
    KAMA = tb.KAMA(close.values,timeperiod=5)
    OBV = tb.OBV(close.values, volume)
    CCI = tb.CCI(high.values, low.values, close.values, timeperiod=5)
    DEMA = tb.DEMA(close.values,timeperiod=5)
    HT_TRENDLINE = tb.HT_TRENDLINE(close.values)
    MIDPOINT = tb.MIDPOINT(close.values,timeperiod=5)
    MIDPRICE = tb.MIDPRICE(high.values, low.values, timeperiod=5)
    CMO = tb.CMO(close.values, timeperiod=5)
    ADX =tb.ADX(high.values, low.values, close.values, timeperiod=5)
    ADXR = tb.ADXR(high.values, low.values, close.values, timeperiod=5)
    AROON_D, AROON_U = tb.AROON(high.values, low.values, timeperiod=5)
    ROC = tb.ROC(close.values, timeperiod=5)
    CMO = tb.CMO(close.values, timeperiod= 5)
    PPO = tb.PPO(close.values, fastperiod=3, slowperiod=5, matype=0)

    features=['ma5','ma10','ma20','v_ma5','v_ma10','v_ma20','price_change','p_change','EMA_5','EMA_12','EMA_20','MACD_5','MACD_Singal',
              'hist','RSI_6','RSI_12','SMA',
              'upper','middle','lower','KAMA','OBV','CCI','DEMA','HT_TRENDLINE','MIDPOINT','MIDPRICE',
              'CMO','ADX','ADXR','AROON_D','AROON_U','ROC','CMO','PPO']
    ######################计算各种指标#############################  
    
    ###### 将以上获得的features通过时间平移,将前五天的历史数据作为今天的features 建立一个235维的数据集。#####
    for f in features:
        df[f]=locals()[f]
    indicators = list(df.columns.values)
    for i in range(1,6):
        for indicator in indicators:
            name= "{0}".format(i) +'_'+indicator
            df[name] = df[indicator].shift(i)   
    df.dropna(inplace=True)   #只要有NaN的行,全部删出以免出错
    print('Cleaning Completed')
    return df
criptomoeda_volume = criptomoeda['v'].astype('float')
criptomoeda_datas_fechamento = criptomoeda['close_time'].astype('float')
criptomoeda_datas_abertura = criptomoeda['open_time'].astype('float')
taker_base_vol = criptomoeda['taker_base_vol'].astype('float')
taker_quote_vol = criptomoeda['taker_quote_vol'].astype('float')
closeprice = criptomoeda_close.iloc[-1]
cci = talib.CCI(criptomoeda_maxima,
                criptomoeda_minima,
                criptomoeda_close,
                timeperiod=14)
atr = talib.ATR(criptomoeda_maxima,
                criptomoeda_minima,
                criptomoeda_close,
                timeperiod=14)

midpoint = talib.MIDPOINT(criptomoeda_close, timeperiod=30)

sma6 = talib.SMA(criptomoeda_close, timeperiod=6)
sma9 = talib.SMA(criptomoeda_close, timeperiod=9)

real = talib.T3(criptomoeda_close, timeperiod=14)
print('Close Price: $%.2f' % (closeprice))
# Média movel de 14 dias do Fechamento
criptomoeda_fechamento_mediamovel = criptomoeda['c'].rolling(30).mean()
# Média movel de 30 dias do Fechamento
criptomoeda_fechamento_mediamovel100 = criptomoeda['c'].rolling(100).mean()
#======== Importar biblioteca SKLEARN
import numpy as np
from sklearn import linear_model
from sklearn.metrics import mean_squared_error, r2_score
from matplotlib import rcParams
Exemplo n.º 27
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()))
Exemplo n.º 28
0
def create_tas(bars,
                verbose=False,
                ohlcv_cols=['Adj_High', 'Adj_Low', 'Adj_Open', 'Adj_Close', 'Adj_Volume'],
                return_df=False,
                cl=True,
                tp=True):
    """
    This is basically set up for daily stock data.  Other time frames would need adapting probably.

    :param bars: resampled pandas dataframe with open, high, low, close, volume, and typical_price columns
    :param verbose: boolean, if true, prints more debug
    :param ohlcv_cols: list of strings; the column names for high, low, open, close, and volume
    :param cl: use the close price to make TAs
    :param tp: calcuclate typical price and use it for TAs

    :returns: pandas dataframe with TA signals calculated (modifies dataframe in place)
    """
    h, l, o, c, v = ohlcv_cols
    if 'typical_price' not in bars.columns and tp:
        bars['typical_price'] = bars[[h, l, c]].mean(axis=1)

    # bollinger bands
    # strange bug, if values are small, need to multiply to larger value for some reason
    mult = 1
    last_close = bars.iloc[0][c]
    lc_m = last_close * mult
    while lc_m < 1:
        mult *= 10
        lc_m = last_close * mult

    if verbose:
        print('using multiplier of', mult)

    if tp:
        mult_tp = bars['typical_price'].values * mult

    mult_close = bars[c].values * mult
    mult_open = bars[o].values * mult
    mult_high = bars[h].values * mult
    mult_low = bars[l].values * mult
    # for IB data, the volume is integer, but needs to be float for talib
    volume = bars[v].astype('float').values


    # ADR - average daily range
    # http://forextraininggroup.com/using-adr-average-daily-range-find-short-term-trading-opportunities/
    # TODO

    ### overlap studies
    # bollinger bands -- should probably put these into another indicator
    if cl:
        upper_cl, middle_cl, lower_cl = talib.BBANDS(mult_close,
                                        timeperiod=10,
                                        nbdevup=2,
                                        nbdevdn=2)

        bars['bband_u_cl'] = upper_cl / mult
        bars['bband_m_cl'] = middle_cl / mult
        bars['bband_l_cl'] = lower_cl / mult
        bars['bband_u_cl_diff'] = bars['bband_u_cl'] - bars[c]
        bars['bband_m_cl_diff'] = bars['bband_m_cl'] - bars[c]
        bars['bband_l_cl_diff'] = bars['bband_l_cl'] - bars[c]
        bars['bband_u_cl_diff_hi'] = bars['bband_u_cl'] - bars[h]
        bars['bband_l_cl_diff_lo'] = bars['bband_l_cl'] - bars[l]
        # bars['bband_u_cl'].fillna(method='bfill', inplace=True)
        # bars['bband_m_cl'].fillna(method='bfill', inplace=True)
        # bars['bband_l_cl'].fillna(method='bfill', inplace=True)

    if tp:
        upper_tp, middle_tp, lower_tp = talib.BBANDS(mult_tp,
                                        timeperiod=10,
                                        nbdevup=2,
                                        nbdevdn=2)

        bars['bband_u_tp'] = upper_tp / mult
        bars['bband_m_tp'] = middle_tp / mult
        bars['bband_l_tp'] = lower_tp / mult
        bars['bband_u_tp_diff'] = bars['bband_u_tp'] - bars['typical_price']
        bars['bband_m_tp_diff'] = bars['bband_m_tp'] - bars['typical_price']
        bars['bband_l_tp_diff'] = bars['bband_l_tp'] - bars['typical_price']
        bars['bband_u_tp_diff_hi'] = bars['bband_u_tp'] - bars[h]
        bars['bband_l_tp_diff_lo'] = bars['bband_l_tp'] - bars[l]
        # think this is already taken care of at the end...check
        # bars['bband_u_tp'].fillna(method='bfill', inplace=True)
        # bars['bband_m_tp'].fillna(method='bfill', inplace=True)
        # bars['bband_l_tp'].fillna(method='bfill', inplace=True)

    # Double Exponential Moving Average
    if cl:
        bars['dema_cl'] = talib.DEMA(mult_close, timeperiod=30) / mult
        bars['dema_cl_diff'] = bars['dema_cl'] - bars[c]

    if tp:
        bars['dema_tp'] = talib.DEMA(mult_tp, timeperiod=30) / mult
        bars['dema_tp_diff'] = bars['dema_tp'] - bars['typical_price']


    # exponential moving Average
    if cl:
        bars['ema_cl'] = talib.EMA(mult_close, timeperiod=30) / mult
        bars['ema_cl_diff'] = bars['ema_cl'] - bars[c]

    if tp:
        bars['ema_tp'] = talib.EMA(mult_tp, timeperiod=30) / mult
        bars['ema_tp_diff'] = bars['ema_tp'] - bars['typical_price']

    # Hilbert Transform - Instantaneous Trendline - like a mva but a bit different, should probably take slope or
    # use in another indicator
    if cl:
        bars['ht_tl_cl'] = talib.HT_TRENDLINE(mult_close) / mult
        bars['ht_tl_cl_diff'] = bars['ht_tl_cl'] - bars[c]

    if tp:
        bars['ht_tl_tp'] = talib.HT_TRENDLINE(mult_tp) / mult
        bars['ht_tl_tp_diff'] = bars['ht_tl_tp'] - bars['typical_price']

    # KAMA - Kaufman's Adaptative Moving Average -- need to take slope or something
    if cl:
        bars['kama_cl'] = talib.KAMA(mult_close, timeperiod=30) / mult
        bars['kama_cl_diff'] = bars['kama_cl'] - bars[c]

    if tp:
        bars['kama_tp'] = talib.KAMA(mult_tp, timeperiod=30) / mult
        bars['kama_tp_diff'] = bars['kama_tp'] - bars['typical_price']

    # MESA Adaptive Moving Average -- getting TA_BAD_PARAM error
    # mama_cl, fama_cl = talib.MAMA(mult_close, fastlimit=100, slowlimit=100) / mult
    # mama_tp, fama_tp = talib.MAMA(mult_tp, fastlimit=100, slowlimit=100) / mult
    # mama_cl_osc = (mama_cl - fama_cl) / mama_cl
    # mama_tp_osc = (mama_tp - fama_tp) / mama_tp
    # bars['mama_cl'] = mama_cl
    # bars['mama_tp'] = mama_tp
    # bars['fama_cl'] = fama_cl
    # bars['fama_tp'] = fama_tp
    # bars['mama_cl_osc'] = mama_cl_osc
    # bars['mama_tp_osc'] = mama_tp_osc

    # Moving average with variable period
    if cl:
        bars['mavp_cl'] = talib.MAVP(mult_close, np.arange(mult_close.shape[0]).astype(np.float64), minperiod=2, maxperiod=30, matype=0) / mult
        bars['mavp_cl_diff'] = bars['mavp_cl'] - bars[c]

    if tp:
        bars['mavp_tp'] = talib.MAVP(mult_tp, np.arange(mult_tp.shape[0]).astype(np.float64), minperiod=2, maxperiod=30, matype=0) / mult
        bars['mavp_tp_diff'] = bars['mavp_tp'] - bars['typical_price']

    # midpoint over period
    if cl:
        bars['midp_cl'] = talib.MIDPOINT(mult_close, timeperiod=14) / mult
        bars['midp_cl_diff'] = bars['midp_cl'] - bars[c]

    if tp:
        bars['midp_tp'] = talib.MIDPOINT(mult_tp, timeperiod=14) / mult
        bars['midp_tp_diff'] = bars['midp_tp'] - bars['typical_price']

    # midpoint price over period
    bars['midpr'] = talib.MIDPRICE(mult_high, mult_low, timeperiod=14) / mult
    if cl:
        bars['midpr_diff_cl'] = bars['midpr'] - bars[c]

    if tp:
        bars['midpr_diff_tp'] = bars['midpr'] - bars['typical_price']


    # parabolic sar
    bars['sar'] = talib.SAR(mult_high, mult_low, acceleration=0.02, maximum=0.2) / mult

    if cl:
        bars['sar_diff_cl'] = bars['sar'] - bars[c]

    if tp:
        bars['sar_diff_tp'] = bars['sar'] - bars['typical_price']
    # need to make an oscillator for this

    # simple moving average
    # 10, 20, 30, 40 day
    if cl:
        bars['sma_10_cl'] = talib.SMA(mult_close, timeperiod=10) / mult
        bars['sma_20_cl'] = talib.SMA(mult_close, timeperiod=20) / mult
        bars['sma_30_cl'] = talib.SMA(mult_close, timeperiod=30) / mult
        bars['sma_40_cl'] = talib.SMA(mult_close, timeperiod=40) / mult

    if tp:
        bars['sma_10_tp'] = talib.SMA(mult_tp, timeperiod=10) / mult
        bars['sma_20_tp'] = talib.SMA(mult_tp, timeperiod=20) / mult
        bars['sma_30_tp'] = talib.SMA(mult_tp, timeperiod=30) / mult
        bars['sma_40_tp'] = talib.SMA(mult_tp, timeperiod=40) / mult

    # triple exponential moving average
    if cl:
        bars['tema_cl'] = talib.TEMA(mult_close, timeperiod=30) / mult
        bars['tema_cl_diff'] = bars['tema_cl'] - bars[c]

    if tp:
        bars['tema_tp'] = talib.TEMA(mult_tp, timeperiod=30) / mult
        bars['tema_tp_diff'] = bars['tema_tp'] - bars['typical_price']

    # triangular ma
    if cl:
        bars['trima_cl'] = talib.TRIMA(mult_close, timeperiod=30) / mult
        bars['trima_cl_diff'] = bars['trima_cl'] - bars[c]

    if tp:
        bars['trima_tp'] = talib.TRIMA(mult_tp, timeperiod=30) / mult
        bars['trima_tp_diff'] = bars['trima_tp'] - bars['typical_price']

    # weighted moving average
    if cl:
        bars['wma_cl'] = talib.WMA(mult_close, timeperiod=30) / mult
        bars['wma_cl_diff'] = bars['wma_cl'] - bars[c]

    if tp:
        bars['wma_tp'] = talib.WMA(mult_tp, timeperiod=30) / mult
        bars['wma_tp_diff'] = bars['wma_tp'] - bars['typical_price']

    #### momentum indicators  -- for now left out some of those with unstable periods (maybe update and included them, not sure)

    # Average Directional Movement Index - 0 to 100 I think
    bars['adx_14'] = talib.ADX(mult_high, mult_low, mult_close, timeperiod=14)
    bars['adx_5'] = talib.ADX(mult_high, mult_low, mult_close, timeperiod=5)

    # Average Directional Movement Index Rating
    bars['adxr'] = talib.ADXR(mult_high, mult_low, mult_close, timeperiod=14)

    # Absolute Price Oscillator
    # values around -100 to +100
    if cl:
        bars['apo_cl'] = talib.APO(mult_close, fastperiod=12, slowperiod=26, matype=0)

    if tp:
        bars['apo_tp'] = talib.APO(mult_tp, fastperiod=12, slowperiod=26, matype=0)

    # Aroon and Aroon Oscillator 0-100, so don't need to renormalize
    arup, ardn = talib.AROON(mult_high, mult_low, timeperiod=14)
    bars['arup'] = arup
    bars['ardn'] = ardn

    # linearly related to aroon, just aroon up - aroon down
    bars['aroonosc'] = talib.AROONOSC(mult_high, mult_low, timeperiod=14)

    # balance of power - ratio of values so don't need to re-normalize
    bars['bop'] = talib.BOP(mult_open, mult_high, mult_low, mult_close)

    # Commodity Channel Index
    # around -100 to + 100
    bars['cci'] = talib.CCI(mult_high, mult_low, mult_close, timeperiod=14)

    # Chande Momentum Oscillator
    if cl:
        bars['cmo_cl'] = talib.CMO(mult_close, timeperiod=14)

    if tp:
        bars['cmo_tp'] = talib.CMO(mult_tp, timeperiod=14)

    # dx - Directional Movement Index
    bars['dx'] = talib.DX(mult_high, mult_low, mult_close, timeperiod=14)

    # Moving Average Convergence/Divergence
    # https://www.quantopian.com/posts/how-does-the-talib-compute-macd-why-the-value-is-different
    # macd diff btw fast and slow EMA
    if cl:
        macd_cl, macdsignal_cl, macdhist_cl = talib.MACD(mult_close, fastperiod=12, slowperiod=26, signalperiod=9)
        bars['macd_cl'] = macd_cl / mult
        bars['macdsignal_cl'] = macdsignal_cl / mult
        bars['macdhist_cl'] = macdhist_cl / mult

    if tp:
        macd_tp, macdsignal_tp, macdhist_tp = talib.MACD(mult_tp, fastperiod=12, slowperiod=26, signalperiod=9)
        bars['macd_tp'] = macd_tp / mult
        bars['macdsignal_tp'] = macdsignal_tp / mult
        bars['macdhist_tp'] = macdhist_tp / mult

    # mfi - Money Flow Index
    bars['mfi'] = talib.MFI(mult_high, mult_low, mult_close, volume, timeperiod=14)

    # minus di - Minus Directional Indicator
    bars['mdi'] = talib.MINUS_DI(mult_high, mult_low, mult_close, timeperiod=14)

    # Minus Directional Movement
    bars['mdm'] = talib.MINUS_DM(mult_high, mult_low, timeperiod=14)

    # note: too small of a timeperiod will result in junk data...I think.  or at least very discretized
    if cl:
        bars['mom_cl'] = talib.MOM(mult_close, timeperiod=14) / mult
    # bars['mom_cl'].fillna(method='bfill', inplace=True)

    if tp:
        bars['mom_tp'] = talib.MOM(mult_tp, timeperiod=14) / mult
    # bars['mom_tp'].fillna(method='bfill', inplace=True)

    # plus di - Plus Directional Indicator
    bars['pldi'] = talib.PLUS_DI(mult_high, mult_low, mult_close, timeperiod=14)

    # Plus Directional Movement
    bars['pldm'] = talib.PLUS_DM(mult_high, mult_low, timeperiod=14)

    # percentage price Oscillator
    # matype explanation: https://www.quantopian.com/posts/moving-averages
    if cl:
        bars['ppo_cl'] = talib.PPO(mult_close, fastperiod=12, slowperiod=26, matype=1)
        if bars['ppo_cl'].isnull().all():
            bars['ppo_cl_signal'] = 0
        else:
            bars['ppo_cl_signal'] = talib.EMA(bars['ppo_cl'].bfill().values, timeperiod=9)

    if tp:
        bars['ppo_tp'] = talib.PPO(mult_tp, fastperiod=12, slowperiod=26, matype=1)

    # rate of change -- really only need one
    # if cl:
    #     bars['roc_cl'] = talib.ROC(mult_close, timeperiod=10)
    #
    # if tp:
    #     bars['roc_tp'] = talib.ROC(mult_tp, timeperiod=10)

    # rocp - Rate of change Percentage: (price-prevPrice)/prevPrice
    if cl:
        bars['rocp_cl'] = talib.ROCP(mult_close, timeperiod=10)

    if tp:
        bars['rocp_tp'] = talib.ROCP(mult_tp, timeperiod=10)

    # rocr - Rate of change ratio: (price/prevPrice)
    # bars['rocr_cl'] = talib.ROCR(mult_close, timeperiod=10)
    # bars['rocr_tp'] = talib.ROCR(mult_tp, timeperiod=10)
    #
    # # Rate of change ratio 100 scale: (price/prevPrice)*100
    # bars['rocr_cl_100'] = talib.ROCR100(mult_close, timeperiod=10)
    # bars['rocr_tp_100'] = talib.ROCR100(mult_tp, timeperiod=10)

    # Relative Strength Index
    if cl:
        bars['rsi_cl_14'] = talib.RSI(mult_close, timeperiod=14)
        bars['rsi_cl_5'] = talib.RSI(mult_close, timeperiod=5)

    if tp:
        bars['rsi_tp'] = talib.RSI(mult_tp, timeperiod=14)

    # stochastic oscillator - % of price diffs, so no need to rescale
    slowk, slowd = talib.STOCH(mult_high, mult_low, mult_close, fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0)
    fastk, fastd = talib.STOCHF(mult_high, mult_low, mult_close, fastk_period=5, fastd_period=3, fastd_matype=0)
    bars['slowk'] = slowk
    bars['slowd'] = slowd
    bars['fastk'] = fastk
    bars['fastd'] = fastd

    # Stochastic Relative Strength Index
    if cl:
        fastk_cl, fastd_cl = talib.STOCHRSI(mult_close, timeperiod=14, fastk_period=5, fastd_period=3, fastd_matype=0)
        bars['strsi_cl_k'] = fastk_cl
        bars['strsi_cl_d'] = fastd_cl

    if tp:
        fastk_tp, fastd_tp = talib.STOCHRSI(mult_tp, timeperiod=14, fastk_period=5, fastd_period=3, fastd_matype=0)
        bars['strsi_tp_k'] = fastk_tp
        bars['strsi_tp_d'] = fastd_tp

    # trix - 1-day Rate-Of-Change (ROC) of a Triple Smooth EMA
    if cl:
        if bars.shape[0] > 90:
            bars['trix_cl_30'] = talib.TRIX(mult_close, timeperiod=30)
            bars['trix_cl_30_signal'] = talib.EMA(bars['trix_cl_30'].bfill().values, timeperiod=20)

        bars['trix_cl_14'] = talib.TRIX(mult_close, timeperiod=14)
        if bars['trix_cl_14'].isnull().all():
            bars['trix_cl_14_signal'] = 0
        else:
            bars['trix_cl_14_signal'] = talib.EMA(bars['trix_cl_14'].bfill().values, timeperiod=9)
        bars['trix_cl_12'] = talib.TRIX(mult_close, timeperiod=12)
        if bars['trix_cl_12'].isnull().all():
            bars['trix_cl_12_signal'] = 0
        else:
            bars['trix_cl_12_signal'] = talib.EMA(bars['trix_cl_12'].bfill().values, timeperiod=9)
        bars['trix_cl_5'] = talib.TRIX(mult_close, timeperiod=5)
        if bars['trix_cl_5'].isnull().all():
            bars['trix_cl_5_signal'] = 0
        else:
            bars['trix_cl_5_signal'] = talib.EMA(bars['trix_cl_5'].bfill().values, timeperiod=3)

    if tp:
        bars['trix_tp'] = talib.TRIX(mult_tp, timeperiod=30)

    # ultimate Oscillator - between 0 and 100
    bars['ultosc'] = talib.ULTOSC(mult_high, mult_low, mult_close, timeperiod1=7, timeperiod2=14, timeperiod3=28)

    # williams % r  -- 0 to 100
    bars['willr'] = talib.WILLR(mult_high, mult_low, mult_close, timeperiod=14)


    ### volume indicators
    # Chaikin A/D Line
    bars['ad'] = talib.AD(mult_high, mult_low, mult_close, volume)

    # Chaikin A/D Oscillator
    bars['adosc'] = talib.ADOSC(mult_high, mult_low, mult_close, volume, fastperiod=3, slowperiod=10)

    # on balance volume
    if cl:
        bars['obv_cl'] = talib.OBV(mult_close, volume)
        if bars['obv_cl'].isnull().all():
            bars['obv_cl_ema_14'] = 0
        else:
            bars['obv_cl_ema_14'] = talib.EMA(bars['obv_cl'].values, timeperiod=14)
    if tp:
        bars['obv_tp'] = talib.OBV(mult_tp, volume)


    ### volatility indicators
    # average true range
    # Large or increasing ranges suggest traders prepared to continue to bid up or sell down a stock through the course of the day. Decreasing range suggests waning interest.
    # https://en.wikipedia.org/wiki/Average_true_range
    bars['atr_65'] = talib.ATR(mult_high, mult_low, mult_close, timeperiod=65)
    bars['atr_20'] = talib.ATR(mult_high, mult_low, mult_close, timeperiod=20)
    bars['atr_14'] = talib.ATR(mult_high, mult_low, mult_close, timeperiod=14)
    bars['atr_5'] = talib.ATR(mult_high, mult_low, mult_close, timeperiod=5)

    # Normalized Average True Range
    bars['natr_14'] = talib.NATR(mult_high, mult_low, mult_close, timeperiod=14)
    bars['natr_5'] = talib.NATR(mult_high, mult_low, mult_close, timeperiod=5)

    # true range
    bars['trange'] = talib.TRANGE(mult_high, mult_low, mult_close) / mult


    ### Cycle indicators
    # Hilbert Transform - Dominant Cycle Period
    if cl:
        bars['ht_dcp_cl'] = talib.HT_DCPERIOD(mult_close)

    if tp:
        bars['ht_dcp_tp'] = talib.HT_DCPERIOD(mult_tp)

    # Hilbert Transform - Dominant Cycle Phase
    if cl:
        bars['ht_dcph_cl'] = talib.HT_DCPHASE(mult_close)

    if tp:
        bars['ht_dcph_tp'] = talib.HT_DCPHASE(mult_tp)

    # Hilbert Transform - Phasor Components
    if cl:
        inphase_cl, quadrature_cl = talib.HT_PHASOR(mult_close)
        bars['ht_ph_cl'] = inphase_cl
        bars['ht_q_cl'] = quadrature_cl

    if tp:
        inphase_tp, quadrature_tp = talib.HT_PHASOR(mult_tp)
        bars['ht_ph_tp'] = inphase_tp
        bars['ht_q_tp'] = quadrature_tp

    # Hilbert Transform - SineWave
    if cl:
        sine_cl, leadsine_cl = talib.HT_SINE(mult_close)
        bars['ht_s_cl'] = sine_cl
        bars['ht_ls_cl'] = leadsine_cl

    if tp:
        sine_tp, leadsine_tp = talib.HT_SINE(mult_tp)
        bars['ht_s_tp'] = sine_tp
        bars['ht_ls_tp'] = leadsine_tp

    # Hilbert Transform - Trend vs Cycle Mode
    if cl:
        bars['ht_tr_cl'] = talib.HT_TRENDMODE(mult_close)

    if tp:
        bars['ht_tr_tp'] = talib.HT_TRENDMODE(mult_tp)


    bars.fillna(method='bfill', inplace=True)

    if return_df:
        return bars
Exemplo n.º 29
0
def calc_features(df):
    open = df['op']
    high = df['hi']
    low = df['lo']
    close = df['cl']
    volume = df['volume']

    orig_columns = df.columns

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

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

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

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

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

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

    return df
Exemplo n.º 30
0
# i cannot say no word.
# use double derivative instead.
derivative=(lambda k0: [(k0[m+1]-k0[m]) for m in range(len(k0)-1)])
# to illustrate this:
for r,k in enumerate(nothing):
    print(r,k)
    vm=wrap(k)
    upperband, middleband, lowerband = talib.BBANDS(vm, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0)
    print(upperband,middleband,lowerband)
    print("-----jigsaw-----")
    print(list(talib.WMA(vm, timeperiod=30)))
    print(list(talib.TRIMA(vm, timeperiod=30)))
    print(list(talib.TEMA(vm, timeperiod=30)))
    print(list(talib.T3(vm, timeperiod=5, vfactor=0)))
    print(list(talib.SMA(vm, timeperiod=30)))
    print(list(talib.MIDPOINT(vm, timeperiod=14)))
#    print(list(talib.MAMA(vm, fastlimit=0, slowlimit=0)))
    print(list(talib.MA(vm, timeperiod=30, matype=0)))
    print(list(talib.KAMA(vm, timeperiod=30)))
    print(list(talib.HT_TRENDLINE(vm)))
    # you have the trend here?
    print(list(talib.EMA(vm, timeperiod=30)))
    print(list(talib.DEMA(vm, timeperiod=30)))
    # reading is like a survey, so as the stock market.
    if len(k)>3:
        # we have got some variable periods here, how do we see this?
        # only if we can conclude.
        high0=derivative(k)
        high=wrap(high0[:-1])
        low=wrap(derivative(high0))
        print(list(talib.SAREXT(high,low, startvalue=0, offsetonreverse=0, accelerationinitlong=0, accelerationlong=0, accelerationmaxlong=0, accelerationinitshort=0, accelerationshort=0, accelerationmaxshort=0)))