Search code examples
pythontechnical-indicator

Why is RSI calculation with python ta library changes depending on starting position?


I have a DataFrame and I want to calculate the RSI on the Close column with a window of 14 like so:

from ta.momentum import RSIIndicator
import pandas as pd

data = pd.read_csv()
output = RSIIndicator(data.Close, 14).rsi()
print(output.head(20))

This works and I get the following RSI result:

0           NaN
1           NaN
2           NaN
3           NaN
4           NaN
5           NaN
6           NaN
7           NaN
8           NaN
9           NaN
10          NaN
11          NaN
12          NaN
13    30.565576
14    30.565576
15    30.565576
16    36.847817
17    53.471152
18    53.471152
19    59.140918

But If I start the RSI at another arbitrary position, example on data.iloc[1:], I understand that since I shifted a position by 1, the 13th index is now going to be NaN and RSI will start at the 14th. But why does this change the values?

t = RSIIndicator(data.Close.iloc[1:], window).rsi()
print(t(20))


1           NaN
2           NaN
3           NaN
4           NaN
5           NaN
6           NaN
7           NaN
8           NaN
9           NaN
10          NaN
11          NaN
12          NaN
13          NaN
14    31.481498
15    31.481498
16    37.849374
17    54.534367
18    54.534367
19    60.171078
20    44.372719

Shouldn't the RSI be the same value no matter where you start. The only thing that is needed is the previous 14 values right? so why does the RSI change if the oldest 15th value is not there?

This is important because I would like to calculate the RSI on the fly meaning as data comes in, I would pass the previous 14 data points to the RSI function and get the next value. But it seems like I always need to pass the whole dataset from beginning.


Solution

  • The RSI basically switches up the method of its calculation after the first RSI calculation. After once it starts "smoothing" the values.

    The first calculations for average gain and average loss are simple 14-period averages. The second and subsequent, calculations are based on the prior averages and the current gain loss.

    https://school.stockcharts.com/doku.php?id=technical_indicators:relative_strength_index_rsi

    Read the "Calculating the RSI" section.

    This means you always need to keep SOME of the previous data in order to calculate the new RSI correctly.