def real_signal_simple_bolling(df, now_pos, avg_price, para=[200, 2]): n = int(para[0]) m = para[1] df['median'] = df['close'].rolling(n).mean() median = df.iloc[-1]['median'] median2 = df.iloc[-2]['median'] # 计算标准差 df['std'] = df['close'].rolling(n).std(ddof=0) std = df.iloc[-1]['std'] std2 = df.iloc[-2]['std'] # 计算上轨、下轨道 upper = median + m * std lower = median - m * std upper2 = median2 + m * std2 lower2 = median2 - m * std2 # ===寻找交易信号 signal = None close = df.iloc[-1]['close'] close2 = df.iloc[-2]['close'] # 找出做多信号 if (close > upper) and (close2 <= upper2): signal = 1 # 找出做空信号 elif (close < lower) and (close2 >= lower2): signal = -1 # 找出做多平仓信号 elif (close < median) and (close2 >= median2): signal = 0 # 找出做空平仓信号 elif (close > median) and (close2 <= median2): signal = 0 return signal以上代码加入下列逻辑代码,通过判断当前收盘价是否超过布林带上轨、成交量是否超过阈值以及当前成交量是否高于成交量移动平均值来判断是否买入或卖出
时间: 2024-02-06 16:04:28 浏览: 66
好的,以下是加入判断逻辑的代码:
```python
# 计算成交量移动平均线
vol_n = 20
df['vol_ma'] = df['volume'].rolling(vol_n).mean()
# 设置成交量阈值和布林带阈值
vol_threshold = df['volume'].quantile(0.8)
bb_threshold = 2.0
# 判断是否买入或卖出
if signal is not None:
# 判断当前成交量是否超过阈值
if df.iloc[-1]['volume'] > vol_threshold:
if signal == 1:
# 判断当前成交量是否高于成交量移动平均值
if df.iloc[-1]['volume'] > df.iloc[-1]['vol_ma']:
now_pos += signal
elif signal == -1:
# 判断当前成交量是否高于成交量移动平均值
if df.iloc[-1]['volume'] > df.iloc[-1]['vol_ma']:
now_pos += signal
else:
# 判断当前收盘价是否超过布林带阈值
if abs(close - median) > bb_threshold * std:
now_pos += signal
```
这段代码中,我们首先计算了成交量的移动平均线,并设定了成交量阈值和布林带阈值。然后在判断是否进行买入或卖出时,加入了成交量的判断逻辑。如果当前成交量超过了阈值,并且高于成交量移动平均值,则执行买入或卖出操作;否则,判断当前收盘价是否超过布林带阈值,若超过则执行买入或卖出操作。
阅读全文