TA-Lib / ta-lib-python

Python wrapper for TA-Lib (http://ta-lib.org/).
http://ta-lib.github.io/ta-lib-python
Other
9.27k stars 1.72k forks source link

RSI Indicator Requires 1 More Value Than the ‘timeperiod’ Specifies #603

Open TrainedPro opened 1 year ago

TrainedPro commented 1 year ago

Summary:

The RSI (Relative Strength Index) indicator in the TA-Lib library requires one more value than the specified 'timeperiod'. This behavior differs from other indicators such as the SMA (Simple Moving Average), which only requires the specified number of values. This discrepancy in behavior may lead to confusion when using the RSI indicator and should be documented for clarity.

Steps to Reproduce:

  1. Import the TA-Lib library.
  2. Create a sample DataFrame with a single column.
  3. Calculate the RSI with a window size of 'timeperiod - 1' using the talib.RSI() function.
  4. Print the RSI values.

Code

import pandas as pd
import talib

# Create a sample DataFrame with 10 columns
data = {
    'col1': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
}

df = pd.DataFrame(data)

# Calculate the RSI with a window size of 'timeperiod - 1'
rsi = talib.RSI(df['col1'], timeperiod=9)

# Print the RSI values
print(rsi)

Actual Behaviour

If timeperiod values are in dataframe, it would return Nan, the above code that has timeperiod + 1 values would return the proper value.

Expected Behaviour

The RSI calculation should produce valid results using the specified timeperiod without requiring an additional value. This should be inline with other function such as SMA. Code has been provided for SMA below.

Code

import pandas as pd
import talib

# Create a sample DataFrame with 10 columns
data = {
    'col1': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
}

df = pd.DataFrame(data)

# Calculate the SMA with a window size of 9 (10 - 1)
sma = talib.SMA(df['col1'], timeperiod=9)

# Print the SMA values
print(sma)

Additional Information

mrjbq7 commented 1 year ago

I believe time period matches the definition of RSI in most formulas. I would expect n=1 to use two observations. On Jul 6, 2023, at 7:09 PM, TrainedPro @.***> wrote: Summary: The RSI (Relative Strength Index) indicator in the TA-Lib library requires one more value than the specified 'timeperiod'. This behavior differs from other indicators such as the SMA (Simple Moving Average), which only requires the specified number of values. This discrepancy in behavior may lead to confusion when using the RSI indicator and should be documented for clarity. Steps to Reproduce:

Import the TA-Lib library. Create a sample DataFrame with a single column. Calculate the RSI with a window size of 'timeperiod - 1' using the talib.RSI() function. Print the RSI values.

Code import pandas as pd import talib

Create a sample DataFrame with 10 columns

data = { 'col1': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], }

df = pd.DataFrame(data)

Calculate the RSI with a window size of 'timeperiod - 1'

rsi = talib.RSI(df['col1'], timeperiod=9)

Print the RSI values

print(rsi) Actual Behaviour If timeperiod values are in dataframe, it would return Nan, the above code that has timeperiod + 1 values would return the proper value. Expected Behaviour The RSI calculation should produce valid results using the specified timeperiod without requiring an additional value. This should be inline with other function such as SMA. Code has been provided for SMA below. Code import pandas as pd import talib

Create a sample DataFrame with 10 columns

data = { 'col1': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], }

df = pd.DataFrame(data)

Calculate the SMA with a window size of 9 (10 - 1)

sma = talib.SMA(df['col1'], timeperiod=9)

Print the SMA values

print(sma) Additional Information

TA-Lib Version: 0.4.26 Operating System: Ubuntu 22.04.2 LTS Python Version: Python 3.10.6

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you are subscribed to this thread.Message ID: @.***>