def get_rsi(source, debug_param=False):
    '''
    ### Generate RSI


    # from talib import RSI
    # import ta   #https://github.com/bukosabino/ta
    #import pandas_ta as ta  # https://github.com/twopirllc/pandas-ta
    '''

    if not debug_param:
        rsiList = [2, 3, 5, 9, 10, 14, 20, 25]
    else:
        rsiList = [9, 14]

    close = source['Close']
    rsi_features = pd.DataFrame(index=source.index)

    for i in rsiList:
        # rsi(close, length=None, drift=None, offset=None, **kwargs)
        rsicol = ta.rsi(close, length=i)
        rsi_features = rsi_features.join(rsicol)

    print("Number of features: {}".format(rsi_features.shape))
    print(rsi_features.head(10))

    return rsi_features
Пример #2
0
 def get_df(symbol='BTCUSDT', tf='1h') -> pd.DataFrame:
     interval_dict = {
         '1m': Client.KLINE_INTERVAL_1MINUTE,
         '5m': Client.KLINE_INTERVAL_5MINUTE,
         '15m': Client.KLINE_INTERVAL_15MINUTE,
         '1h': Client.KLINE_INTERVAL_1HOUR,
         '4h': Client.KLINE_INTERVAL_4HOUR,
         '1d': Client.KLINE_INTERVAL_1DAY,
         '1w': Client.KLINE_INTERVAL_1WEEK,
         '1M': Client.KLINE_INTERVAL_1MONTH,
     }
     klines = client.futures_klines(symbol=symbol, interval=interval_dict[tf])
     rows_list = []
     for kline in klines:
         row_dict = {
             'date': datetime.fromtimestamp(int(str(kline[0])[0:10])),
             'open': float(kline[1]),
             'high': float(kline[2]),
             'low': float(kline[3]),
             'close': float(kline[4]),
             'volume': float(kline[5]),
         }
         rows_list.append(row_dict)
     df = pd.DataFrame(rows_list)
     df['rsi'] = ta.rsi(df.close, 14)
     df = pd.concat((df, ta.macd(df.close, 12, 26, 9)), axis=1)
     df['ema_20'], df['ema_50'] = ta.ema(df.close, 20), ta.ema(df.close, 50)
     if len(df) >= 288:
         df['ema_200'] = ta.ema(df.close, 200)
     else:
         df['ema_200'] = ta.ema(df.close, len(df.close)-3)
     df.set_index('date', inplace=True)
     df.rename_axis('date', inplace=True)
     df = df.tail(88)
     return df
Пример #3
0
def get_RSI(new_data, funds):
    RSI = ta.rsi(new_data.close)
    current_rsi = RSI.iloc[-1]
    RSI_gradient = RSI.diff().iloc[-1]
    mean_rsi = np.nanmean(RSI[-1-100:-1])
    RSI_difference = current_rsi - mean_rsi
    return (RSI, current_rsi, RSI_gradient, mean_rsi, RSI_difference)
Пример #4
0
def bollinger_plot(company_name, new_df, time=0):
    fig, ax = plt.subplots(4, figsize=(15, 15))
    x_axis = new_df.index[time:]
    ax[0].fill_between(x_axis, new_df["Upper"][time:], new_df["Lower"][time:], color="grey")
    ax[0].plot(x_axis, new_df["close"][time:], color="Gold", lw=2, label="Close price")
    ax[0].plot(x_axis, new_df["SMA"][time:], color="Blue", lw=2, label="SMA")
    ax[1].plot((ta.coppock(new_df.close)))
    (buy_signal, sell_signal, buy_index, sell_index) = get_coppock(new_df)
    ax[1].scatter(buy_index, buy_signal, color="green", label="Buy signal", marker="^", alpha=1)
    ax[1].scatter(sell_index, sell_signal, color="red", label="Sell signal", marker="v", alpha=1)
    MACD = ta.macd(new_df.close, fast = 5, slow = 35, signal = 5)
    buy_signal, sell_signal, buy_index, sell_index = get_MACD(new_df, fast = 5, slow = 35, signal = 5)
    ax[2].bar(MACD.iloc[:,1].index, MACD.iloc[:,1], label="bar")
    ax[2].plot(MACD.iloc[:,0] , label="MACD")
    ax[2].plot(MACD.iloc[:,2], label="MACDS")
    ax[2].scatter(buy_index, buy_signal, color="red", label="sell signal", marker="v", alpha=1)
    ax[2].scatter(sell_index, sell_signal, color="green", label="buy signal", marker="^", alpha=1)
    ax[2].legend()
    ax[1].legend()
    ax[0].scatter(x_axis, new_df["Bollinger Buy"][time:], color="green", label="Buy signal", marker="^", alpha=1)
    ax[0].scatter(x_axis, new_df["Bollinger Sell"][time:], color="red", label="Sell signal", marker="v", alpha=1)
    ax[0].set_title(company_name)
    ax[3].plot(ta.rsi(new_df.close))
    ax[3].axhline(30, linestyle="--", color="red")
    ax[3].axhline(70, linestyle="--", color="green")
    plt.legend()
    plt.savefig("{}".format(str(datetime.datetime.now().strftime("%Y-%m-%d %H-%M-%S")) + ".png"))
def get_rsi_difference(source):
    '''
    # RSI shift, in which direction it is moving
    #import pandas_ta as ta  # https://github.com/twopirllc/pandas-ta
    ### Generate RSI difference

    '''
    rsiList = [2, 3, 5, 9, 10, 14, 20, 25]
    #rsiList = [9, 14]

    rsi_values = rsiList
    close = source['Close']
    rsi_change_features = pd.DataFrame(index=source.index)

    for period in rsi_values:
        rsi = ta.rsi(close, length=period)
        # Other column, here the same column shifted to find out if the direction changes
        rsi_diff = rsi - rsi.shift(1)
        rsi_diff.name = 'RSI' + str(period) + '_diff'
        rsi_change_features = rsi_change_features.join(rsi_diff)

    print("Number of features: {}".format(rsi_change_features.shape))
    print(rsi_change_features.head(10))

    return rsi_change_features
