def BBANDS_STOP(df, n, nstd):
MA = pd.Series(pd.rolling_mean(df['close'], n))
MSD = pd.Series(pd.rolling_std(df['close'], n))
Upper = pd.Series(MA + MSD * nstd, name = 'BBSTOP_upper')
Lower = pd.Series(MA - MSD * nstd, name = 'BBSTOP_lower')
Trend = pd.Series(0, index = Lower.index, name = 'BBSTOP_trend')
for idx, dateidx in enumerate(Upper.index):
if idx >= n:
Trend[idx] = Trend[idx-1]
if (df.close[idx] > Upper[idx-1]):
Trend[idx] = 1
if (df.close[idx] < Lower[idx-1]):
Trend[idx] = -1
if (Trend[idx]==1) and (Lower[idx] < Lower[idx-1]):
Lower[idx] = Lower[idx-1]
elif (Trend[idx]==-1) and (Upper[idx] > Upper[idx-1]):
Upper[idx] = Upper[idx-1]
return pd.concat([Upper,Lower, Trend], join='outer', axis=1)
评论列表
文章目录