talib 不是万能的、有些其它指标 如:BBI、PSY、BIAS等这些指标是没有的
def BBI(df, n1=3, n2=6, n3=12, n4=24):"""
多空指数
"""
bbi = (MA(df["close"], n1) + MA(df["close"], n2) + MA(df["close"], n3) + MA(df["close"], n4)) / 4
new_df = pd.DataFrame(data=list(bbi), columns=["bbi"])
return new_df def PSY(df, n=12, m=6):
"""
心理线
"""
new_df = pd.DataFrame()
new_df["psy"] = COUNT(df["close"] > df["close"].shift(1), n) / n * 100
new_df["psyma"] = MA(new_df["psy"], m)
return new_df def BIAS(df, n=6):
"""
乖离率
"""
ma1 = MA(df["close"], n)
new_df = pd.DataFrame(data=list((df["close"] - ma1) / ma1 * 100), columns=["bias"])
return new_df def MA(close, n=5):
"""
简单移动平均线
"""
ma_data = close.rolling(n).mean()
return ma_data def COUNT(cond, n):
"""
统计n周期中满足cond条件的个数
"""
if n == 0: # 从第一个有效值开始统计
count_data = pd.Series(np.where(cond, 1, 0).cumsum())
else: # 统计每个n周期
count_data = pd.Series(pd.Series(np.where(cond, 1, 0)).rolling(n).sum())
return count_data 调用方法: bbi = BBI(df, 6, 12, 24, 48)
if df['close'].values[-2] < bbi.values[-2] and df['close'].values[-1] >= bbi.values[-1]:
order.up_cross(symbol, 'bbi 策略做多')
if df['close'].values[-2] > bbi.values[-2] and df['close'].values[-1] <= bbi.values[-1]:
order.down_cross(symbol, 'bbi 策略做空')
psy = PSY(df, 10, 10)
if (psy['psy'].iloc[-2] < 30) and (psy['psy'].iloc[-1] >= 30):
order.up_cross(symbol, 'psy 策略做多')
if (psy['psy'].iloc[-2] > 70) and (psy['psy'].iloc[-1] <= 70):
order.down_cross(symbol, 'psy 策略做空')
bias = BIAS(df, 12)
if (bias['bias'].iloc[-2] > -10) and (bias['bias'].iloc[-1] <= -10):
order.up_cross(symbol, 'bias 策略做多')
if (bias['bias'].iloc[-2] < 10) and (bias['bias'].iloc[-1] >= 10):
order.down_cross(symbol, 'bias 策略做空') 写好策略应用后 标签:二十二,bbi,df,bias,new,psy,close,量化,区块 From: https://www.cnblogs.com/nbjjy/p/17495516.html