Пример #6
0
def get_data():
    database = sqliteDb()
    currency = request.args.get('currency')
    timeframe = request.args.get('timeframe')
    indicator_name = request.args.get('indicator_name')
    indicator_period = request.args.get('indicator_period')
    candle = request.args.get('candle')
    response = database.getData(currency, timeframe)
    database.close()
    df = pd.DataFrame.from_records(
        response,
        columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
    if indicator_name == 'EMA':
        indicator = ta.ema(df['close'], length=int(indicator_period))
    elif indicator_name == 'RSI':
        indicator = ta.rsi(df['close'], length=int(indicator_period))
    indicator = indicator.fillna('undefined')
    dict = indicator.to_dict()
    result = {
        'open value': response[99 - int(candle)][1],
        'high value': response[99 - int(candle)][2],
        'low value': response[99 - int(candle)][3],
        'close value': response[99 - int(candle)][4],
        'timestamp': response[99 - int(candle)][0],
        indicator_name: dict[99 - int(candle)]
    }
    return json.dumps(result)
Пример #7
0
def candel_hits(df, riz_thresh=40):
    import talib
    import pandas_ta as pta
    df['SHORTLINE'] = talib.CDLSHORTLINE(df.open, df.high, df.low, df.close)

    df['RSI-2'] = pta.rsi(df.close, legnth=2)
    df['candel_hit'] = (df['SHORTLINE'] == 100) & (df['RSI-2'] < riz_thresh)

    df['candelscale'] = df['candel_hit'].replace(True, 1).replace(1, df.close)

    df[['high', 'low', 'candelscale']].iplot(theme='solar',
                                             fill=True,
                                             title='SHORTLINE CANDEL')

    candel_control = df.drop(['open', 'high', 'low'], axis=1)
    candel_hit = candel_control['candel_hit'][-1] == True
    print(candel_hit)
    candel_control = pd.DataFrame(candel_control.iloc[-1])

    import pyttsx3

    eng = pyttsx3.init()
    eng.setProperty("rate", 150)

    if candel_hit == True:
        eng.say("WE HAVE {} CANDEL HIT ON D M T K ".format(candel_name))
        eng.runAndWait()
    return candel_control
    def on_5min_bar(self, bar: BarData):
        """"""
        self.cancel_all()

        self.am5.update_bar(bar)
        if not self.am5.inited:
            return

        if not self.ma_trend:
            return

        close_5 = pd.Series(self.am5.close_array)
        self.rsi_value = ta.rsi(close_5, self.rsi_window).iloc[-1]

        if self.pos == 0:
            if self.ma_trend > 0 and self.rsi_value >= self.rsi_long:
                self.buy(bar.close_price + 5, self.fixed_size)
            elif self.ma_trend < 0 and self.rsi_value <= self.rsi_short:
                self.short(bar.close_price - 5, self.fixed_size)

        elif self.pos > 0:
            if self.ma_trend < 0 or self.rsi_value < 50:
                self.sell(bar.close_price - 5, abs(self.pos))

        elif self.pos < 0:
            if self.ma_trend > 0 or self.rsi_value > 50:
                self.cover(bar.close_price + 5, abs(self.pos))

        self.put_event()
Пример #9
0
def indicator_adder(df):
    '''
    this adds the most common indicators to a dataframe
    with lower case close open high low volume
    takes: dataframe
    returns : dataframe
    '''
    df['rsi'] = pta.rsi(df.close)
    df['riz'] = pta.rsi(df.close,length=2)
    df['vwap'] = pta.vwap(df.high,df.low,df.close,df.volume)
    df[['stoch_k','stoch_d']] = pta.stoch(df.high,df.low,df.close)
    if len(df)>20:
        df['ema'] = pta.ema(df.close,length=20)
    
    #if len(df)>6:
        #df        = super_trend(df)
    return df
Пример #10
0
def MACD_backtest(df, funds = 10, difference_coef = 2, current_coef = 1e01, intercept = funds/2, win = 0, loss = 0,
    gradient = True, gradient_threshold = 0.1, difference_bool = True, sell_params = True, difference_threshold = 10, fast = 12, slow = 26, signal = 9,
    gain_list = [], loss_list = [], kelly = 0.5, RSI_bool = True, RSI_buy_parameter = 40, RSI_sell_parameter = 60):
    original_funds = funds
    RSI = ta.rsi(df.close)
    funds = funds
    (buy_signal, sell_signal, sell_index, buy_index) = CryptoFunctions.get_MACD(df, fast = fast, slow = slow, signal = signal)
    MACD = ta.macd(df.close)
    if sell_index[0] < buy_index[0]:
        del sell_index[0]
    if buy_index[-1] > sell_index[-1]:
        del buy_index[-1]
    total_profit = 0
    total_exposure = 0
    length = len(buy_index)
    for index in range(len(buy_index)):
        current_rsi = RSI[buy_index[index]]
        if buy_index[index] <= 50:
            mean_rsi = np.nanmean(RSI[buy_index[index] - 20:buy_index[index]])
        else:
            mean_rsi = np.nanmean(RSI[buy_index[index]-50:buy_index[index]])
        RSI_difference = current_rsi - mean_rsi
        if gradient and difference_bool and RSI_bool:
            #print("COP")
            macd_buy_gradient = MACD.iloc[:,0].diff()[buy_index[index]]
            macd_sell_gradient = MACD.iloc[:,0].diff()[sell_index[index]]
            #if macd_buy_gradient <= gradient_threshold and RSI_difference >= -difference_threshold and
            if current_rsi > RSI_buy_parameter:
                if index + 1 < length:
                    index += 1
            if sell_params:
                #if macd_sell_gradient >= -gradient_threshold and RSI_difference <= difference_threshold and \
                if current_rsi < RSI_sell_parameter:
                    if index + 1 < length:
                        index += 1
        #print(kelly)
        amount_to_invest = kelly*funds
        total_exposure += amount_to_invest
        funds -= amount_to_invest
        buy_price = df.iloc[buy_index[index],4]
        held = amount_to_invest/buy_price
        sell_price = df.iloc[sell_index[index],4]
        funds += sell_price*held
        difference = sell_price - buy_price
        if difference >= 0:
            win += 1
            gain_list.append(difference*held)
        else:
            loss += 1
            loss_list.append(difference*held)
        total_profit += held*difference
        fees = 0.0025*amount_to_invest + 0.0025*sell_price*held
        total_profit -= fees
    gain_percentage = (total_profit/original_funds)*100
    return (total_profit, total_exposure, total_exposure/gain_percentage, gain_percentage, win, loss, gain_list, loss_list)
Пример #11
0
    def on_bar(self, bar: BarData):
        """
        Callback of new bar data update.
        """
        self.cancel_all()

        am = self.am
        am.update_bar(bar)
        if not am.inited:
            return

        high = pd.Series(am.high_array)
        low = pd.Series(am.low_array)
        close = pd.Series(am.close_array)

        atr_array = ta.atr(
            high, low, close,
            self.atr_length)  #am.atr(self.atr_length, array=True)
        self.atr_value = atr_array.iloc[-1]
        self.atr_ma = atr_array.iloc[-self.atr_ma_length:].mean()
        self.rsi_value = ta.rsi(close, self.rsi_length).iloc[-1]

        if self.pos == 0:
            self.intra_trade_high = bar.high_price
            self.intra_trade_low = bar.low_price

            if self.atr_value > self.atr_ma:
                if self.rsi_value > self.rsi_buy:
                    self.buy(bar.close_price + 5, self.fixed_size)
                elif self.rsi_value < self.rsi_sell:
                    self.short(bar.close_price - 5, self.fixed_size)

        elif self.pos > 0:
            self.intra_trade_high = max(self.intra_trade_high, bar.high_price)
            self.intra_trade_low = bar.low_price

            long_stop = self.intra_trade_high * \
                (1 - self.trailing_percent / 100)
            self.sell(long_stop, abs(self.pos), stop=True)

        elif self.pos < 0:
            self.intra_trade_low = min(self.intra_trade_low, bar.low_price)
            self.intra_trade_high = bar.high_price

            short_stop = self.intra_trade_low * \
                (1 + self.trailing_percent / 100)
            self.cover(short_stop, abs(self.pos), stop=True)

        self.put_event()
Пример #12
0
def rsi(s_interval: str, df_stock: pd.DataFrame, length: int, scalar: float,
        drift: int) -> pd.DataFrame:
    """Relative strength index

    Parameters
    ----------
    s_interval: str
        Stock time interval
    df_stock: pd.DataFrame
        Dataframe of prices
    length: int
        Length of window
    scalar: float
        Scalar variable
    drift: int
        Drift variable

    Returns
    ----------
    df_ta: pd.DataFrame
        Dataframe of technical indicator
    """
    # Daily
    if s_interval == "1440min":
        df_ta = ta.rsi(df_stock["Adj Close"],
                       length=length,
                       scalar=scalar,
                       drift=drift).dropna()

    # Intraday
    else:
        df_ta = ta.rsi(df_stock["Close"],
                       length=length,
                       scalar=scalar,
                       drift=drift).dropna()
    return pd.DataFrame(df_ta)
Пример #13
0
    def on_bar(self, bar: BarData):
        """
        Callback of new bar data update.
        """
        self.am.update_bar(bar)
        if not self.am.inited:
            self.set_signal_pos(0)

        close = pd.Series(self.am.close_array)

        rsi_value = ta.rsi(close, self.rsi_window).iloc[-1]

        if rsi_value >= self.rsi_long:
            self.set_signal_pos(1)
        elif rsi_value <= self.rsi_short:
            self.set_signal_pos(-1)
        else:
            self.set_signal_pos(0)
Пример #14
0
    def extract_features(self):
        min_max_scaler = preprocessing.MinMaxScaler()

        # RSI
        # ref in https://arxiv.org/pdf/1911.10107.pdf
        rsi = ta.rsi(self.data.close, length=28)
        normalized_rsi = rsi.to_numpy().reshape(-1,
                                                1) / 100 - 0.5  # (-0.5, 0.5)

        # normalized close
        # ref in https://arxiv.org/pdf/1911.10107.pdf
        min_max_scaler = preprocessing.MinMaxScaler()
        normalized_close = min_max_scaler.fit_transform(
            self.data.close.to_frame()).reshape(-1, 1) - 0.5

        # normalized return
        # ref in https://arxiv.org/pdf/1911.10107.pdf
        ret_window = 60
        ret_60 = self.data.close / self.data.close.shift(ret_window) - 1
        normalized_ret_60 = ret_60 / ta.stdev(
            ret_60, ret_window)  #ret_60.rolling(ret_window).std()
        normalized_ret_60 = normalized_ret_60.to_numpy().reshape(-1, 1)

        ret_window = 120
        ret_120 = self.data.close / self.data.close.shift(ret_window) - 1
        normalized_ret_120 = ret_120 / ta.stdev(ret_120, ret_window)
        normalized_ret_120 = normalized_ret_120.to_numpy().reshape(-1, 1)

        # MACD indicators
        # ref in https://arxiv.org/pdf/1911.10107.pdf
        qt = (ta.sma(self.data.close, 30) -
              ta.sma(self.data.close, 60)) / ta.stdev(self.data.close, 60)
        macd = qt / ta.stdev(qt, 60)
        macd = macd.to_numpy().reshape(-1, 1)

        # concat features
        features = np.concatenate(
            (normalized_rsi, normalized_close, normalized_ret_60,
             normalized_ret_120, macd),
            axis=1)
        self.features = features
def get_rsi_signal(source):
    '''
    ### RSIx < value
    If RSI3 < 2 give signal, buying signal

    '''

    close = source['Close']
    rsi_signal_features = pd.DataFrame(index=source.index)

    # If RSI3 < 2 give signal, buying signal
    rsi3 = ta.rsi(close, length=3)
    rsi3_signal = (rsi3 < 5) * 1
    rsi3_decay_signal = generate_smoothed_trigger(rsi3_signal)
    rsi_signal_features = rsi_signal_features.join(
        pd.DataFrame(rsi3_decay_signal, columns=['RSI' + str(3) + 'sign']))

    print("Number of features: {}".format(rsi_signal_features.shape))
    print(rsi_signal_features[rsi_signal_features['RSI3sign'] == 1].head(5))

    return rsi_signal_features
Пример #16
0
    def checkLongSignal(self, i = None):
        if self.holding:
            return False

        df = self.dataframe
        if i == None:
            i = len(df) - 1

        close = self.df_15min["close"].iloc[-1]
        close_24h = self.df_15min["close"].iloc[len(self.df_15min) - 96]
        change24h = close/close_24h

        ema25 = ta.ema(self.df_15min['close'], 25)
        ema50 = ta.ema(self.df_15min['close'], 50)
        rsi = ta.rsi(self.df_15min['close'], self.rsi_len)
        entry_signal = close <= ema25.iloc[-1]*self.ema_trigger and rsi.iloc[-1] <= self.rsi_threshold and ema50.iloc[-1] > ema25.iloc[-1]

        if entry_signal:
            self.bot_controller.disable_entry = True
            self.holding = True

        return entry_signal
Пример #17
0
def rsi(values: pd.Series, length: int, scalar: float, drift: int) -> pd.DataFrame:
    """Relative strength index

    Parameters
    ----------
    values: pd.Series
        Dataframe of prices
    length: int
        Length of window
    scalar: float
        Scalar variable
    drift: int
        Drift variable

    Returns
    ----------
    pd.DataFrame
        Dataframe of technical indicator
    """
    return pd.DataFrame(
        ta.rsi(values, length=length, scalar=scalar, drift=drift).dropna()
    )
Пример #18
0
def generate_features_data():
    market_data_df = pd.DataFrame(
        columns=['timestamp', 'high', 'low', 'open', 'close', 'volume'])
    market_data_df = market_data_df.append(data())
    timestamp_df = market_data_df['timestamp'].copy(deep=True)
    slow_vwma_value_df = pd.DataFrame(
        vwma(market_data_df.close, market_data_df.volume, length=15, offset=0))
    fast_vwma_value_df = pd.DataFrame(
        vwma(market_data_df.close, market_data_df.volume, length=5, offset=0))
    slow_sma_value_df = pd.DataFrame(sma(market_data_df.close, 15))
    fast_sma_value_df = pd.DataFrame(sma(market_data_df.close, 5))
    rsi_data_value_df = pd.DataFrame(rsi(market_data_df.close, 10))

    slow_vwma_df = pd.concat([timestamp_df, slow_vwma_value_df], axis=1)
    fast_vwma_df = pd.concat([timestamp_df, fast_vwma_value_df], axis=1)
    slow_sma_df = pd.concat([timestamp_df, slow_sma_value_df], axis=1)
    fast_sma_df = pd.concat([timestamp_df, fast_sma_value_df], axis=1)
    rsi_df = pd.concat([timestamp_df, rsi_data_value_df], axis=1)

    slow_vwma_df.to_csv('slow_vwma_data.csv', index=False)
    fast_vwma_df.to_csv('fast_vwma_data.csv', index=False)
    slow_sma_df.to_csv('slow_sma_data.csv', index=False)
    fast_sma_df.to_csv('fast_sma_data.csv', index=False)
    rsi_df.to_csv('rsi_data.csv', index=False)
Пример #19
0
        json.dump(quotes, f, indent=4)

    return quotes


# only take most frequent stocks
symbols = Counter(df["Ticker"]).most_common(400)
symbols = [s[0] for s in symbols]
quotes = prefetch_agg(symbols)
bad_tickers = []

# calc TA
for ticker in tqdm(symbols, total=len(symbols), desc="Calculating TA"):
    try:
        quotes_df = pd.DataFrame(quotes[ticker]).transpose()
        quotes_df["rsi10"] = ta.rsi(quotes_df["c"], length=10)
        quotes_df["volumeEMA"] = ta.ema(quotes_df["v"], length=10)
        quotes[ticker] = quotes_df.to_dict(orient="index")
    except:
        bad_tickers.append(ticker)

# keep track of counts and reset each day
day = arrow.get(df["Time"].iloc[0].format("YYYY-MM-DD"))
counter = Counter()

data, rows = [], []
for idx, row in tqdm(df.iterrows(), total=len(df)):
    # new day - reset counter
    current_day = arrow.get(row["Time"].format("YYYY-MM-DD"))
    if current_day > day:
        counter = Counter()
Пример #20
0
def get_RSI(data_series_of_target_feature, lookback_value):
    return panda.rsi(data_series_of_target_feature, length=lookback_value)
Пример #21
0
def add_rsi():
    data['RSI'] = ta.rsi(data['4. close'])
Пример #22
0
def riz_delta(df,DN_THRESH=7,UP_THRESH=95,buy_col=True,riz_to_high_level=60,riz_len= 2,limit_lookback=False,time_delta=52,plot=True,return_df=True):
    '''
    RETURNS:
        1.riz - results_dataFrame
        2. original df - if return_df==true
    
    Makes Riz Targets
        COLUMNS:
            1.rix_up - first up corner coming out of extream levels
            2.rix_dn - first dn corner coming out of extream levels
    TODO:        
            3.riz_buy
    '''
    li = []

    #Function
    if limit_lookback==True:
        last_year = datetime.now() - pd.Timedelta(weeks=time_delta)
        print('lastyear',last_year)
        df = df[df.index > last_year]

    from stealing_fire import sola,hl
    import pandas_ta as pta


    df['riz'] = pta.rsi(df.close,length=riz_len)
    d = {}
    DN_THRESH = 7
    UP_THRESH = 95

    #saving results for  documenting thee research
    #d['sheet']     = sheet
    d['DN_THRESH'] = DN_THRESH
    d['UP_THRESH'] = UP_THRESH

    print('DN_THRESH:',DN_THRESH)
    print('UP_THRESH:',UP_THRESH)



    df['low_ex'] = df['riz'] < DN_THRESH
    df['high_ex']= df['riz'] > UP_THRESH

    hl(df)

    # How Often do Riz Corners Signal a Push?

    hl(df)
    df['rix_up'] = (df['low_ex']==False) & (df['low_ex'].shift()==True)
    df['rix_dn'] = (df['high_ex']==False) & (df['high_ex'].shift()==True)
    hl(df)

    df['rix_bionary'] = False

    rib = df['rix_bionary']
    rix = df['rix_up']
    rixd= df['rix_dn']

    for i in range(1,len(df)):
        if rix[i] == True:
            rib[i] = True
        elif rixd[i] == True:
            rib[i] = False
        else:
            rib[i] = rib[i-1]

    hl(df)

    df['swix'] = rib != rib.shift()

    rixdf = df[df['swix'] == True]

    rixdf['low_to_high_ex_diff'] = rixdf['close'] - rixdf['close'].shift()

    rixdf['positive_delta']      = rixdf['low_to_high_ex_diff'] > rixdf['low_to_high_ex_diff'].shift()
    rixdf['negitive_delta']      = rixdf['low_to_high_ex_diff'] < rixdf['low_to_high_ex_diff'].shift()

    
    hl(rixdf.groupby('rix_up').agg('mean'))

    dflen = len(rixdf)/2
    if plot == True:
        jenay(df,scale_two='high_ex',scale_one='low_ex')
        rixdf.groupby('rix_up').agg('mean')['low_to_high_ex_diff'].iplot(theme='solar',kind='bar')    
        rixdf.groupby('rix_up').agg('sum')[['positive_delta','negitive_delta']].iloc[0].plot(kind='pie')

    rdf = rixdf.groupby('rix_up').agg('sum')[['positive_delta','negitive_delta']]

    po = rdf['positive_delta'].iloc[0]
    ne = rdf['negitive_delta'].iloc[0]

    delta_percent = str(round(po/(po+ne)*100)) + ' %'
    print('delta_percent',delta_percent)
    d['delta_percent']        = delta_percent
    rdf['rix_delta_accuracy'] = delta_percent

    start_date = df.index[0]
    end_date   = df.index[-1]
    print('start_date',start_date)
    print('end_date',end_date)
    d['start_date'] = start_date
    d['end_date']   = end_date
    d['TOTALTIME'] = end_date - start_date
    d['positive_delta'] = po
    d['negitive_delta'] = ne

    if buy_col == True:
        df['buy'] =  (df['rix_up']==True) & (df['riz'] < riz_to_high_level)


    li.append(d)
    rrdf = pd.DataFrame(li)
    
    ouput = rrdf
    if return_df == True:
        output = [rrdf,df]
    return output
Пример #23
0
                            stochk, stochd, macd, macdh, macds, 'RSIsignal',
                            'RSIscore', 'BBsignal', 'BBscore', 'STOCHsignal',
                            'STOCHscore', 'MACDsignal', 'MACDscore',
                            'AverageScore', 'Signal'
                        ])

