コード例 #1
0
 def calculate(self) -> pd.Series:
     """
     Calculates the simple moving average
     :return: Series containing the simple moving average values for each closing price
     """
     return momentum.rsi(self._history['Close'], self.__days)
     """
コード例 #2
0
def AddIndicators(df):
    # Add Simple Moving Average (SMA) indicators
    df["sma7"] = SMAIndicator(close=df["Close"], window=7,
                              fillna=True).sma_indicator()
    df["sma25"] = SMAIndicator(close=df["Close"], window=25,
                               fillna=True).sma_indicator()
    df["sma99"] = SMAIndicator(close=df["Close"], window=99,
                               fillna=True).sma_indicator()

    # Add Bollinger Bands indicator
    indicator_bb = BollingerBands(close=df["Close"], window=20, window_dev=2)
    df['bb_bbm'] = indicator_bb.bollinger_mavg()
    df['bb_bbh'] = indicator_bb.bollinger_hband()
    df['bb_bbl'] = indicator_bb.bollinger_lband()

    # Add Parabolic Stop and Reverse (Parabolic SAR) indicator
    indicator_psar = PSARIndicator(high=df["High"],
                                   low=df["Low"],
                                   close=df["Close"],
                                   step=0.02,
                                   max_step=2,
                                   fillna=True)
    df['psar'] = indicator_psar.psar()

    # Add Moving Average Convergence Divergence (MACD) indicator
    df["MACD"] = macd(close=df["Close"],
                      window_slow=26,
                      window_fast=12,
                      fillna=True)  # mazas

    # Add Relative Strength Index (RSI) indicator
    df["RSI"] = rsi(close=df["Close"], window=14, fillna=True)  # mazas

    return df
コード例 #3
0
def calculate_rsi(product, **kwargs):
    shortened_mfi = []
    days = 14
    better_start_date = fucking_date.now() - datetime.timedelta(days)
    better_end_date = fucking_date.now()
    shortened_mfi = client.get_product_historic_rates(product.id,
                                                      start=better_start_date,
                                                      end=better_end_date,
                                                      granularity=86400)
    shortened_mfi.reverse()
    #time.sleep(1)
    shortened_better_data = {
        'Open': [x[3] for x in shortened_mfi],
        'High': [x[2] for x in shortened_mfi],
        'Low': [x[1] for x in shortened_mfi],
        'Close': [x[4] for x in shortened_mfi],
        'Volume': [x[5] for x in shortened_mfi]
    }

    shortened_df = pd.notna(
        pd.DataFrame([x[1:] for x in shortened_mfi],
                     columns=['Open', 'High', 'Low', 'Close', 'Volume']))

    shortened_rsi_value = \
        rsi(pd.Series(data=[x[4] for x in shortened_mfi]), n=len(shortened_mfi)).values[-1]
    product.rsi = shortened_rsi_value
コード例 #4
0
    def get_state(self, t):
        """
        return the state (define state here) given time t
        """
        # time feature
        d = t - self.window_size
        if d <= 0:
            state = np.zeros((5, 5))
        else:
            feature1 = volume.acc_dist_index(
                high=self.OHLCV_df[d - 1:t]["High"],
                low=self.OHLCV_df[d - 1:t]["Low"],
                close=self.OHLCV_df[d - 1:t]["Adj Close"],
                volume=self.OHLCV_df[d - 1:t]['Volume'])
            feature2 = volatility.average_true_range(
                high=self.OHLCV_df[d - 1:t]["High"],
                low=self.OHLCV_df[d - 1:t]["Low"],
                close=self.OHLCV_df[d - 1:t]["Adj Close"])
            feature3 = trend.macd(close=self.OHLCV_df[d - 1:t]["Adj Close"],
                                  n_slow=self.window_size,
                                  n_fast=self.window_size // 2,
                                  fillna=True)
            feature4 = momentum.rsi(close=self.OHLCV_df[d - 1:t]["Adj Close"])
            feature5 = others.daily_return(
                close=self.OHLCV_df[d - 1:t]["Adj Close"])
            state = pd.concat(
                [feature1, feature2, feature3, feature4, feature5],
                axis=1).values[-5:, :]
            # block = self.timeseries[d-1:t]
            # state =  np.diff(np.log(block))
        state = state.flatten()
        state = np.append(state, (self.inventory, self.cash))

        return state
コード例 #5
0
        def rsi_cb_pressed(stock_analysis_tool: QMainWindow,
                           cb: QCheckBox) -> None:
            """

            :param stock_analysis_tool:
            :param cb:
            :return:
            """
            if cb.isChecked():
                # Add RSI to Display Graph
                stock_analysis_tool.df['rsi'] = rsi(
                    close=stock_analysis_tool.df['close'],
                    n=stock_analysis_tool.rsi_n)
                stock_analysis_tool.df['rsi overbought'] = 70
                stock_analysis_tool.df['rsi oversold'] = 30
                stock_analysis_tool.add_column_to_graph(column_name='rsi')
                stock_analysis_tool.add_column_to_graph(
                    column_name='rsi overbought',
                    color=stock_analysis_tool.protected_colors['red'])
                stock_analysis_tool.add_column_to_graph(
                    column_name='rsi oversold',
                    color=stock_analysis_tool.protected_colors['green'])
            else:
                # Remove RSI from Display Graph
                stock_analysis_tool.remove_column_from_graph(column_name='rsi')
                stock_analysis_tool.remove_column_from_graph(
                    column_name='rsi overbought')
                stock_analysis_tool.remove_column_from_graph(
                    column_name='rsi oversold')
                stock_analysis_tool.df = stock_analysis_tool.df.drop("rsi",
                                                                     axis=1)
                stock_analysis_tool.df = stock_analysis_tool.df.drop(
                    "rsi overbought", axis=1)
                stock_analysis_tool.df = stock_analysis_tool.df.drop(
                    "rsi oversold", axis=1)
コード例 #6
0
def RSI(
    nu, df
):  #Calcula RSI de un DF. Debe tener el timestamp en columna, no en index

    c = df.close
    rsi = ta.rsi(c, n=nu)
    return rsi
コード例 #7
0
        def rsi_time_frame_text_changed(stock_analysis_tool: QMainWindow,
                                        rsi_time_frame_text: QTextEdit):
            """

            :param stock_analysis_tool:
            :param rsi_time_frame_text:
            :return:
            """
            text = rsi_time_frame_text.toPlainText()

            if text != "":
                try:
                    stock_analysis_tool.rsi_n = int(text)
                    if 'rsi' in stock_analysis_tool.df.columns:
                        stock_analysis_tool.df = stock_analysis_tool.df.drop(
                            "rsi", axis=1)
                        stock_analysis_tool.df['rsi'] = rsi(
                            close=stock_analysis_tool.df['close'],
                            n=stock_analysis_tool.rsi_n)
                        x = [
                            datetime.timestamp(date_time)
                            for date_time in stock_analysis_tool.df.index
                        ]
                        stock_analysis_tool.data_lines['rsi'].setData(
                            x, stock_analysis_tool.df['rsi'])
                except ValueError:
                    print("Invalid RSI Input")
コード例 #8
0
def ta_only_forecastMomentum(price_data_csv):
    df = pandas.read_csv(filepath_or_buffer=price_data_csv.name,
                         delimiter=',',
                         header=0)
    df['time'] = pandas.to_datetime(
        df.time, unit='s')  # gotta convert those dates to a usable format
    data_frame = pandas.DataFrame(df).set_index('time').sort_values(
        by='time', ascending=True)

    # Fast Stochastic Oscillator Check
    SMA_Period = 3  # typical SMA period for S.O.
    data_frame['Stoch_D'] = momentum.stoch_signal(high=data_frame['high'],
                                                  low=data_frame['low'],
                                                  close=data_frame['close'],
                                                  n=15,
                                                  d_n=SMA_Period,
                                                  fillna=False)
    data_frame['Stoch_K'] = momentum.stoch(high=data_frame['high'],
                                           low=data_frame['low'],
                                           close=data_frame['close'],
                                           n=15,
                                           d_n=SMA_Period,
                                           fillna=False)

    data_frame['RSI'] = momentum.rsi(close=data_frame['close'], n=15)

    # predict based on signal
    signal = data_frame['Stoch_D'].array[-1] + data_frame['Stoch_K'].array[-1]
    RSI = data_frame['RSI'].array[-1]
    if signal > 160 and RSI > 70:  # overbought assets
        print(
            f"\nSO signal: {signal}\nRSI: {RSI}\nCLOSE: {data_frame['close'].array[-1]}"
        )
        return {
            'signal': 2,
            'RSI': RSI,
            'SO': signal,
            'PRICE': data_frame['close'].array[-1]
        }
    elif signal < 40 and RSI < 30:  # oversold asset
        print(
            f"\nSO signal: {signal}\nRSI: {RSI}\nCLOSE: {data_frame['close'].array[-1]}"
        )
        return {
            'signal': 0,
            'RSI': RSI,
            'SO': signal,
            'PRICE': data_frame['close'].array[-1]
        }
    else:  # not overbought/sold, should hold the asset
        print(
            f"\nSO signal: {signal}\nRSI: {RSI}\nCLOSE: {data_frame['close'].array[-1]}"
        )
        return {
            'signal': 1,
            'RSI': RSI,
            'SO': signal,
            'PRICE': data_frame['close'].array[-1]
        }
コード例 #9
0
ファイル: utils.py プロジェクト: yfjelley/stock
    def get_ml_feature(self, symbol, prices=None, cutoff=None):
        if prices:
            price = prices.get(symbol, 1E10)
            vix = prices['^VIX']
        else:
            price = self.closes[symbol][cutoff]
            vix = self.closes['^VIX'][cutoff]

        if cutoff:
            close = self.closes[symbol][cutoff - DAYS_IN_A_YEAR:cutoff]
            high = np.array(
                self.hists[symbol].get('High')[cutoff - DAYS_IN_A_YEAR:cutoff])
            low = np.array(
                self.hists[symbol].get('Low')[cutoff - DAYS_IN_A_YEAR:cutoff])
        else:
            close = self.closes[symbol][-DAYS_IN_A_YEAR:]
            high = np.array(self.hists[symbol].get('High')[-DAYS_IN_A_YEAR:])
            low = np.array(self.hists[symbol].get('Low')[-DAYS_IN_A_YEAR:])
        # Basic stats
        day_range_change = price / np.max(close[-DATE_RANGE:]) - 1
        today_change = price / close[-1] - 1
        yesterday_change = close[-1] / close[-2] - 1
        day_before_yesterday_change = close[-2] / close[-3] - 1
        twenty_day_change = price / close[-20] - 1
        year_high_change = price / np.max(close) - 1
        year_low_change = price / np.min(close) - 1
        all_changes = [
            close[t + 1] / close[t] - 1 for t in range(len(close) - 1)
            if close[t + 1] > 0 and close[t] > 0
        ]
        # Technical indicators
        close = np.append(close, price)
        high = np.append(high, price)
        low = np.append(low, price)
        pd_close = pd.Series(close)
        pd_high = pd.Series(high)
        pd_low = pd.Series(low)
        rsi = momentum.rsi(pd_close).values[-1]
        macd_rate = trend.macd_diff(pd_close).values[-1] / price
        wr = momentum.wr(pd_high, pd_low, pd_close).values[-1]
        tsi = momentum.tsi(pd_close).values[-1]
        feature = {
            'Today_Change': today_change,
            'Yesterday_Change': yesterday_change,
            'Day_Before_Yesterday_Change': day_before_yesterday_change,
            'Twenty_Day_Change': twenty_day_change,
            'Day_Range_Change': day_range_change,
            'Year_High_Change': year_high_change,
            'Year_Low_Change': year_low_change,
            'Change_Average': np.mean(all_changes),
            'Change_Variance': np.var(all_changes),
            'RSI': rsi,
            'MACD_Rate': macd_rate,
            'WR': wr,
            'TSI': tsi,
            'VIX': vix
        }
        return feature
コード例 #10
0
def get_technical_indicators(dataset, key='Close'):
    # Create 7 and 21 days Moving Average
    dataset['ma7'] = dataset[key].rolling(window=7).mean()
    dataset['ma21'] = dataset[key].rolling(window=21).mean()

    # Create MACD -> Moving Average Convergence/Divergence (trend and momentum indicator)
    dataset['26ema'] = dataset[key].ewm(span=26).mean()
    dataset['12ema'] = dataset[key].ewm(span=12).mean()
    dataset['MACD'] = (dataset['12ema'] - dataset['26ema'])

    # Create Bollinger Bands
    # Bollinger Bands are volatility bands placed above and below a moving average. Volatility is based on the standard deviation, which changes as volatility 
    # increases and decreases. The bands automatically widen when volatility increases and narrow when volatility decreases.
    dataset['20std'] = dataset[key].rolling(20).std()
    dataset['upper_band'] = dataset['ma21'] + (dataset['20std'] * 2)
    dataset['lower_band'] = dataset['ma21'] - (dataset['20std'] * 2)

    # Create Exponential moving average
    dataset['ema'] = dataset[key].ewm(com=0.5).mean()

    # Create Momentum
    dataset['momentum_1'] = dataset[key].diff(1)
    dataset['momentum_10'] = dataset[key].diff(10)

    # close price raw return 1, 10 days horizon
    # dataset['returnsClosePrevRaw1'] = dataset['Close'].pct_change(1)
    # dataset['returnsClosePrevRaw10'] = dataset['Close'].pct_change(10)

    # open price raw return 1, 10 days horizon
    # dataset['returnsOpenPrevRaw1'] = dataset['Open'].pct_change(1)
    # dataset['returnsOpenPrevRaw10'] = dataset['Open'].pct_change(10)

    # Create AROON - identifing when trends are likely to change direction, n=20
    dataset['aroon_up_20'] = dataset[key].rolling(20, min_periods=0).apply(lambda x: float(np.argmax(x) + 1) / 20 * 100,
                                                                           raw=True)
    dataset['aroon_down_20'] = dataset[key].rolling(20, min_periods=0).apply(
        lambda x: float(np.argmin(x) + 1) / 20 * 100, raw=True)

    # Create CCI, Commodity Channel Index
    pp = (dataset['High'] + dataset['Low'] + dataset[key]) / 3.0
    dataset['cci'] = (pp - pp.rolling(20, min_periods=0).mean()) / (0.015 * pp.rolling(20, min_periods=0).std())

    # Create STOCH - Stochastic Oscillator
    smin = dataset['Low'].rolling(14, min_periods=0).min()
    smax = dataset['High'].rolling(14, min_periods=0).max()
    dataset['stoch'] = 100 * (dataset[key] - smin) / (smax - smin)

    # Create RSI - Relative Strength Index
    dataset['rsi'] = rsi(dataset[key])

    # Create ADI - Accumulation/Distribution Index
    dataset['adi'] = acc_dist_index(dataset['High'], dataset['Low'], dataset[key], dataset['Volume'])

    # Create OBV - On-balance volume
    dataset['obv'] = on_balance_volume(dataset[key], dataset['Volume'])

    return dataset
コード例 #11
0
def RSI(df, col_name, intervals):
    """
        Relative Stock index
    """
    from ta.momentum import rsi
    from tqdm.auto import tqdm

    for interval in tqdm(intervals):
        df["rsi_" + str(interval)] = rsi(df[col_name], n=interval)
コード例 #12
0
def on_message_funct(ws, message):
    global candle_close_prices  # reference global variables

    json_message = json.loads(
        message
    )  # takes json message received from websocket and decodes to python

    candle = json_message['k']  # unpacking required data
    is_candle_closed = candle['x']
    close = candle['c']

    if is_candle_closed is True:  # collecting the required data
        candle_close_prices.append(close)
        np_candle_close_prices = numpy.array(candle_close_prices)
        start_point = len(np_candle_close_prices)
        print(start_point)

        print('candle closed at {}'.format(close))
        print('np_candle_close_prices')
        print(np_candle_close_prices)

        if start_point > rsi_period:  # creating an rsi !!!!!!!!!!!!!!!!
            print('URA')
            my_rsi = rsi(np_candle_close_prices, rsi_period, False)
            last_rsi = my_rsi[0][-1]

            print('all rsis are calculated to far')
            print(my_rsi)
            print('the last rsi is {}'.format(last_rsi))

            if last_rsi > rsi_overbought:  # execute selling order
                if in_position is True:
                    print('!!!!! SELL !!!!!')
                    order_succeeded = order(side=SIDE_SELL)
                    if order_succeeded is True:
                        in_position = False
                else:
                    print('It is overbought, but you are not in position')

            if last_rsi < rsi_oversold:  # execute buying order
                if in_position is True:
                    print('It is oversold, but you are already in position')
                else:
                    print('!!!!! BUY !!!!!')
                    order_succeeded = order(side=SIDE_BUY)
                    if order_succeeded is True:
                        in_position = True
コード例 #13
0
def RSI(datos, start, end= '', window = 10, colName = 'Adj Close'):
    '''
    ENTRADA
    datos: Pandas dataframe que contiene al menos una columna de fechas (DATE) y otra
    columna numérica

    start, end: strings en formato 'YYYY-MM-DD' representando la fecha de inicio
    y la fecha final respectivamente

    window: Entero que representa la ventan de tiempo a utilizar

    colName: String que representa el nombre de la columna con la cual se calculará el indicador

    SALIDA
    resultado: Dataframe datos con una columna extra conteniendo la información
    del indicador
    '''
    #Localiza la fecha de inicio y revisa si hay suficiente información
    indiceInicio=datos[datos['Date']==start].index[0]
    if window > indiceInicio + 1:
        print 'No hay suficiente historia para esta fecha'
        return datos

    #Último índice
    if end=='':
        lastIndex=datos.shape[0] - 1
    else:
        lastIndex=datos[datos['Date']==end].index[0]

    #calcula el indicador
    indicador = rsi(datos[colName], window)

    #agrega la nueva columna
    resultado = deepcopy(datos)
    resName = colName + '-RSI-' + str(window)
    resultado[resName] = indicador

    #Filtra a partir del índice correspondiente a la fecha start
    resultado=resultado.iloc[indiceInicio:lastIndex+1,:]
    resultado=resultado.reset_index(drop=True)

    #añade metadatos
    resultado.tipo = 'rsi'
    resultado.resName = resName

    return resultado
コード例 #14
0
def main():
    parser = argparse.ArgumentParser("RSI Alert")
    parser.add_argument("coins", type=str, nargs="+")
    parser.add_argument("--verbose", "-v", action="count", default=0)
    args = parser.parse_args()

    log_level = (3 - min(args.verbose, 2)) * 10
    logging.basicConfig(level=log_level)
    logging.debug(f"{args=}")

    iteration = 0

    while True:

        iteration += 1
        print(f"\n-- Iteration {iteration} --\n")

        for coin in args.coins:
            updating_message = f"Updating {coin}"
            logging.info(updating_message)
            print(updating_message)

            series = yf.download(tickers=f"{coin.upper()}-USD",
                                 period="22h",
                                 interval="1m")
            logging.debug(f"{series=}")

            closes = series["Close"]
            closes = closes.loc[closes != 0]
            logging.info(f"Series fetched for {coin=}")
            logging.debug(f"{closes=}")

            rsi_series = rsi(closes)
            logging.info(f"Calculated rsi for {coin=}")
            rsi_series = rsi_series.dropna()
            logging.debug(f"{rsi_series=}")
            last_rsi_val = int(rsi_series.iloc[-1])
            logging.info(f"{last_rsi_val=}")

            if last_rsi_val < 30 or last_rsi_val > 70:
                print(f"-- RSI ALERT for {coin}: {last_rsi_val} --\n")
                logging.info(f"RSI value alert for {coin}: {last_rsi_val}")
                alert()

        time.sleep(60)
コード例 #15
0
def apply_rsi(df, time_period, on_data="close"):
    """
    calculate the RSI on specified data type. return df with columns added.

    Parameters:

    df : pandas dataframe of ohlcv data
    time_period : iterable of length 2, specifying the range to vary the window
    on_data : str of data to calculate macd indicator on

    """
    ensure_correct_data_calculation_choice(on_data)
    df_new = pd.DataFrame(index=df.index)
    for period in range(time_period[0], time_period[1]):
        df_new["RSI_Period_" + str(period)] = rsi(df[on_data],
                                                  n=period,
                                                  fillna=True)
    return df_new
コード例 #16
0
 def S_RSI14(self, ticker):
     dailydata = self.rawdata.loc[self.rawdata['Symbol'] == ticker]
     rsi = momentum.rsi(dailydata.Close, n=14, fillna=True)
     if rsi[-1] < 40 or rsi[-1] > 60:
         interest = abs(((rsi[-1] / 100 - 0.5) * 2))**(1 / 3)
     else:
         return 0, ""
     if abs(rsi[-1] - 50) > 20:
         adj = "Strongly"
     else:
         adj = "moderately"
     if rsi[-1] > 50:
         state = "overbought"
     else:
         state = "oversold"
     statement = "{ticker}'s 14 day RSI is indicating that it is {adj} {state}".format(
         ticker=ticker, adj=adj, state=state)
     return interest, statement
コード例 #17
0
ファイル: MyClass.py プロジェクト: zlopez101/refactored-disco
    def apply_rsi(self, time_period, on_data="close"):
        """
        calculate the RSI on specified data type. return df with columns added.

        Parameters:

        df : pandas dataframe of ohlcv data
        time_period : iterable of length 2, specifying the range to vary the window
        on_data : str of data to calculate macd indicator on

        """
        assert len(
            time_period) == 2, "time_period must have a start and and end"
        self._ensure_data_calculation_choice(on_data)
        self.rsi = pd.DataFrame(index=self.data.index)
        for period in tqdm(range(time_period[0], time_period[1]),
                           desc="Applying RSI parameters"):
            self.rsi["RSI_" + str(period) + "_period"] = rsi(
                self.data[on_data], n=period)
コード例 #18
0
ファイル: Indicator.py プロジェクト: Drako06/TBotSignals
def marketChoice(interval):

    products = client.get_products()
    #market = 'MATICUSDT'

    data = products['data']
    criptos = []
    for elemento in data:
        #print(elemento['symbol'], elemento['market'])
        if elemento['market'] == 'USDT':
            criptos.append(elemento['symbol'])
    #print(criptos)
    print("Elejimos las USDT")

    ascendientes = []
    # tickers = client.get_ticker(symbol='OGNUSDT')
    for i in criptos:
        try:
            #print(i)
            tickers = client.get_ticker(symbol=i)
            #print(tickers)
            #print(float(tickers['quoteVolume']))
            if float(tickers['quoteVolume']) > 700000:  #float(tickers['priceChangePercent']) >= 0.3 and
                ascendientes.append([tickers['symbol'], tickers['priceChangePercent']])
        except:
            pass
    #print(len(ascendientes))
    print("Tenemos los Ascendientes")
    #print(ascendientes)

    """
    ascendientes = ['VETUSDT', 'MATICUSDT', 'XLMUSDT', 'BULLUSDT'] #, 'TRXUSDT', 'IOSTUSDT'
                    #'EOSUSDT']  # , 'ETCUSDT', 'BNBUSDT', 'XLMUSDT', 'MATICUSDT']
    """
    markets = []

    #ascendientes = ['VETUSDT', 'MATICUSDT', 'BULLUSDT', 'BEARUSDT', 'XLMUSDT', 'TRXUSDT', 'ICXUSDT']
    for j, k in ascendientes:
        dataset = client.get_klines(symbol=j, interval=interval, limit=20)
        data = pd.DataFrame(dataset, dtype=float)
        data.pop(1)
        data = data.iloc[:, :5]
        data = data.rename(
            columns={0: "FechaApertura", 2: 'ValorMaximo', 3: 'ValorMinimo', 4: 'Cierre', 5: 'Volumen'})
        columns = ['FechaApertura', 'ValorMaximo', 'ValorMinimo', 'Cierre', 'Volumen']
        data = data[columns]
        #a = len(data.index)

        #row = [fecha, maximo, minimo, cierre, volumen]
        # print(data.loc[len(data.index)])
        #data.loc[len(data.index)] = row
        data = pd.DataFrame(data, dtype=float)
        valorMaximo = data['ValorMaximo']
        valorMinimo = data['ValorMinimo']
        cierre = data['Cierre']
        volumen = data['Volumen']
        fecha = data['FechaApertura']
        a = len(data.index) - 1
        rsi = momentum.rsi(cierre, n=14, fillna=False)
        mfi = momentum.money_flow_index(valorMaximo, valorMinimo, cierre, volumen, n=14, fillna=True)
        boll_high = volatility.bollinger_hband(cierre, n=20, ndev=2, fillna=True)
        #boll_low = volatility.bollinger_lband(cierre, n=20, ndev=2, fillna=True)
        ganancia = abs(((cierre[a] * 100) / boll_high[a]) - 100)

        print(j, rsi[a-1], rsi[a], ganancia)
        print(j, mfi[a-1], mfi[a], ganancia)

        if 28 >= mfi[a] > mfi[a - 1] and ganancia > 0.5 and rsi[a-1] < rsi[a] < 40:
            markets.append(j)

    print(markets)
    if len(markets) != 0:
        market = random.choice(markets)

        print("Imprime el mercado elegido: " + market)

    #### VALIDAMOS LA TABLA PARA LA MONEDA SELECCIONADA

        return market
    else:
        print("No se han encontrado Mercados disponibles")
        return False
    #validTable(market)


    #### CREAMOS UNA INSTANCIA DE LA CLASE DE COMUNICACION CON EL WEBSOCKET


    """se hardcodea el mercado para volver a una tabla donde se quiere seguir"""


    #market = ['VETUSDT', 'MATICUSDT', 'XLMUSDT', 'BULLUSDT', 'BEARUSDT']
    #market = 'VETUSDT'
    #market = random.choice(market)
    print("Imprime el mercado elegido: " + market)
    #market = 'XLMUSDT'
    return market
コード例 #19
0
ファイル: run.py プロジェクト: seyedalirahimi/tehran-stocks
from ta.momentum import rsi

if __name__ == "__main__":
    _rsi14 = rsi(Closes, 14)
コード例 #20
0
ファイル: database-1546289772.py プロジェクト: palisadoes/AI
    def __dataframe(self):
        """Create an comprehensive list of  from data.

        Args:
            None

        Returns:
            result: dataframe for learning

        """
        # Calculate the percentage and real differences between columns
        difference = math.Difference(self._ohlcv)
        num_difference = difference.actual()
        pct_difference = difference.relative()

        # Create result to return.
        result = pd.DataFrame()

        # Add current value columns
        result['open'] = self._ohlcv['open']
        result['high'] = self._ohlcv['high']
        result['low'] = self._ohlcv['low']
        result['close'] = self._ohlcv['close']
        result['volume'] = self._ohlcv['volume']

        # Add columns of differences
        result['num_diff_open'] = num_difference['open']
        result['num_diff_high'] = num_difference['high']
        result['num_diff_low'] = num_difference['low']
        result['num_diff_close'] = num_difference['close']
        result['pct_diff_open'] = pct_difference['open']
        result['pct_diff_high'] = pct_difference['high']
        result['pct_diff_low'] = pct_difference['low']
        result['pct_diff_close'] = pct_difference['close']
        result['pct_diff_volume'] = pct_difference['volume']

        # Add date related columns
        # result['day'] = self._dates.day
        result['weekday'] = self._dates.weekday
        # result['week'] = self._dates.week
        result['month'] = self._dates.month
        result['quarter'] = self._dates.quarter
        # result['dayofyear'] = self._dates.dayofyear

        # Moving averages
        result['ma_open'] = result['open'].rolling(
            self._globals['ma_window']).mean()
        result['ma_high'] = result['high'].rolling(
            self._globals['ma_window']).mean()
        result['ma_low'] = result['low'].rolling(
            self._globals['ma_window']).mean()
        result['ma_close'] = result['close'].rolling(
            self._globals['ma_window']).mean()
        result['ma_volume'] = result['volume'].rolling(
            self._globals['vma_window']).mean()
        result['ma_volume_long'] = result['volume'].rolling(
            self._globals['vma_window_long']).mean()
        result[
            'ma_volume_delta'] = result['ma_volume_long'] - result['ma_volume']

        # Standard deviation related
        result['ma_std_close'] = result['close'].rolling(
            self._globals['ma_window']).std()
        result['std_pct_diff_close'] = result['pct_diff_close'].rolling(
            self._globals['ma_window']).std()
        result['bollinger_lband'] = volatility.bollinger_lband(result['close'])
        result['bollinger_hband'] = volatility.bollinger_lband(result['close'])
        result[
            'bollinger_lband_indicator'] = volatility.bollinger_lband_indicator(
                result['close'])
        result[
            'bollinger_hband_indicator'] = volatility.bollinger_hband_indicator(
                result['close'])

        # Rolling ranges
        result['amplitude'] = result['high'] - result['low']

        _min = result['low'].rolling(self._globals['week']).min()
        _max = result['high'].rolling(self._globals['week']).max()
        result['amplitude_medium'] = abs(_min - _max)

        _min = result['low'].rolling(2 * self._globals['week']).min()
        _max = result['high'].rolling(2 * self._globals['week']).max()
        result['amplitude_long'] = abs(_min - _max)

        _min = result['volume'].rolling(self._globals['week']).min()
        _max = result['volume'].rolling(self._globals['week']).max()
        result['vol_amplitude'] = abs(_min - _max)

        _min = result['volume'].rolling(2 * self._globals['week']).min()
        _max = result['volume'].rolling(2 * self._globals['week']).max()
        result['vol_amplitude_long'] = abs(_min - _max)

        # Volume metrics
        result['force_index'] = volume.force_index(result['close'],
                                                   result['volume'])
        result['negative_volume_index'] = volume.negative_volume_index(
            result['close'], result['volume'])
        result['ease_of_movement'] = volume.ease_of_movement(
            result['high'], result['low'], result['close'], result['volume'])
        result['acc_dist_index'] = volume.acc_dist_index(
            result['high'], result['low'], result['close'], result['volume'])
        result['on_balance_volume'] = volume.on_balance_volume(
            result['close'], result['volume'])
        result['on_balance_volume_mean'] = volume.on_balance_volume(
            result['close'], result['volume'])
        result['volume_price_trend'] = volume.volume_price_trend(
            result['close'], result['volume'])

        # Calculate the Stochastic values
        result['k'] = momentum.stoch(result['high'],
                                     result['low'],
                                     result['close'],
                                     n=self._globals['kwindow'])

        result['d'] = momentum.stoch_signal(result['high'],
                                            result['low'],
                                            result['close'],
                                            n=self._globals['kwindow'],
                                            d_n=self._globals['dwindow'])

        # Calculate the Miscellaneous values
        result['rsi'] = momentum.rsi(result['close'],
                                     n=self._globals['rsiwindow'],
                                     fillna=False)

        miscellaneous = math.Misc(self._ohlcv)
        result['proc'] = miscellaneous.proc(self._globals['proc_window'])

        # Calculate ADX
        result['adx'] = trend.adx(result['high'],
                                  result['low'],
                                  result['close'],
                                  n=self._globals['adx_window'])

        # Calculate MACD difference
        result['macd_diff'] = trend.macd_diff(
            result['close'],
            n_fast=self._globals['macd_sign'],
            n_slow=self._globals['macd_slow'],
            n_sign=self._globals['macd_sign'])

        # Create series for increasing / decreasing closes (Convert NaNs to 0)
        _result = np.nan_to_num(result['pct_diff_close'].values)
        _increasing = (_result >= 0).astype(int) * self._buy
        _decreasing = (_result < 0).astype(int) * self._sell
        result['increasing'] = _increasing + _decreasing

        # Stochastic subtraciton
        result['k_d'] = pd.Series(result['k'].values - result['d'].values)

        # Other indicators
        result['k_i'] = self._stochastic_indicator(result['k'], result['high'],
                                                   result['low'],
                                                   result['ma_close'])
        result['d_i'] = self._stochastic_indicator(result['d'], result['high'],
                                                   result['low'],
                                                   result['ma_close'])
        result['stoch_i'] = self._stochastic_indicator_2(
            result['k'], result['d'], result['high'], result['low'],
            result['ma_close'])
        result['rsi_i'] = self._rsi_indicator(result['rsi'], result['high'],
                                              result['low'],
                                              result['ma_close'])
        result['adx_i'] = self._adx_indicator(result['adx'])
        result['macd_diff_i'] = self._macd_diff_indicator(result['macd_diff'])
        result['volume_i'] = self._volume_indicator(result['ma_volume'],
                                                    result['ma_volume_long'])

        # Create time shifted columns
        for step in range(1, self._ignore_row_count + 1):
            # result['t-{}'.format(step)] = result['close'].shift(step)
            result['tpd-{}'.format(step)] = result['close'].pct_change(
                periods=step)
            # result['tad-{}'.format(step)] = result[
            #    'close'].diff(periods=step)

        # Mask increasing with
        result['increasing_masked'] = _mask(result['increasing'].to_frame(),
                                            result['stoch_i'],
                                            as_integer=True).values

        # Get class values for each vector
        classes = pd.DataFrame(columns=self._shift_steps)
        for step in self._shift_steps:
            # Shift each column by the value of its label
            classes[step] = result[self._label2predict].shift(-step)

        # Remove all undesirable columns from the dataframe
        undesired_columns = ['open', 'close', 'high', 'low', 'volume']
        for column in undesired_columns:
            result = result.drop(column, axis=1)

        # Delete the firsts row of the dataframe as it has NaN values from the
        # .diff() and .pct_change() operations
        result = result.iloc[self._ignore_row_count:]
        classes = classes.iloc[self._ignore_row_count:]

        # Convert result to float32 to conserve memory
        result = result.astype(np.float32)

        # Return
        return result, classes
コード例 #21
0
ファイル: momentum.py プロジェクト: macsrc/ta
 def test_rsi2(self):
     target = 'RSI'
     result = rsi(**self._params)
     pd.testing.assert_series_equal(self._df[target].tail(),
                                    result.tail(),
                                    check_names=False)
コード例 #22
0
def RSI(df0):
    df = df0
    df['rsi'] = rsi(df['close'], 10, fillna=False)
    return df
コード例 #23
0
ファイル: database-1544220306.py プロジェクト: palisadoes/AI
    def __dataframe(self):
        """Create vectors from data.

        Args:
            shift_steps: List of steps

        Returns:
            result: dataframe for learning

        """
        # Calculate the percentage and real differences between columns
        difference = math.Difference(self._file_values())
        num_difference = difference.actual()
        pct_difference = difference.relative()

        # Create result to return.
        # Make sure it is float16 for efficient computing
        result = pd.DataFrame(columns=[
            'open', 'high', 'low', 'close', 'volume', 'increasing',
            'weekday', 'day', 'dayofyear', 'quarter', 'month', 'num_diff_open',
            'num_diff_high', 'num_diff_low', 'num_diff_close', 'pct_diff_open',
            'pct_diff_high', 'pct_diff_low', 'pct_diff_close',
            'k', 'd', 'rsi', 'adx', 'macd_diff', 'proc',
            'ma_open', 'ma_high', 'ma_low', 'ma_close',
            'ma_volume', 'ma_volume_long',
            'ma_volume_delta']).astype('float16')

        # Add current value columns
        result['open'] = self._file_values()['open']
        result['high'] = self._file_values()['high']
        result['low'] = self._file_values()['low']
        result['close'] = self._file_values()['close']
        result['volume'] = self._file_values()['volume']

        # Add columns of differences
        result['num_diff_open'] = num_difference['open']
        result['num_diff_high'] = num_difference['high']
        result['num_diff_low'] = num_difference['low']
        result['num_diff_close'] = num_difference['close']
        result['pct_diff_open'] = pct_difference['open']
        result['pct_diff_high'] = pct_difference['high']
        result['pct_diff_low'] = pct_difference['low']
        result['pct_diff_close'] = pct_difference['close']
        result['pct_diff_volume'] = pct_difference['volume']

        # Add date related columns
        result['day'] = self.dates().day
        result['weekday'] = self.dates().weekday
        result['week'] = self.dates().week
        result['month'] = self.dates().month
        result['quarter'] = self.dates().quarter
        result['dayofyear'] = self.dates().dayofyear

        # Moving averages
        result['ma_open'] = result['open'].rolling(
            self._globals['ma_window']).mean()
        result['ma_high'] = result['high'].rolling(
            self._globals['ma_window']).mean()
        result['ma_low'] = result['low'].rolling(
            self._globals['ma_window']).mean()
        result['ma_close'] = result['close'].rolling(
            self._globals['ma_window']).mean()
        result['ma_volume'] = result['volume'].rolling(
            self._globals['vma_window']).mean()
        result['ma_volume_long'] = result['volume'].rolling(
            self._globals['vma_window_long']).mean()
        result['ma_volume_delta'] = result[
            'ma_volume_long'] - result['ma_volume']

        # Calculate the Stochastic values
        stochastic = math.Stochastic(
            self._file_values(), window=self._globals['kwindow'])
        result['k'] = stochastic.k()
        result['d'] = stochastic.d(window=self._globals['dwindow'])

        # Calculate the Miscellaneous values
        result['rsi'] = momentum.rsi(
            result['close'],
            n=self._globals['rsiwindow'],
            fillna=False)

        miscellaneous = math.Misc(self._file_values())
        result['proc'] = miscellaneous.proc(self._globals['proc_window'])

        # Calculate ADX
        result['adx'] = trend.adx(
            result['high'],
            result['low'],
            result['close'],
            n=self._globals['adx_window'])

        # Calculate MACD difference
        result['macd_diff'] = trend.macd_diff(
            result['close'],
            n_fast=self._globals['macd_sign'],
            n_slow=self._globals['macd_slow'],
            n_sign=self._globals['macd_sign'])

        # Create series for increasing / decreasing closes (Convert NaNs to 0)
        _result = np.nan_to_num(result['pct_diff_close'].values)
        _increasing = (_result >= 0).astype(int) * 1
        _decreasing = (_result < 0).astype(int) * 0
        result['increasing'] = _increasing + _decreasing

        # Delete the first row of the dataframe as it has NaN values from the
        # .diff() and .pct_change() operations
        result = result.iloc[self._ignore_row_count:]

        # Return
        return result
コード例 #24
0
def AddIndicators(df):
    # Add Relative Strength Index (RSI) indicator
    df["RSI"] = rsi(close=df["Close"], window=14, fillna=True)

    return df
コード例 #25
0
ファイル: database.py プロジェクト: palisadoes/AI
    def __dataframe(self):
        """Create an comprehensive list of  from data.

        Args:
            None

        Returns:
            result: dataframe for learning

        """
        # Calculate the percentage and real differences between columns
        difference = math.Difference(self._ohlcv)
        num_difference = difference.actual()
        pct_difference = difference.relative()

        # Create result to return.
        result = pd.DataFrame()

        # Add current value columns
        # NOTE Close must be first for correct correlation column dropping
        result['close'] = self._ohlcv['close']
        result['open'] = self._ohlcv['open']
        result['high'] = self._ohlcv['high']
        result['low'] = self._ohlcv['low']
        result['volume'] = self._ohlcv['volume']

        # Add columns of differences
        # NOTE Close must be first for correct correlation column dropping
        result['num_diff_close'] = num_difference['close']
        result['pct_diff_close'] = pct_difference['close']

        result['num_diff_open'] = num_difference['open']
        result['pct_diff_open'] = pct_difference['open']

        result['num_diff_high'] = num_difference['high']
        result['pct_diff_high'] = pct_difference['high']

        result['num_diff_low'] = num_difference['low']
        result['pct_diff_low'] = pct_difference['low']
        result['pct_diff_volume'] = pct_difference['volume']

        # Add date related columns
        # result['day'] = self._dates.day
        result['weekday'] = self._dates.weekday
        # result['week'] = self._dates.week
        result['month'] = self._dates.month
        result['quarter'] = self._dates.quarter
        # result['dayofyear'] = self._dates.dayofyear

        # Moving averages
        result['ma_open'] = result['open'].rolling(
            self._globals['ma_window']).mean()
        result['ma_high'] = result['high'].rolling(
            self._globals['ma_window']).mean()
        result['ma_low'] = result['low'].rolling(
            self._globals['ma_window']).mean()
        result['ma_close'] = result['close'].rolling(
            self._globals['ma_window']).mean()
        result['ma_volume'] = result['volume'].rolling(
            self._globals['vma_window']).mean()
        result['ma_volume_long'] = result['volume'].rolling(
            self._globals['vma_window_long']).mean()
        result['ma_volume_delta'] = result[
            'ma_volume_long'] - result['ma_volume']

        # Standard deviation related
        result['ma_std_close'] = result['close'].rolling(
            self._globals['ma_window']).std()
        result['std_pct_diff_close'] = result['pct_diff_close'].rolling(
            self._globals['ma_window']).std()
        result['bollinger_lband'] = volatility.bollinger_lband(result['close'])
        result['bollinger_hband'] = volatility.bollinger_lband(result['close'])
        result['bollinger_lband_indicator'] = volatility.bollinger_lband_indicator(result['close'])
        result['bollinger_hband_indicator'] = volatility.bollinger_hband_indicator(result['close'])

        # Rolling ranges
        result['amplitude'] = result['high'] - result['low']

        _min = result['low'].rolling(
            self._globals['week']).min()
        _max = result['high'].rolling(
            self._globals['week']).max()
        result['amplitude_medium'] = abs(_min - _max)

        _min = result['low'].rolling(
            2 * self._globals['week']).min()
        _max = result['high'].rolling(
            2 * self._globals['week']).max()
        result['amplitude_long'] = abs(_min - _max)

        _min = result['volume'].rolling(
            self._globals['week']).min()
        _max = result['volume'].rolling(
            self._globals['week']).max()
        result['vol_amplitude'] = abs(_min - _max)

        _min = result['volume'].rolling(
            2 * self._globals['week']).min()
        _max = result['volume'].rolling(
            2 * self._globals['week']).max()
        result['vol_amplitude_long'] = abs(_min - _max)

        # Volume metrics
        result['force_index'] = volume.force_index(
            result['close'], result['volume'])
        result['negative_volume_index'] = volume.negative_volume_index(
            result['close'], result['volume'])
        result['ease_of_movement'] = volume.ease_of_movement(
            result['high'], result['low'], result['close'], result['volume'])
        result['acc_dist_index'] = volume.acc_dist_index(
            result['high'], result['low'], result['close'], result['volume'])
        result['on_balance_volume'] = volume.on_balance_volume(
            result['close'], result['volume'])
        result['on_balance_volume_mean'] = volume.on_balance_volume(
            result['close'], result['volume'])
        result['volume_price_trend'] = volume.volume_price_trend(
            result['close'], result['volume'])

        # Calculate the Stochastic values
        result['k'] = momentum.stoch(
            result['high'],
            result['low'],
            result['close'],
            n=self._globals['kwindow'])

        result['d'] = momentum.stoch_signal(
            result['high'],
            result['low'],
            result['close'],
            n=self._globals['kwindow'],
            d_n=self._globals['dwindow'])

        # Calculate the Miscellaneous values
        result['rsi'] = momentum.rsi(
            result['close'],
            n=self._globals['rsiwindow'],
            fillna=False)

        miscellaneous = math.Misc(self._ohlcv)
        result['proc'] = miscellaneous.proc(self._globals['proc_window'])

        # Calculate ADX
        result['adx'] = trend.adx(
            result['high'],
            result['low'],
            result['close'],
            n=self._globals['adx_window'])

        # Calculate MACD difference
        result['macd_diff'] = trend.macd_diff(
            result['close'],
            n_fast=self._globals['macd_sign'],
            n_slow=self._globals['macd_slow'],
            n_sign=self._globals['macd_sign'])

        # Create series for increasing / decreasing closes (Convert NaNs to 0)
        _result = np.nan_to_num(result['pct_diff_close'].values)
        _increasing = (_result >= 0).astype(int) * self._buy
        _decreasing = (_result < 0).astype(int) * self._sell
        result['increasing'] = _increasing + _decreasing

        # Stochastic subtraciton
        result['k_d'] = pd.Series(result['k'].values - result['d'].values)

        # Other indicators
        result['k_i'] = self._stochastic_indicator(
            result['k'], result['high'], result['low'], result['ma_close'])
        result['d_i'] = self._stochastic_indicator(
            result['d'], result['high'], result['low'], result['ma_close'])
        result['stoch_i'] = self._stochastic_indicator_2(
            result['k'], result['d'],
            result['high'], result['low'], result['ma_close'])
        result['rsi_i'] = self._rsi_indicator(
            result['rsi'], result['high'], result['low'], result['ma_close'])
        result['adx_i'] = self._adx_indicator(result['adx'])
        result['macd_diff_i'] = self._macd_diff_indicator(result['macd_diff'])
        result['volume_i'] = self._volume_indicator(
            result['ma_volume'], result['ma_volume_long'])

        # Create time shifted columns
        for step in range(1, self._ignore_row_count + 1):
            result['t-{}'.format(step)] = result['close'].shift(step)
            result['tpd-{}'.format(step)] = result[
                'close'].pct_change(periods=step)
            result['tad-{}'.format(step)] = result[
                'close'].diff(periods=step)

        # Mask increasing with
        result['increasing_masked'] = _mask(
            result['increasing'].to_frame(),
            result['stoch_i'],
            as_integer=True).values

        # Get class values for each vector
        classes = pd.DataFrame(columns=self._shift_steps)
        for step in self._shift_steps:
            # Shift each column by the value of its label
            if self._binary is True:
                # Classes need to be 0 or 1 (One hot encoding)
                classes[step] = (
                    result[self._label2predict].shift(-step) > 0).astype(int)
            else:
                classes[step] = result[self._label2predict].shift(-step)
            # classes[step] = result[self._label2predict].shift(-step)

        # Delete the firsts row of the dataframe as it has NaN values from the
        # .diff() and .pct_change() operations
        ignore = max(max(self._shift_steps), self._ignore_row_count)
        result = result.iloc[ignore:]
        classes = classes.iloc[ignore:]

        # Convert result to float32 to conserve memory
        result = result.astype(np.float32)

        # Return
        return result, classes
コード例 #26
0
ファイル: main.py プロジェクト: camilos0808/CryptoPython
def RSI(klines):
    rsi = momentum.rsi(klines['close'], n=14)

    return rsi.iloc[-1], klines['close'].iloc[-1]
コード例 #27
0
def engineer_data_over_single_interval(df: pd.DataFrame,
                                       indicators: list,
                                       ticker: str = "",
                                       rsi_n: int = 14,
                                       cmo_n: int = 7,
                                       macd_fast: int = 12,
                                       macd_slow: int = 26,
                                       macd_sign: int = 9,
                                       roc_n: int = 12,
                                       cci_n: int = 20,
                                       dpo_n: int = 20,
                                       cmf_n: int = 20,
                                       adx_n: int = 14,
                                       mass_index_low: int = 9,
                                       mass_index_high: int = 25,
                                       trix_n: int = 15,
                                       stochastic_oscillator_n: int = 14,
                                       stochastic_oscillator_sma_n: int = 3,
                                       ultimate_oscillator_short_n: int = 7,
                                       ultimate_oscillator_medium_n: int = 14,
                                       ultimate_oscillator_long_n: int = 28,
                                       ao_short_n: int = 5,
                                       ao_long_n: int = 34,
                                       kama_n: int = 10,
                                       tsi_high_n: int = 25,
                                       tsi_low_n: int = 13,
                                       eom_n: int = 14,
                                       force_index_n: int = 13,
                                       ichimoku_low_n: int = 9,
                                       ichimoku_medium_n: int = 26):
    from ta.momentum import rsi, wr, roc, ao, stoch, uo, kama, tsi
    from ta.trend import macd, macd_signal, cci, dpo, adx, mass_index, trix, ichimoku_a
    from ta.volume import chaikin_money_flow, acc_dist_index, ease_of_movement, force_index

    # Momentum Indicators
    if Indicators.RELATIVE_STOCK_INDEX in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.RELATIVE_STOCK_INDEX.value +
                           " for stock " + ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.RELATIVE_STOCK_INDEX.value] = rsi(close=df['close'],
                                                        n=rsi_n)

    if Indicators.WILLIAMS_PERCENT_RANGE in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.WILLIAMS_PERCENT_RANGE.value +
                           " for stock " + ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.WILLIAMS_PERCENT_RANGE.value] = wr(
            df['high'], df['low'], df['close'])

    if Indicators.CHANDE_MOMENTUM_OSCILLATOR in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.CHANDE_MOMENTUM_OSCILLATOR.value +
                           " for stock " + ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.CHANDE_MOMENTUM_OSCILLATOR.
           value] = chande_momentum_oscillator(close_data=df['close'],
                                               period=cmo_n)

    if Indicators.RATE_OF_CHANGE in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.RATE_OF_CHANGE.value + " for stock " +
                           ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.RATE_OF_CHANGE.value] = roc(close=df['close'], n=roc_n)

    if Indicators.STOCHASTIC_OSCILLATOR in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.STOCHASTIC_OSCILLATOR.value +
                           " for stock " + ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.STOCHASTIC_OSCILLATOR.value] = stoch(
            high=df['high'],
            low=df['low'],
            close=df['close'],
            n=stochastic_oscillator_n,
            d_n=stochastic_oscillator_sma_n)

    if Indicators.ULTIMATE_OSCILLATOR in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.ULTIMATE_OSCILLATOR.value +
                           " for stock " + ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.ULTIMATE_OSCILLATOR.value] = uo(
            high=df['high'],
            low=df['low'],
            close=df['close'],
            s=ultimate_oscillator_short_n,
            m=ultimate_oscillator_medium_n,
            len=ultimate_oscillator_long_n)

    if Indicators.AWESOME_OSCILLATOR in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.AWESOME_OSCILLATOR.value +
                           " for stock " + ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.AWESOME_OSCILLATOR.value] = ao(high=df['high'],
                                                     low=df['low'],
                                                     s=ao_short_n,
                                                     len=ao_long_n)

    if Indicators.KAUFMAN_ADAPTIVE_MOVING_AVERAGE in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.KAUFMAN_ADAPTIVE_MOVING_AVERAGE.value +
                           " for stock " + ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.KAUFMAN_ADAPTIVE_MOVING_AVERAGE.value] = kama(
            close=df['close'], n=kama_n)

    if Indicators.TRUE_STRENGTH_INDEX in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.TRUE_STRENGTH_INDEX.value +
                           " for stock " + ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.TRUE_STRENGTH_INDEX.value] = tsi(close=df['close'],
                                                       r=tsi_high_n,
                                                       s=tsi_low_n)

    # Trend Indicator
    if Indicators.MOVING_AVERAGE_CONVERGENCE_DIVERGENCE in indicators:
        Logger.console_log(
            message="Calculating " +
            Indicators.MOVING_AVERAGE_CONVERGENCE_DIVERGENCE.value +
            " for stock " + ticker,
            status=Logger.LogStatus.EMPHASIS)
        df[Indicators.MOVING_AVERAGE_CONVERGENCE_DIVERGENCE.value] = macd(close=df['close'],
                                                                          n_slow=macd_slow,
                                                                          n_fast=macd_fast) - \
                                                                     macd_signal(close=df['close'],
                                                                                 n_slow=macd_slow,
                                                                                 n_fast=macd_fast,
                                                                                 n_sign=macd_sign)

    if Indicators.COMMODITY_CHANNEL_INDEX in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.COMMODITY_CHANNEL_INDEX.value +
                           " for stock " + ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.COMMODITY_CHANNEL_INDEX.value] = cci(high=df['high'],
                                                           low=df['low'],
                                                           close=df['close'],
                                                           n=cci_n)

    if Indicators.DETRENDED_PRICE_OSCILLATOR in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.DETRENDED_PRICE_OSCILLATOR.value +
                           " for stock " + ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.DETRENDED_PRICE_OSCILLATOR.value] = dpo(
            close=df['close'], n=dpo_n)

    if Indicators.AVERAGE_DIRECTIONAL_INDEX in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.AVERAGE_DIRECTIONAL_INDEX.value +
                           " for stock " + ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.AVERAGE_DIRECTIONAL_INDEX.value] = adx(high=df['high'],
                                                             low=df['low'],
                                                             close=df['close'],
                                                             n=adx_n)

    if Indicators.MASS_INDEX in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.MASS_INDEX.value + " for stock " +
                           ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.MASS_INDEX.value] = mass_index(high=df['high'],
                                                     low=df['low'],
                                                     n=mass_index_low,
                                                     n2=mass_index_high)

    if Indicators.TRIPLE_EXPONENTIALLY_SMOOTHED_MOVING_AVERAGE in indicators:
        Logger.console_log(
            message="Calculating " +
            Indicators.TRIPLE_EXPONENTIALLY_SMOOTHED_MOVING_AVERAGE.value +
            " for stock " + ticker,
            status=Logger.LogStatus.EMPHASIS)
        df[Indicators.TRIPLE_EXPONENTIALLY_SMOOTHED_MOVING_AVERAGE.
           value] = trix(close=df['close'], n=trix_n)

    if Indicators.ICHIMOKU_A in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.ICHIMOKU_A.value + " for stock " +
                           ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.ICHIMOKU_A.value] = ichimoku_a(high=df['high'],
                                                     low=df['low'],
                                                     n1=ichimoku_low_n,
                                                     n2=ichimoku_medium_n)

    # Volume Indicator
    if Indicators.CHAIKIN_MONEY_FLOW in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.CHAIKIN_MONEY_FLOW.value +
                           " for stock " + ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.CHAIKIN_MONEY_FLOW.value] = chaikin_money_flow(
            high=df['high'],
            low=df['low'],
            close=df['close'],
            volume=df['volume'],
            n=cmf_n)

    if Indicators.ACCUMULATION_DISTRIBUTION_INDEX in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.ACCUMULATION_DISTRIBUTION_INDEX.value +
                           " for stock " + ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.ACCUMULATION_DISTRIBUTION_INDEX.value] = acc_dist_index(
            high=df['high'],
            low=df['low'],
            close=df['close'],
            volume=df['volume'])

    if Indicators.EASE_OF_MOVEMENT in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.EASE_OF_MOVEMENT.value + " for stock " +
                           ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.EASE_OF_MOVEMENT.value] = ease_of_movement(
            high=df['high'], low=df['low'], volume=df['volume'], n=eom_n)

    if Indicators.FORCE_INDEX in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.FORCE_INDEX.value + " for stock " +
                           ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.FORCE_INDEX.value] = force_index(close=df['close'],
                                                       volume=df['volume'],
                                                       n=force_index_n)
コード例 #28
0
import pandas as pd
import numpy as np
from ta.momentum import money_flow_index
from ta.momentum import rsi  # Relative Strength Index
from ta.momentum import stoch  # Stochastic Oscilator
from ta.momentum import uo  # Ultimate Oscilator
from ta.momentum import wr  #William Percent Range
from ta.trend import macd  # Moving Average Convergence/Divergence

# *************************** DATA PREPROCESSING ******************************

# Load Data
df = pd.read_csv('^GSPC.csv')

# Data Augmentation
df['Relative_Strength_Index'] = rsi(df['Close'])
df['Money_Flow_Index'] = money_flow_index(df['High'], df['Low'], df['Close'],
                                          df['Volume'])
df['Stoch_Oscilator'] = stoch(df['High'], df['Low'], df['Close'])
df['Ultimate_Oscilator'] = uo(df['High'], df['Low'], df['Close'])
df['William_Percent'] = wr(df['High'], df['Low'], df['Close'])
df['MACD'] = macd(df['Close'])

# Some indicators require many days in advance before they produce any
# values. So the begining rows of our df may have NaNs. Lets drop them:
df = df.dropna()

# Scaling Data
from sklearn.preprocessing import MinMaxScalern
sc = MinMaxScaler(feature_range=(0, 1))
scaled_df = sc.fit_transform(df.iloc[:, 1:].values)
コード例 #29
0
    def get_ml_feature(self, symbol, prices=None, cutoff=None):
        feature = {}
        if prices:
            price = prices.get(symbol, 1E10)
            vix = prices['^VIX']
        else:
            price = self.closes[symbol][cutoff]
            vix = self.closes['^VIX'][cutoff]

        if cutoff:
            close = self.closes[symbol][cutoff - DAYS_IN_A_YEAR:cutoff]
            volume = self.volumes[symbol][cutoff - DAYS_IN_A_YEAR:cutoff]
        else:
            close = self.closes[symbol][-DAYS_IN_A_YEAR:]
            volume = self.volumes[symbol][-DAYS_IN_A_YEAR:]
        close = np.append(close, price)

        # Log returns
        feature['Day_1_Return'] = np.log(close[-1] / close[-2])
        feature['Day_2_Return'] = np.log(close[-2] / close[-3])
        feature['Day_3_Return'] = np.log(close[-3] / close[-4])
        feature['Weekly_Return'] = np.log(price / close[-DAYS_IN_A_WEEK])
        feature['Monthly_Return'] = np.log(price / close[-DAYS_IN_A_MONTH])
        feature['Quarterly_Return'] = np.log(price / close[-DAYS_IN_A_QUARTER])
        feature['From_Weekly_High'] = np.log(price /
                                             np.max(close[-DAYS_IN_A_WEEK:]))
        feature['From_Weekly_Low'] = np.log(price /
                                            np.min(close[-DAYS_IN_A_WEEK:]))

        # Technical indicators
        pd_close = pd.Series(close)
        feature['RSI'] = momentum.rsi(pd_close).values[-1]
        feature['MACD_Rate'] = trend.macd_diff(pd_close).values[-1] / price
        feature['TSI'] = momentum.tsi(pd_close).values[-1]

        # Markets
        feature['VIX'] = vix

        # Other numerical factors
        # Fit five data points to a second order polynomial
        feature['Acceleration'] = (2 * close[-5] - 1 * close[-4] -
                                   2 * close[-3] - 1 * close[-2] +
                                   2 * close[-1]) / 14
        feature['Momentum'] = (-2 * close[-5] - 1 * close[-4] + 1 * close[-2] +
                               2 * close[-1]) / 10
        quarterly_returns = [
            np.log(close[i] / close[i - 1])
            for i in range(-DAYS_IN_A_QUARTER, -1)
        ]
        monthly_returns = quarterly_returns[-DAYS_IN_A_MONTH:]
        weekly_returns = quarterly_returns[-DAYS_IN_A_WEEK:]
        feature['Monthly_Skewness'] = stats.skew(monthly_returns)
        feature['Monthly_Volatility'] = np.std(monthly_returns)
        feature['Weekly_Skewness'] = stats.skew(weekly_returns)
        feature['Weekly_Volatility'] = np.std(weekly_returns)
        feature['Z_Score'] = (
            feature['Day_1_Return'] -
            np.mean(quarterly_returns)) / np.std(quarterly_returns)
        feature['Monthly_Avg_Dollar_Volume'] = np.average(
            np.multiply(close[-DAYS_IN_A_MONTH - 1:-1],
                        volume[-DAYS_IN_A_MONTH:])) / 1E6

        return feature
コード例 #30
0
ファイル: matplot.py プロジェクト: cjj208/tt
def bar():

    history = fx.get_history(instrument=symbol,
                             timeframe="m1",
                             quotes_count=300)
    # print (history)
    if history.size != 0:
        df = pd.DataFrame(history)
        df = df[[
            'Date',
            'BidOpen',
            'BidHigh',
            'BidLow',
            'BidClose',
            'Volume',
        ]]
        df.rename(columns={
            'Date': "datetime",
            'BidOpen': "open",
            "BidHigh": "high",
            "BidLow": "low",
            "BidClose": "close",
            'Volume': "volume",
        },
                  inplace=True)

        df['ema_f_h'] = trend.ema(df.high, periods=34)
        df['ema_f_c'] = trend.ema(df.close, periods=34)
        df['ema_f_l'] = trend.ema(df.low, periods=34)
        df['ema_s_h'] = trend.ema(df.high, periods=144)
        df['ema_s_c'] = trend.ema(df.close, periods=144)
        df['ema_s_l'] = trend.ema(df.low, periods=144)
        df['ema_across'] = np.where(df.ema_f_c > df.ema_s_c, int(1),
                                    int(-1))  # 上穿1,下穿-1
        # MACD(df, fast=55, slow=144, n=55)
        df["macd"] = trend.MACD(df.close, n_slow=144, n_fast=34,
                                n_sign=34).macd()
        df["macd_signal"] = trend.MACD(df.close,
                                       n_slow=144,
                                       n_fast=34,
                                       n_sign=34).macd_signal()
        df['macd_across'] = np.where(df.macd > df.macd_signal, int(1),
                                     int(-1))  # 上穿1,下穿-1

        df['rsi'] = momentum.rsi(
            df.close,
            n=34,
        )
        df['emarsi'] = trend.ema(df.rsi, periods=34)
        df['rsi_postion'] = np.where(df.rsi > df.emarsi, int(1),
                                     int(-1))  # 上穿1,下穿-1

        # df['stoch'] = momentum.stoch(high=df["high"],low=df["low"],close=df["close"],n=144)
        # df['stoch_signal'] = momentum.stoch_signal(high=df["high"],low=df["low"],close=df["close"],n=144)
        # df["stoch_sig"] = np.where(df["stoch"] > df["stoch_signal"], 1, 0)
        # df["stoch_sig_shfit"] = df["stoch_sig"].shift(1)

        df['cci'] = trend.cci(
            df.high,
            df.low,
            df.close,
            n=55,
        )
        df['emacci'] = trend.ema(df.cci, periods=55)
        df['cci_postion'] = np.where(df.cci > df.emacci, int(1),
                                     int(-1))  # 上穿1,下穿-1

        # 价格为于快线与慢线之上则为1,快慢线之下则为-1,其它为0
        df["close_postion"] = np.where(
            (df['close'] > df['ema_f_c']) & (df['close'] > df['ema_s_c']),
            int(1),
            np.where(
                (df['close'] < df['ema_f_c']) & (df['close'] < df['ema_s_c']),
                int(-1), int(0)))
        return df
コード例 #31
0
    def __dataframe(self):
        """Create vectors from data.

        Args:
            shift_steps: List of steps

        Returns:
            result: dataframe for learning

        """
        # Calculate the percentage and real differences between columns
        difference = math.Difference(self._file_values())
        num_difference = difference.actual()
        pct_difference = difference.relative()

        # Create result to return.
        # Make sure it is float16 for efficient computing
        result = pd.DataFrame(columns=[
            'open', 'high', 'low', 'close', 'volume', 'increasing', 'weekday',
            'day', 'dayofyear', 'quarter', 'month', 'num_diff_open',
            'num_diff_high', 'num_diff_low', 'num_diff_close', 'pct_diff_open',
            'pct_diff_high', 'pct_diff_low', 'pct_diff_close', 'k', 'd', 'rsi',
            'adx', 'macd_diff', 'proc', 'ma_open', 'ma_high', 'ma_low',
            'ma_close', 'ma_volume', 'ma_volume_long', 'ma_volume_delta'
        ]).astype('float16')

        # Add current value columns
        result['open'] = self._file_values()['open']
        result['high'] = self._file_values()['high']
        result['low'] = self._file_values()['low']
        result['close'] = self._file_values()['close']
        result['volume'] = self._file_values()['volume']

        # Add columns of differences
        result['num_diff_open'] = num_difference['open']
        result['num_diff_high'] = num_difference['high']
        result['num_diff_low'] = num_difference['low']
        result['num_diff_close'] = num_difference['close']
        result['pct_diff_open'] = pct_difference['open']
        result['pct_diff_high'] = pct_difference['high']
        result['pct_diff_low'] = pct_difference['low']
        result['pct_diff_close'] = pct_difference['close']
        result['pct_diff_volume'] = pct_difference['volume']

        # Add date related columns
        result['day'] = self.dates().day
        result['weekday'] = self.dates().weekday
        result['week'] = self.dates().week
        result['month'] = self.dates().month
        result['quarter'] = self.dates().quarter
        result['dayofyear'] = self.dates().dayofyear

        # Moving averages
        result['ma_open'] = result['open'].rolling(
            self._globals['ma_window']).mean()
        result['ma_high'] = result['high'].rolling(
            self._globals['ma_window']).mean()
        result['ma_low'] = result['low'].rolling(
            self._globals['ma_window']).mean()
        result['ma_close'] = result['close'].rolling(
            self._globals['ma_window']).mean()
        result['ma_volume'] = result['volume'].rolling(
            self._globals['vma_window']).mean()
        result['ma_volume_long'] = result['volume'].rolling(
            self._globals['vma_window_long']).mean()
        result[
            'ma_volume_delta'] = result['ma_volume_long'] - result['ma_volume']

        # Calculate the Stochastic values
        stochastic = math.Stochastic(self._file_values(),
                                     window=self._globals['kwindow'])
        result['k'] = stochastic.k()
        result['d'] = stochastic.d(window=self._globals['dwindow'])

        # Calculate the Miscellaneous values
        result['rsi'] = momentum.rsi(result['close'],
                                     n=self._globals['rsiwindow'],
                                     fillna=False)

        miscellaneous = math.Misc(self._file_values())
        result['proc'] = miscellaneous.proc(self._globals['proc_window'])

        # Calculate ADX
        result['adx'] = trend.adx(result['high'],
                                  result['low'],
                                  result['close'],
                                  n=self._globals['adx_window'])

        # Calculate MACD difference
        result['macd_diff'] = trend.macd_diff(
            result['close'],
            n_fast=self._globals['macd_sign'],
            n_slow=self._globals['macd_slow'],
            n_sign=self._globals['macd_sign'])

        # Create series for increasing / decreasing closes (Convert NaNs to 0)
        _result = np.nan_to_num(result['pct_diff_close'].values)
        _increasing = (_result >= 0).astype(int) * 1
        _decreasing = (_result < 0).astype(int) * 0
        result['increasing'] = _increasing + _decreasing

        # Delete the first row of the dataframe as it has NaN values from the
        # .diff() and .pct_change() operations
        result = result.iloc[self._ignore_row_count:]

        # Return
        return result
コード例 #32
0
                df['ema_across'] = np.where(df.ema_f_c > df.ema_s_c, int(1),
                                            int(-1))  # 上穿1,下穿-1
                # MACD(df, fast=55, slow=144, n=55)
                df["macd"] = trend.MACD(df.close,
                                        n_slow=144,
                                        n_fast=34,
                                        n_sign=34).macd()
                df["macd_signal"] = trend.MACD(df.close,
                                               n_slow=144,
                                               n_fast=34,
                                               n_sign=34).macd_signal()
                df['macd_across'] = np.where(df.macd > df.macd_signal, int(1),
                                             int(-1))  # 上穿1,下穿-1

                df['rsi'] = momentum.rsi(
                    df.close,
                    n=34,
                )
                df['emarsi'] = trend.ema(df.rsi, periods=34)
                df['rsi_postion'] = np.where(df.rsi > df.emarsi, int(1),
                                             int(-1))  # 上穿1,下穿-1

                # df['stoch'] = momentum.stoch(high=df["high"],low=df["low"],close=df["close"],n=144)
                # df['stoch_signal'] = momentum.stoch_signal(high=df["high"],low=df["low"],close=df["close"],n=144)
                # df["stoch_sig"] = np.where(df["stoch"] > df["stoch_signal"], 1, 0)
                # df["stoch_sig_shfit"] = df["stoch_sig"].shift(1)

                df['cci'] = trend.cci(
                    df.high,
                    df.low,
                    df.close,
                    n=55,