【问题标题】:Calculate Bollinger Band using Pandas使用 Pandas 计算布林带
【发布时间】:2022-11-02 08:12:24
【问题描述】:

给定一个系列,我想用 Pandas 计算它的布林带。存在一个先前的问题,answer 仅讨论计算的std 方面,而不是整个计算。

给定一个pandas.Series 类型的变量series 和dtype float64,要计算一个长度为20、标准差为2 的带,我尝试了:

2 * (series.rolling(20).mean() + series.rolling(20).std(ddof=0))

然而,上面的公式产生了一个非常不正确的结果。

【问题讨论】:

    标签: python pandas technical-indicator


    【解决方案1】:

    考虑这个函数:

    import pandas as pd
    
    
    def bollinger_band(series: pd.Series, length: int = 20, *, num_std: int = 2) -> pd.Series:
        # Ref: https://stackoverflow.com/a/74283044/
        rolling = series.rolling(length)
        if num_std == 0:
            bband = rolling.mean()  # Skips calculating std.
        else:
            bband = rolling.mean() + rolling.std(ddof=0) * num_std
        return bband
    

    如上所述,num_std 的乘数仅适用于滚动std

    要计算下限,请指定负数 num_std,例如-2。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-12
      • 1970-01-01
      • 1970-01-01
      • 2017-02-03
      相关资源
      最近更新 更多