# loop through tickers to compute and store ta indicators values/scores
for ticker in tickers:
    # retrieve security data
    df = pdr.DataReader(ticker,
                        data_source='yahoo',
                        start=start_date,
                        end=end_date)

    # create ta indicators
    df[rsi] = ta.rsi(df['Adj Close'], length=rsi_length)  # rsi
    df[[lower_bband, mean,
        upper_bband]] = ta.bbands(df['Adj Close'],
                                  length=bband_length,
                                  std=bband_std)  # bollinger bands
    df[[stochk, stochd]] = ta.stoch(df.High,
                                    df.Low,
                                    df['Adj Close'],
                                    k=stoch_k,
                                    d=stoch_d,
                                    smooth_k=stoch_smoothk)
    df[[macd, macdh, macds]] = ta.macd(df['Adj Close'],
                                       fast=macd_fast,
                                       slow=macd_slow,
                                       signal=macd_signal)
Пример #24
0
                       ) - (df.tail(1).index.tolist()[0])
    # print(changes_of_time)
    dateposition_positive = df[df['Date'] ==
                               testdatefullstring].index.tolist()[0]
    # print(dateposition_positive)

    selected = df
    selected = selected.loc[:,
                            ['Date', 'Open', 'High', 'Low', 'Close', 'Volume']]
    # selected.set_index('Date', drop=True, inplace=True)
    # selected.index = pd.to_datetime(selected.index)
    total_row = len(selected.index)
    dateposition = -abs(total_row - dateposition_positive)
    # print(selected.iloc[dateposition])

    selected['rsiClose'] = ta.rsi(selected['Close'], 14)
    # selected['rsiClose'] = talib.RSI(selected['Close'], 14)
    selected.fillna(value={'rsiClose': 0}, inplace=True)
    selected.dropna(inplace=True)
    selected['fastk'], selected['fastd'] = talib.STOCH(
        selected['rsiClose'],
        selected['rsiClose'],
        selected['rsiClose'],
        fastk_period=14,
        slowk_period=3,
        slowk_matype=talib.MA_Type.SMA,
        slowd_period=3,
        slowd_matype=talib.MA_Type.SMA)
    selected['sma20'] = talib.SMA(selected['Close'], timeperiod=20)
    selected['sma50'] = talib.SMA(selected['Close'], timeperiod=50)
    selected['sma200'] = talib.SMA(selected['Close'], timeperiod=200)
