def ASCTREND(df, n, risk = 3, stop_ratio = 0.5, atr_mode = 0):
wpr = WPR(df, n)
uplevel = 67 + risk
dnlevel = 33 - risk
signal = pd.Series(0, index = df.index, name = "ASCSIG_%s" % str(n))
trend = pd.Series(index = df.index, name = "ASCTRD_%s" % str(n))
stop = pd.Series(index = df.index, name = "ASCSTOP_%s" % str(n))
ind = (wpr >= uplevel) & (wpr.shift(1) < uplevel)
signal[ind] = 1
trend[ind] = 1
ind = (wpr <= dnlevel) & (wpr.shift(1) > dnlevel)
signal[ind] = -1
trend[ind] = -1
trend = trend.fillna(method='ffill')
if atr_mode == 0:
atr = ATR(df, n + 1)
else:
atr = pd.rolling_mean(df['high'] - df['low'], n + 1)
stop[trend > 0] = df['low'] - stop_ratio * atr
stop[trend < 0] = df['high'] + stop_ratio * atr
return pd.concat([signal, trend, stop], join='outer', axis=1)
评论列表
文章目录