def BBANDS(df_price, periods=20, mul=2):
# Middle Band = 20-day simple moving average (SMA)
df_middle_band = pd.rolling_mean(df_price, window=periods)
#df_middle_band = pd.rolling(window=periods,center=False).mean()
# 20-day standard deviation of price
""" Pandas uses the unbiased estimator (N-1 in the denominator),
whereas Numpy by default does not.
To make them behave the same, pass ddof=1 to numpy.std()."""
df_std = pd.rolling_std(df_price, window=periods)
#df_std = pd.rolling(window=periods,center=False).std()
# Upper Band = 20-day SMA + (20-day standard deviation of price x 2)
df_upper_band = df_middle_band + (df_std * mul)
# Lower Band = 20-day SMA - (20-day standard deviation of price x 2)
df_lower_band = df_middle_band - (df_std * mul)
return (df_upper_band, df_middle_band, df_lower_band)
评论列表
文章目录