Пример #25
0
def rsi(l_args, s_ticker, s_interval, df_stock):
    parser = argparse.ArgumentParser(
        prog='rsi',
        description=
        """ The Relative Strength Index (RSI) calculates a ratio of the 
                                     recent upward price movements to the absolute price movement. The RSI ranges 
                                     from 0 to 100. The RSI is interpreted as an overbought/oversold indicator when 
                                     the value is over 70/below 30. You can also look for divergence with price. If 
                                     the price is making new highs/lows, and the RSI is not, it indicates a reversal. """
    )

    parser.add_argument('-l',
                        "--length",
                        action="store",
                        dest="n_length",
                        type=check_positive,
                        default=14,
                        help='length')
    parser.add_argument('-s',
                        "--scalar",
                        action="store",
                        dest="n_scalar",
                        type=check_positive,
                        default=100,
                        help='scalar')
    parser.add_argument('-d',
                        "--drift",
                        action="store",
                        dest="n_drift",
                        type=check_positive,
                        default=1,
                        help='drift')
    parser.add_argument('-o',
                        "--offset",
                        action="store",
                        dest="n_offset",
                        type=check_positive,
                        default=0,
                        help='offset')

    try:
        (ns_parser, l_unknown_args) = parser.parse_known_args(l_args)

        if l_unknown_args:
            print(
                f"The following args couldn't be interpreted: {l_unknown_args}\n"
            )
            return

        # Daily
        if s_interval == "1440min":
            df_ta = ta.rsi(df_stock['5. adjusted close'],
                           length=ns_parser.n_length,
                           scalar=ns_parser.n_scalar,
                           drift=ns_parser.n_drift,
                           offset=ns_parser.n_offset).dropna()

            plt.subplot(211)
            plt.plot(df_stock.index,
                     df_stock['5. adjusted close'].values,
                     'k',
                     lw=2)
            plt.title(f"Relative Strength Index (RSI) on {s_ticker}")
            plt.xlim(df_stock.index[0], df_stock.index[-1])
            plt.ylabel(f'Share Price ($)')
            plt.grid(b=True, which='major', color='#666666', linestyle='-')
            plt.minorticks_on()
            plt.grid(b=True,
                     which='minor',
                     color='#999999',
                     linestyle='-',
                     alpha=0.2)
            plt.subplot(212)
            plt.plot(df_ta.index, df_ta.values, 'b', lw=2)
            plt.xlim(df_stock.index[0], df_stock.index[-1])
            plt.axhspan(70, 100, facecolor='r', alpha=0.2)
            plt.axhspan(0, 30, facecolor='g', alpha=0.2)
            plt.axhline(70, linewidth=3, color='r', ls='--')
            plt.axhline(30, linewidth=3, color='g', ls='--')
            plt.xlabel('Time')
            plt.grid(b=True, which='major', color='#666666', linestyle='-')
            plt.minorticks_on()
            plt.grid(b=True,
                     which='minor',
                     color='#999999',
                     linestyle='-',
                     alpha=0.2)
            plt.ylim([0, 100])
            plt.gca().twinx()
            plt.ylim(plt.gca().get_ylim())
            plt.yticks([.15, .85], ('OVERSOLD', 'OVERBOUGHT'))
            plt.show()

        # Intraday
        else:
            df_ta = ta.rsi(df_stock['4. close'],
                           length=ns_parser.n_length,
                           scalar=ns_parser.n_scalar,
                           drift=ns_parser.n_drift,
                           offset=ns_parser.n_offset).dropna()

            plt.subplot(211)
            plt.plot(df_stock.index, df_stock['4. close'].values, 'k', lw=2)
            plt.title(f"Relative Strength Index (RSI) on {s_ticker}")
            plt.xlim(df_stock.index[0], df_stock.index[-1])
            plt.ylabel(f'Share Price ($)')
            plt.grid(b=True, which='major', color='#666666', linestyle='-')
            plt.minorticks_on()
            plt.grid(b=True,
                     which='minor',
                     color='#999999',
                     linestyle='-',
                     alpha=0.2)
            plt.subplot(212)
            plt.plot(df_ta.index, df_ta.values, 'b', lw=2)
            plt.xlim(df_stock.index[0], df_stock.index[-1])
            plt.axhspan(70, 100, facecolor='r', alpha=0.2)
            plt.axhspan(0, 30, facecolor='g', alpha=0.2)
            plt.axhline(70, linewidth=3, color='r', ls='--')
            plt.axhline(30, linewidth=3, color='g', ls='--')
            plt.xlabel('Time')
            plt.grid(b=True, which='major', color='#666666', linestyle='-')
            plt.minorticks_on()
            plt.grid(b=True,
                     which='minor',
                     color='#999999',
                     linestyle='-',
                     alpha=0.2)
            plt.ylim([0, 100])
            plt.gca().twinx()
            plt.ylim(plt.gca().get_ylim())
            plt.yticks([.15, .85], ('OVERSOLD', 'OVERBOUGHT'))
            plt.show()
        print("")

    except:
        print("")
