RSI计算使用Python ta库为什么会根据起始位置而变化?

huangapple go评论52阅读模式
英文:

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

问题

以下是要翻译的代码部分:

我有一个 `DataFrame`,我想要计算 `Close` 列的 RSI窗口为 `14`,代码如下

from ta.momentum import RSIIndicator
import pandas as pd

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

这段代码有效我得到以下的 RSI 结果

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

但如果我从另一个任意位置开始计算 RSI例如 `data.iloc[1:]`,我理解由于向后移动了一个位置13 个索引现在会变成 `NaN`,RSI 会从第 14 个开始但为什么这会改变数值呢

t = RSIIndicator(data.Close.iloc[1:], window).rsi()
print(t.head(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

不管从哪里开始RSI 不应该是相同的值吗唯一需要的是之前的 14 个值所以如果最早的 15 个值不在那里为什么 RSI 会改变呢

这很重要因为我想要**实时计算** RSI也就是说随着数据的输入我将传递前面的 14 个数据点给 RSI 函数并得到下一个值但似乎我总是需要从头开始传递整个数据集
英文:

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.

答案1

得分: 1

RSI的计算方法在第一次计算后会基本改变。一旦开始后,它会“平滑”数值。

首次计算的平均涨幅和平均跌幅非常简单,是14周期的平均值。随后的计算是基于先前的平均值和当前的涨幅和跌幅。

请阅读“计算RSI”部分。

这意味着您始终需要保留一些先前的数据,以便正确计算新的RSI。

英文:

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.

huangapple
  • 本文由 发表于 2023年2月14日 04:56:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441104.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定