<1>以N=20为例
<2>中轨线=N日的移动平均线
<3>上轨线=中轨线+两倍的标准差
<4>下轨线=中轨线-两倍的标准差
import pandas as pd #002594收盘价,截止2021-0816 date = [250.37, 254.96, 251.83, 248.7, 266.35, 264.97, 245.17, 236.28, 222.81, 223.86, 227.39, 241.11, 248.84, 258.55, 248.43, 238.59, 236.38, 257.07, 264.58, 291.03, 279.94, 307.94, 299.31, 303.48, 293.55, 292.0, 294.99, 308.98, 300.02, 285.4] close = pd.Series(date) #标准差 std = close.rolling(20,min_periods=1).std() df = pd.DataFrame() #中轨 df[‘mid_line‘] = ma20 = close.rolling(20,min_periods=1).mean() #上轨 df[‘up_line‘] = ma20 + 2*std #下轨 df[‘down_line‘] = ma20 - 2*std print(df)