Пример #26
0
def add_plots(df: pd.DataFrame, additional_charts: Dict[str, bool]):
    """Add additional plots to the candle chart.

    Parameters
    ----------
    df : pd.DataFrame
        The source data
    additional_charts : Dict[str, bool]
        A dictionary of flags to include additional charts

    Returns
    -------
    Tuple
        Tuple of lists containing the plots, legends and subplot legends
    """
    panel_number = 2
    plots_to_add = []
    legends = []
    subplot_legends = []

    if additional_charts["ad"]:
        ad = ta.ad(df["High"], df["Low"], df["Close"], df["Volume"])
        ad_plot = mpf.make_addplot(ad, panel=panel_number)
        plots_to_add.append(ad_plot)
        subplot_legends.extend([panel_number * 2, ["AD"]])
        panel_number += 1

    if additional_charts["bbands"]:
        bbands = ta.bbands(df["Close"])
        bbands = bbands.drop("BBB_5_2.0", axis=1)
        bbands_plot = mpf.make_addplot(bbands, panel=0)
        plots_to_add.append(bbands_plot)
        legends.extend(["Lower BBand", "Middle BBand", "Upper BBand"])

    if additional_charts["cci"]:
        cci = ta.cci(df["High"], df["Low"], df["Close"])
        cci_plot = mpf.make_addplot(cci, panel=panel_number)
        plots_to_add.append(cci_plot)
        subplot_legends.extend([panel_number * 2, ["CCI"]])
        panel_number += 1

    if additional_charts["ema"]:
        ema = ta.ema(df["Close"])
        ema_plot = mpf.make_addplot(ema, panel=0)
        plots_to_add.append(ema_plot)
        legends.append("10 EMA")

    if additional_charts["rsi"]:
        rsi = ta.rsi(df["Close"])
        rsi_plot = mpf.make_addplot(rsi, panel=panel_number)
        plots_to_add.append(rsi_plot)
        subplot_legends.extend([panel_number * 2, ["RSI"]])
        panel_number += 1

    if additional_charts["obv"]:
        obv = ta.obv(df["Close"], df["Volume"])
        obv_plot = mpf.make_addplot(obv, panel=panel_number)
        plots_to_add.append(obv_plot)
        subplot_legends.extend([panel_number * 2, ["OBV"]])
        panel_number += 1

    if additional_charts["sma"]:
        sma_length = [20, 50]
        for length in sma_length:
            sma = ta.sma(df["Close"], length=length)
            sma_plot = mpf.make_addplot(sma, panel=0)
            plots_to_add.append(sma_plot)
            legends.append(f"{length} SMA")

    if additional_charts["vwap"]:
        vwap = ta.vwap(df["High"], df["Low"], df["Close"], df["Volume"])
        vwap_plot = mpf.make_addplot(vwap, panel=0)
        plots_to_add.append(vwap_plot)
        legends.append("vwap")

    return plots_to_add, legends, subplot_legends
