简体   繁体   English

Pandas.Series.ewm 用于计算 RSI 指标,带有 Wilders 移动平均线

[英]Pandas.Series.ewm for calculating a RSI Indicator, with Wilders Moving Average

I asked a question earlier today.我今天早些时候问了一个问题。 I have found a partial Solution to it: pandas.Series.ewm我找到了部分解决方案:pandas.Series.ewm

Data: I have a Series, see example数据:我有一个系列,请参阅示例

1   54.8
2   56.8
3   57.85
4   59.85
5   60.57
6   61.1
7   62.17
8   60.6
9   62.35
10  62.15
11  62.35
12  61.45
13  62.8
14  61.37
15  62.5
16  62.57
17  60.8
18  59.37
19  60.35
20  62.35
21  62.17
22  62.55
23  64.55
24  64.37
25  65.3
26  64.42
27  62.9
28  61.6
29  62.05
30  60.05
31  59.7
32  60.9
33  60.25
34  58.27
35  58.7
36  57.72
37  58.1
38  58.2

date    close   change  gain    loss    avg_gain    avg_loss    rs  rsi
1   54.8                            
2   56.8    2   2   0               
3   57.85   1.05    1.05    0               
4   59.85   2   2   0               
5   60.57   0.72    0.72    0               
6   61.1    0.53    0.53    0               
7   62.17   1.07    1.07    0               
8   60.6    -1.57   0   1.57                
9   62.35   1.75    1.75    0               
10  62.15   -0.2    0   0.2             
11  62.35   0.2 0.2 0               
12  61.45   -0.9    0   0.9             
13  62.8    1.35    1.35    0               
14  61.37   -1.43   0   1.43                
15  62.5    1.13    1.13    0   0.842857143 0.292857143 2.87804878  74.21383648
16  62.57   0.07    0.07    0   0.787653061 0.271938776 2.896435272 74.33551618
17  60.8    -1.77   0   1.77    0.731392128 0.378943149 1.930084053 65.87128622
18  59.37   -1.43   0   1.43    0.679149833 0.454018638 1.495863333 59.93370364
19  60.35   0.98    0.98    0   0.700639131 0.421588735 1.661901925 62.43287589
20  62.35    2      2       0   0.793450622 0.391475254 2.026821908 66.96204698
21  62.17   -0.18   0   0.18    0.736775577 0.376369879 1.957583798 66.18861651
22  62.55   0.38    0.38    0   0.711291607 0.349486316 2.035248805 67.05377173
23  64.55     2     2       0   0.803342207 0.324523008 2.475455322 71.22679168
24  64.37   -0.18   0   0.18    0.745960621 0.314199936 2.374159048 70.36298569
25  65.3    0.93    0.93    0   0.759106291 0.291757083 2.601843568 72.23644
26  64.42   -0.88   0   0.88    0.704884413 0.333774435 2.111858608 67.86486387
27  62.9    -1.52   0   1.52    0.654535526 0.418504832 1.563985589 60.99822072
28  61.6    -1.3    0   1.3     0.607782989 0.481468773 1.262351835 55.79821031
29  62.05   0.45    0.45    0   0.596512775 0.447078146 1.334247224 57.15963631
30  60.05   -2  0   2           0.55390472  0.558001136 0.992658768 49.81579304
31  59.7    -0.35   0   0.35    0.514340097 0.543143912 0.946968356 48.63809691
32  60.9    1.2 1.2 0           0.563315804 0.504347918 1.116919064 52.76153835
33  60.25   -0.65   0   0.65    0.523078961 0.514751638 1.016177361 50.40118893
34  58.27   -1.98   0   1.98    0.485716178 0.619412235 0.784156577 43.9511076
35  58.7    0.43    0.43    0   0.481736451 0.575168504 0.837557077 45.5799217
36  57.72   -0.98   0   0.98    0.447326705 0.60408504  0.740502868 42.54534031
37  58.1    0.38    0.38    0   0.442517654 0.560936108 0.788891369 44.09945638
38  58.2    0.1 0.1 0           0.418052108 0.520869243 0.802604709 44.52472054

例子

Formulas in Excel Excel 中的公式

  1. change: previous_close - current_close更改:previous_close - current_close

  2. gain: =IF(change>0; change; 0)增益:=IF(变化>0;变化;0)

  3. loss: =IF(change<0; -change; 0)损失:=IF(变化<0;-变化;0)

  4. avg_gain (first row): =AVERAGE("over the first n gains") avg_gain (第一行): =AVERAGE("在前 n 个增益上")

5. avg_gain (every next row): =(prev. avg_gain * (n - 1) + current gain) / n 5. avg_gain (每下一行): =(prev. avg_gain * (n - 1) + current gain) / n

  1. avg_loss (first row): =AVERAGE("over the first n gains") avg_loss(第一行):=AVERAGE(“在前 n 个收益中”)

7. avg_loss (every next row): =(prev. avg_loss * (n - 1) + current loss) / n 7. avg_loss(每下一行):=(prev. avg_loss * (n - 1) + current loss) / n

  1. rs: avg_gain / avg_loss rs: avg_gain / avg_loss

  2. rsi: 100-(100/(1+rs)) rsi: 100-(100/(1+rs))

