ta-lib-python icon indicating copy to clipboard operation
ta-lib-python copied to clipboard

Talib RSI wrong values

Open josuhap opened this issue 4 years ago • 5 comments

I'm developing a small python script to get the RSI of a stock using TA-Lib. It was working fine for 6 months but now I realized the RSI function from TA-Lib is returning worng values:

while True: symbol = 'BTCUSDT' for period in periods: try: print(f'{symbol} - {period}') diff = ['1','2','3'] for hour in diff: ago = f'{hour} hours ago' datas = client.get_historical_klines(symbol,period, ago) for data in datas[:-1]: timestamp = converttime(data[0], 'full') price = round(float(data[4]),2) closes.append(price) np_closes = np.array(closes) erre = talib.RSI(np_closes, 14) rsis = erre.tolist() print(f'{timestamp} -> {symbol} -> {rsis[-1]} - {ago}')

Here an screenshot trying to get RSI from different timeframes (1m and 5m) with the difference of the candle numbers. image

I have a big question... If RSI function only gets last 14 values, why the value is so different between those tfs?

Correct value at 5m tf image

Correct value at 1m tf image

Version:

import talib talib.ta_version b'0.4.0 (Oct 16 2019 22:53:18)'

from talib import abstract stoch = abstract.Function('stoch') stoch.set_parameters(fastk_period=14, slowk_period=1, slowd_period=3, slowk_matype=0, slowd_matype=0) stoch.lookback 15

josuhap avatar Nov 29 '21 11:11 josuhap

You may better read the discussion about indicators with "memory effect" here: https://github.com/mrjbq7/ta-lib/issues/469

trufanov-nok avatar Nov 29 '21 14:11 trufanov-nok

Hello @trufanov-nok,

First of all thank you for your response, I have read that discussion but I think the problem is not the same.

To avoid "memory effect" about indicators, I use this commands:

closes = 0 np_closes = 0 erre = 0 rsis = 0 datas = 0

So I am assuming that with those commands I "reset" all the values of them.

In none of the cases the RSI is the correct (1m -> 1/2/3 hours ago and 5m -> 1/2/3 hours ago).

PD: I understand the "memory effect" but only I need is the last value rsis[-1], and that's a worng value

josuhap avatar Nov 29 '21 14:11 josuhap

I think you misunderstand "memory effect", it's not the memory used by Python objects, but rather the influence on your latest RSI value by all previous observed values.

mrjbq7 avatar Nov 29 '21 23:11 mrjbq7

I reproduced this code and assume the problem is in the difference between data returned to python via binance API and data displayed on graph online. I mean their klines' closes are different. Sometimes significantly. Could you confirm?

For the reference my test server code is:

from datetime import datetime
import numpy as np
import talib
from binance.client import Client
client = Client("ALUdiXPFeGbBRa533X8msQJNxp75st0F7W98uInhAg6pGFXD5OXcuDiLwkgjbRxy", "gl5Mcg9CnLOEzYWwN0z2hFgWSySsf3sI8AxzIOsDbItyvcGrk9cASmGxfDrwK3BF", testnet=True)


symbol = 'BTCUSDT'
periods = [Client.KLINE_INTERVAL_5MINUTE, Client.KLINE_INTERVAL_1MINUTE]
diff = ['1','2','3', '10']
closes = []

for period in periods:
    print(f'{symbol} - {period}')
    for hour in diff:
        ago = f'{hour} hours ago'
        datas = client.get_historical_klines(symbol,period, ago)
        for data in datas[:-1]: 
            timestamp = datetime.utcfromtimestamp(int(data[0]/1000)).strftime('%Y-%m-%d %H:%M:%S')
            price = round(float(data[4]),2)
            closes.append(price)
        np_closes = np.array(closes)
        
        erre = talib.RSI(np_closes, 14)
        rsis = erre.tolist()
        print(f'{timestamp} -> {symbol} -> {rsis[-1]} - {ago}')


trufanov-nok avatar Dec 02 '21 09:12 trufanov-nok

you must have a big data because stock calculate rsi from the first date but you use a specefic date, and this diffrent is effective on your calculate but if your date was long the diffrent is small sorry for my weak english:)

hossain93 avatar Jan 14 '22 17:01 hossain93