Пример #27
0
from pandas_ta import rsi
from pandas_ta import bbands
import torch
from torch import nn
from sklearn.preprocessing import MinMaxScaler


# importing training data sets
from torch.autograd import Variable

data_cleaned = pd.read_csv('data_cleaned.csv')
data_cleaned = data_cleaned.assign(slow_vwma_value=(vwma(data_cleaned.close, data_cleaned.volume, length=2, offset=0)),
                                   fast_vwma_value=(vwma(data_cleaned.close, data_cleaned.volume, length=5, offset=0)),
                                   slow_sma_value=(sma(data_cleaned.close, 15)),
                                   fast_sma_value=(sma(data_cleaned.close, 5)),
                                   rsi_data_value=(rsi(data_cleaned.close, 10)),
                                   )
bbands_train = pd.DataFrame(bbands(data_cleaned.close, length=30, std=1.7))
data_cleaned = pd.concat([data_cleaned, bbands_train], axis=1)
# data_cleaned['timestamp'] = pd.to_datetime(data_cleaned['timestamp'], format='%Y-%m-%d %H')
data_cleaned = data_cleaned.set_index('timestamp')
data_cleaned = data_cleaned.dropna()
# print(data_cleaned)
# print(len(data_cleaned))

# importing test data sets
data_cleaned_test = pd.read_csv("data_cleaned_test.csv")
data_cleaned_test = data_cleaned_test.assign(
    slow_vwma_value=(vwma(data_cleaned_test.close, data_cleaned_test.volume, length=15, offset=0)),
    fast_vwma_value=(vwma(data_cleaned_test.close, data_cleaned_test.volume, length=5, offset=0)),
    slow_sma_value=(sma(data_cleaned_test.close, 15)),
Пример #28
0
def higher_highs_trendmap(df,plot=True,only_return_trend=False):#,return_frame=False):
    '''
    NEEDS TO BE TESTED!!! 
    TODO:
        1. VERIFY WITH tradingview
        2. a bunch of these loops if not all can go inside each other...
        
    tracks higher highs and higher lows, when you have one after the other 
       the column:
           trend_change_up == True

    RETURNS COLUMNS:
        'last_hl_uptrend' - when last high and last low were both higher
    '''
    
    df['riz'] = pta.rsi(df.close,legnth=2)

    df['up_corn'] = (df['riz']>df['riz'].shift()) & ( df['riz'].shift()<df['riz'].shift(2))
    df['dn_corn'] = (df['riz']<df['riz'].shift()) & ( df['riz'].shift()>df['riz'].shift(2))
    df['last_up_corn'] = 0.0
    df['last_dn_corn'] = 0.0
    df['higher_low']   = False
    df['higher_high']  = False
    df['lower_high']   = False
    df['lower_low']    = False

    for i in trange(1,len(df)):
        if df['up_corn'][i] == True:
            df['last_up_corn'][i] = df['close'][i]
        else:
            df['last_up_corn'][i] = df['last_up_corn'][i-1]
        if df['dn_corn'][i] == True:
            df['last_dn_corn'][i] = df['close'][i]
        else:
            df['last_dn_corn'][i] = df['last_dn_corn'][i-1]

        if (df['up_corn'][i] == True) & (df['last_up_corn'][i] > df['last_up_corn'][i-1]):
            df['higher_low'][i] = True
        if (df['dn_corn'][i] == True) & (df['last_dn_corn'][i] > df['last_dn_corn'][i-1]):
            df['higher_high'][i] = True

        if (df['up_corn'][i]==True) & (df['higher_low'][i]==False):
            df['lower_low'][i] = True
        if (df['dn_corn'][i] == True) & (df['higher_high'][i] == False):
            df['lower_high'][i] = True

    df['last_high_was_higher'] = False
    for i in trange(len(df)):
        if df['higher_high'][i] == True:
            df['last_high_was_higher'][i] = True
        elif df['lower_high'][i] == True:
            df['last_high_was_higher'][i] = False
        else:
            df['last_high_was_higher'][i] = df['last_high_was_higher'][i-1]

    df['last_high_was_lower'] = ( df['last_high_was_higher'] == False)     

    

    df['last_low_was_higher'] = False
    for i in trange(len(df)):
        if df['higher_low'][i] == True:
            df['last_low_was_higher'][i] = True
        elif df['higher_low'][i] == True:
            df['last_low_was_higher'][i] = False
        else:
            df['last_low_was_higher'][i] = df['last_low_was_higher'][i-1]

    df['last_low_was_lower'] = False
    for i in trange(len(df)):
        if df['lower_low'][i] == True:
            df['last_low_was_lower'][i] = True
        elif df['higher_low'][i] == True:
            df['last_low_was_lower'][i] = False
        else:
            df['last_low_was_lower'][i] = df['last_low_was_lower'][i-1]#.......................................................

    df['closee'] = df['close']  
    df['last_hl_uptrend'] = (df['last_high_was_higher']==True) & (df['last_low_was_higher']==True)
    df['last_low_was_higher'] = df['last_low_was_lower'] == False        
    df['trend_change']   = df['last_hl_uptrend'] != df['last_hl_uptrend'].shift()#...........................
    df['trend_change_up'] = (df['trend_change']==True) & ( df['last_hl_uptrend']==True)
    #df['datee']           = df.index.date
    if plot == True:
        df['last_hl_uptrend_scale'] = df['last_hl_uptrend'].replace(True,1).replace(1,df.close)
        df[['close','last_hl_uptrend_scale']].iplot(theme='solar',fill=True)
        jenay(df,scale_one='last_hl_uptrend',line_one='last_up_corn')



    if only_return_trend == True:
        loosers = ['riz','up_corn','dn_corn','last_up_corn','last_dn_corn','higher_low','higher_high','lower_high','lower_low','closee','last_low_was_higher','datee']
        df = df.drop(loosers,axis=1)
    
    return df
# Plot
st.header(f"Moving Average Convergence Divergence\n {company_name}")
st.line_chart(data[['macd', 'macdsignal']])

## CCI (Commodity Channel Index)
# CCI
cci = ta.trend.cci(data['High'],
                   data['Low'],
                   data['Close'],
                   window=31,
                   constant=0.015)

# Plot
st.header(f"Commodity Channel Index\n {company_name}")
st.line_chart(cci)

# ## RSI (Relative Strength Index)
# RSI
data['RSI'] = ta1.rsi(data['Adj Close'], timeperiod=14)

# Plot
st.header(f"Relative Strength Index\n {company_name}")
st.line_chart(data['RSI'])

# ## OBV (On Balance Volume)
# OBV
data['OBV'] = ta1.obv(data['Adj Close'], data['Volume']) / 10**6

# Plot
st.header(f"On Balance Volume\n {company_name}")
st.line_chart(data['OBV'])
Пример #30
0
def rsi(l_args, s_ticker, s_interval, df_stock):
    parser = argparse.ArgumentParser(
        add_help=False,
        prog="rsi",
        description="""
            The Relative Strength Index (RSI) calculates a ratio of the
            recent upward price movements to the absolute price movement. The RSI ranges
            from 0 to 100. The RSI is interpreted as an overbought/oversold indicator when
            the value is over 70/below 30. You can also look for divergence with price. If
            the price is making new highs/lows, and the RSI is not, it indicates a reversal.
        """,
    )

    parser.add_argument(
        "-l",
        "--length",
        action="store",
        dest="n_length",
        type=check_positive,
        default=14,
        help="length",
    )
    parser.add_argument(
        "-s",
        "--scalar",
        action="store",
        dest="n_scalar",
        type=check_positive,
        default=100,
        help="scalar",
    )
    parser.add_argument(
        "-d",
        "--drift",
        action="store",
        dest="n_drift",
        type=check_positive,
        default=1,
        help="drift",
    )
    parser.add_argument(
        "-o",
        "--offset",
        action="store",
        dest="n_offset",
        type=check_positive,
        default=0,
        help="offset",
    )

    try:
        ns_parser = parse_known_args_and_warn(parser, l_args)
        if not ns_parser:
            return

        # Daily
        if s_interval == "1440min":
            df_ta = ta.rsi(
                df_stock["Adj Close"],
                length=ns_parser.n_length,
                scalar=ns_parser.n_scalar,
                drift=ns_parser.n_drift,
                offset=ns_parser.n_offset,
            ).dropna()

        # Intraday
        else:
            df_ta = ta.rsi(
                df_stock["Close"],
                length=ns_parser.n_length,
                scalar=ns_parser.n_scalar,
                drift=ns_parser.n_drift,
                offset=ns_parser.n_offset,
            ).dropna()

        plt.figure(figsize=plot_autoscale(), dpi=PLOT_DPI)
        plt.subplot(211)
        if s_interval == "1440min":
            plt.plot(df_stock.index, df_stock["Adj Close"].values, "k", lw=2)
        else:
            plt.plot(df_stock.index, df_stock["Close"].values, "k", lw=2)
        plt.title(f"Relative Strength Index (RSI) on {s_ticker}")
        plt.xlim(df_stock.index[0], df_stock.index[-1])
        plt.ylabel("Share Price ($)")
        plt.grid(b=True, which="major", color="#666666", linestyle="-")
        plt.minorticks_on()
        plt.grid(b=True, which="minor", color="#999999", linestyle="-", alpha=0.2)
        plt.subplot(212)
        plt.plot(df_ta.index, df_ta.values, "b", lw=2)
        plt.xlim(df_stock.index[0], df_stock.index[-1])
        plt.axhspan(70, 100, facecolor="r", alpha=0.2)
        plt.axhspan(0, 30, facecolor="g", alpha=0.2)
        plt.axhline(70, linewidth=3, color="r", ls="--")
        plt.axhline(30, linewidth=3, color="g", ls="--")
        plt.xlabel("Time")
        plt.grid(b=True, which="major", color="#666666", linestyle="-")
        plt.minorticks_on()
        plt.grid(b=True, which="minor", color="#999999", linestyle="-", alpha=0.2)
        plt.ylim([0, 100])
        plt.gca().twinx()
        plt.ylim(plt.gca().get_ylim())
        plt.yticks([0.15, 0.85], ("OVERSOLD", "OVERBOUGHT"))

        if gtff.USE_ION:
            plt.ion()

        plt.show()

        print("")

    except Exception as e:
        print(e)
        print("")