Formula in Python for change, gain and loss : (works perfectly fine) Python 中用于更改、增益和损失的公式:(效果很好)

delta = data.diff()
delta_up = delta.clip(lower=0)
delta_down = delta.clip(upper=0).abs()

Problem: I have to formulas but I cant combine them.问题:我必须使用公式,但我无法将它们组合起来。

Formula for the first average gain/loss : (on its own it works)第一个平均收益/损失的公式:(它本身有效)

The first Average needs to be an Simple Moving Average第一个平均线需要是一个简单的移动平均线

first_avg_gain = delta_up.rolling(
    window=periods, min_periods=periods
).mean()[: periods + 1]

first_avg_loss = delta_down.rolling(
    window=periods, min_periods=periods
).mean()[: periods + 1]

Formula for the following average gains/losses :以下平均收益/损失的公式:

This is calculated like that in Excel: (prev. avg_gain * (period_length - 1) + gain)/period_length这与 Excel 中的计算类似:(prev. avg_gain * (period_length - 1) + gain)/period_length

The Formula following should do the same下面的公式应该做同样的事情

avg_gain = delta_up.ewm(
    com=periods - 1, adjust=False, min_periods=periods
).mean()

avg_loss = delta_down.ewm(
    com=periods - 1, adjust=False, min_periods=periods
).mean()

Formula in Python for rs and rsi : rs 和 rsi的 Python 中的公式:

rs = avg_gain / avg_loss
indicator = 100 - 100 / (1 + rs)

Question: How can I combine first_avg_gain and avg_gain / first_avg_loss and avg_loss?问题:如何结合 first_avg_gain 和 avg_gain / first_avg_loss 和 avg_loss?

I need to implement this code, but it doesnt work我需要实现这段代码,但它不起作用

for i, row in enumerate(df['avg_gain'].iloc[window_length+1:]):
    df['avg_gain'].iloc[i + window_length + 1] =\
        (df['avg_gain'].iloc[i + window_length] *
         (window_length - 1) +
         df['gain'].iloc[i + window_length + 1])\
        / window_length

for i, row in enumerate(df['avg_loss'].iloc[window_length+1:]):
    df['avg_loss'].iloc[i + window_length + 1] =\
        (df['avg_loss'].iloc[i + window_length] *
         (window_length - 1) +
         df['loss'].iloc[i + window_length + 1])\
        / window_length

I cannot reproduce your example with periods = 10 .我无法使用periods = 10重现您的示例。 Feels like there's something fishy about your SMA.感觉你的 SMA 有问题。 With periods = 14 : periods = 14

df = (df
    .assign(
        change = df.diff(),
        gain = lambda df_: df_.change.clip(0),
        loss = lambda df_: df_.change.clip(upper=0).abs(),
        avg_gain = lambda df_: df_.gain.rolling(window=14, min_periods=14).mean(),
        avg_loss = lambda df_: df_.loss.rolling(window=14, min_periods=14).mean(),
        rs = lambda df_: df_.avg_gain.div(df_.avg_loss),
        rsi = lambda df_: 100-100/(1+df_.rs)
    )
)

    close   change  gain    loss    avg_gain    avg_loss    rs  rsi
--- --- --- --- --- --- --- --- ---
--- --- --- --- --- --- --- --- ---
14  62.50   1.13    1.13    0.00    0.843   0.293   2.878   74.214
15  62.57   0.07    0.07    0.00    0.705   0.293   2.407   70.651
16  60.80   -1.77   0.00    1.77    0.630   0.419   1.503   60.041
17  59.37   -1.43   0.00    1.43    0.487   0.521   0.934   48.300
18  60.35   0.98    0.98    0.00    0.506   0.521   0.970   49.235
19  62.35   2.00    2.00    0.00    0.611   0.521   1.171   53.943
--- --- --- --- --- --- --- --- ---
--- --- --- --- --- --- --- --- ---

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 计算 pandas 中的移动平均值 - calculating moving average in pandas Pandas 指数移动平均线 (ewm) 权重在整个 DataSeries 中“持续”吗? - Pandas Exponential Moving Average (ewm) weight “persists” through the entire DataSeries? 指数移动平均线 df.ewm() function - Exponential Moving Average df.ewm() function "NumPy 版本的“指数加权移动平均”,相当于 pandas.ewm().mean()" - NumPy version of "Exponential weighted moving average", equivalent to pandas.ewm().mean() 计算简单移动平均线 pandas for loop - Calculating simple moving average pandas for loop 从 pandas dataframe 计算 RSI - Calculating RSI from a pandas dataframe Pandas Groupby计算ewm无法正常工作 - Pandas Groupby with calculating ewm not working as expected 从 pandas DataFrame 计算 RSI 指标? - Calculate RSI indicator from pandas DataFrame? Tradingview pinescript 在 python、pandas 中的 RMA(RSI 中使用的移动平均线。它是指数加权移动平均线,alpha = 1 / 长度) - Tradingview pinescript's RMA (Moving average used in RSI. It is the exponentially weighted moving average with alpha = 1 / length) in python, pandas Pandas:及时计算事件周围时间序列数据的平均行为 - Pandas: calculating average behaviour of time series data around an event in time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM