Pharos
TAVolatility

TA.BBANDS() - 布林带

布林带 (Bollinger Bands)

基于标准差的波动率通道,包含上轨、中轨、下轨,用于判断超买超卖和波动性变化。

返回波动率指标目录


语法

python
TA.BBANDS(data, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0)

参数

参数名类型必选默认值说明
dataany-价格数据数组(通常使用收盘价)
timeperiodany-时间周期,默认 5(标准设置为 20)
nbdevupany-上轨标准差倍数,默认 2
nbdevdnany-下轨标准差倍数,默认 2
matypeany-移动平均类型,默认 0 (SMA)

返回值

返回 (upper, middle, lower) 元组:

  • upper: 上轨数组
  • middle: 中轨数组(移动平均线)
  • lower: 下轨数组

计算方法

plaintext
中轨 (Middle) = SMA(Close, timeperiod)
标准差 (Std) = StandardDeviation(Close, timeperiod)

上轨 (Upper) = Middle + (nbdevup × Std)
下轨 (Lower) = Middle - (nbdevdn × Std)

布林带宽度 = (Upper - Lower) / Middle

信号解读

价格位置信号

  1. 价格触及上轨:可能超买,注意回调
  2. 价格触及下轨:可能超卖,注意反弹
  3. 价格突破上轨:强势信号,可能继续上涨
  4. 价格跌破下轨:弱势信号,可能继续下跌

布林带形态

  • 布林带收窄:波动性降低,可能即将突破
  • 布林带扩张:波动性增加,趋势可能持续
  • 布林带挤压:极低波动,酝酿大行情
  • 布林带喇叭口:剧烈波动,趋势加速

基础示例

基本布林带策略

python
def main():
    while True:
        records = exchange.GetRecords()
        if len(records) < 30:
            Sleep(1000)
            continue
        
        closes = [r['Close'] for r in records]
        current_price = closes[-1]
        
        # 计算布林带
        upper, middle, lower = TA.BBANDS(closes, 20, 2, 2)
        
        Log("布林带上轨:", upper[-1])
        Log("布林带中轨:", middle[-1])
        Log("布林带下轨:", lower[-1])
        Log("当前价格:", current_price)
        
        # 计算布林带宽度(波动性指标)
        bb_width = (upper[-1] - lower[-1]) / middle[-1]
        Log("布林带宽度:", bb_width)
        
        # 价格位置判断
        if current_price > upper[-1]:
            Log("⚠️ 价格在上轨之上,强势区域/超买")
        elif current_price < lower[-1]:
            Log("💡 价格在下轨之下,弱势区域/超卖")
        elif current_price > middle[-1]:
            Log("📈 价格在中轨之上")
        else:
            Log("📉 价格在中轨之下")
        
        Sleep(60000)

高级应用

布林带突破策略

python
def main():
    while True:
        records = exchange.GetRecords()
        if len(records) < 30:
            Sleep(1000)
            continue
        
        closes = [r['Close'] for r in records]
        current_price = closes[-1]
        prev_price = closes[-2]
        
        upper, middle, lower = TA.BBANDS(closes, 20, 2, 2)
        
        # 突破上轨(买入信号)
        if prev_price <= upper[-2] and current_price > upper[-1]:
            Log("✅ 突破布林带上轨,强势买入信号")
            # exchange.Buy(...)
        
        # 跌破下轨(卖出信号)
        elif prev_price >= lower[-2] and current_price < lower[-1]:
            Log("❌ 跌破布林带下轨,弱势卖出信号")
            # exchange.Sell(...)
        
        # 回归中轨(平仓信号)
        elif current_price > middle[-1] and prev_price < middle[-1]:
            Log("💰 价格向上穿越中轨,考虑平空仓或开多仓")
        
        elif current_price < middle[-1] and prev_price > middle[-1]:
            Log("💰 价格向下穿越中轨,考虑平多仓或开空仓")
        
        Sleep(60000)

布林带挤压突破策略

python
def main():
    squeeze_detected = False
    
    while True:
        records = exchange.GetRecords()
        if len(records) < 50:
            Sleep(1000)
            continue
        
        closes = [r['Close'] for r in records]
        current_price = closes[-1]
        
        upper, middle, lower = TA.BBANDS(closes, 20, 2, 2)
        
        # 计算布林带宽度历史
        bb_widths = []
        for i in range(-20, 0):
            width = (upper[i] - lower[i]) / middle[i]
            bb_widths.append(width)
        
        current_width = bb_widths[-1]
        avg_width = sum(bb_widths) / len(bb_widths)
        min_width = min(bb_widths)
        
        # 检测挤压(宽度小于平均值的 50%)
        if current_width < avg_width * 0.5 or current_width == min_width:
            if not squeeze_detected:
                Log("⚠️ 布林带挤压检测到,准备突破")
                squeeze_detected = True
        else:
            squeeze_detected = False
        
        # 在挤压后等待突破方向
        if squeeze_detected:
            if current_price > upper[-1]:
                Log("✅ 向上突破布林带!买入信号")
                # exchange.Buy(...)
                squeeze_detected = False
                
            elif current_price < lower[-1]:
                Log("❌ 向下突破布林带!卖出信号")
                # exchange.Sell(...)
                squeeze_detected = False
        
        Sleep(60000)

布林带均值回归策略

python
def main():
    while True:
        records = exchange.GetRecords()
        if len(records) < 50:
            Sleep(1000)
            continue
        
        closes = [r['Close'] for r in records]
        current_price = closes[-1]
        
        upper, middle, lower = TA.BBANDS(closes, 20, 2, 2)
        
        # 计算价格在布林带中的位置(0-1)
        # %B = (Price - Lower) / (Upper - Lower)
        bb_range = upper[-1] - lower[-1]
        if bb_range != 0:
            bb_percent = (current_price - lower[-1]) / bb_range
        else:
            bb_percent = 0.5
        
        Log(f"价格在布林带位置: {bb_percent:.2%}")
        
        # 均值回归策略
        if bb_percent > 0.9:
            Log("⚠️ 价格接近上轨,预期回归中轨,做空机会")
            # exchange.Sell(...)
            
        elif bb_percent < 0.1:
            Log("💡 价格接近下轨,预期回归中轨,做多机会")
            # exchange.Buy(...)
            
        elif 0.45 < bb_percent < 0.55:
            Log("📊 价格在中轨附近,观望")
        
        Sleep(60000)

布林带 + RSI 组合策略

python
def main():
    while True:
        records = exchange.GetRecords()
        if len(records) < 50:
            Sleep(1000)
            continue
        
        closes = [r['Close'] for r in records]
        current_price = closes[-1]
        
        # 布林带
        upper, middle, lower = TA.BBANDS(closes, 20, 2, 2)
        
        # RSI
        rsi = TA.RSI(closes, 14)
        
        # 双重确认超买
        if current_price > upper[-1] and rsi[-1] > 70:
            Log("❌ 布林带上轨 + RSI 超买,强烈卖出信号")
            # exchange.Sell(...)
        
        # 双重确认超卖
        elif current_price < lower[-1] and rsi[-1] < 30:
            Log("✅ 布林带下轨 + RSI 超卖,强烈买入信号")
            # exchange.Buy(...)
        
        # 背离(价格新低但 RSI 未新低)
        elif current_price < lower[-1] and rsi[-1] > 40:
            Log("💡 价格触及下轨但 RSI 未超卖,可能假跌破")
        
        Sleep(60000)

参数优化建议

常用参数组合

参数推荐值适用场景
(20, 2, 2)标准设置中期趋势、平衡策略
(20, 1.5, 1.5)较窄布林带更频繁信号、短线交易
(20, 2.5, 2.5)较宽布林带减少假信号、长线持仓
(10, 2, 2)短周期日内交易、快速反应
(50, 2, 2)长周期长线投资、趋势跟踪

标准差倍数影响

  • 1 倍标准差:68% 的价格在通道内(信号频繁)
  • 2 倍标准差:95% 的价格在通道内(标准设置)
  • 3 倍标准差:99.7% 的价格在通道内(极少信号)

与其他指标配合

布林带 + ATR

python
upper, middle, lower = TA.BBANDS(closes, 20, 2, 2)
atr = TA.ATR(highs, lows, closes, 14)

bb_width = (upper[-1] - lower[-1]) / middle[-1]
atr_percent = atr[-1] / closes[-1]

# 双重确认低波动
if bb_width < 0.05 and atr_percent < 0.02:
    Log("布林带 + ATR 双重确认低波动")

布林带 + MACD

python
upper, middle, lower = TA.BBANDS(closes, 20, 2, 2)
macd = TA.MACD(closes, 12, 26, 9)

# MACD 金叉 + 价格在下轨
if macd[0][-1] > macd[1][-1] and closes[-1] < lower[-1]:
    Log("MACD 金叉 + 超卖,强烈买入信号")

布林带 + 成交量

python
upper, middle, lower = TA.BBANDS(closes, 20, 2, 2)
volumes = [r['Volume'] for r in records]
vol_avg = sum(volumes[-20:]) / 20

# 突破上轨 + 放量
if closes[-1] > upper[-1] and volumes[-1] > vol_avg * 1.5:
    Log("突破上轨 + 放量确认")

注意事项

  1. 不是买卖信号:触及上下轨不一定是买卖点
  2. 趋势市场:在强趋势中,价格可能长期沿上轨或下轨运行
  3. 收窄预警:布林带收窄通常预示大行情
  4. 假突破:突破后快速回归可能是假突破
  5. 参数调整:根据市场波动性调整标准差倍数
  6. 时间周期:不同时间周期布林带差异很大
  7. 多重确认:结合其他指标(RSI、MACD)确认信号

布林带宽度指标

BBW (Bollinger Bandwidth)

python
upper, middle, lower = TA.BBANDS(closes, 20, 2, 2)
bbw = (upper[-1] - lower[-1]) / middle[-1]

if bbw < 0.05:
    Log("极低波动,挤压状态")
elif bbw > 0.15:
    Log("高波动,扩张状态")

%B 指标

python
# %B = (Price - Lower) / (Upper - Lower)
bb_percent = (closes[-1] - lower[-1]) / (upper[-1] - lower[-1])

# %B > 1: 价格在上轨之上
# %B < 0: 价格在下轨之下
# %B = 0.5: 价格在中轨

相关指标

实战要点

买入信号

✅ 价格触及下轨 + RSI 超卖 ✅ 布林带挤压后向上突破 ✅ 价格回归中轨(从下方) ✅ 下轨支撑确认

卖出信号

❌ 价格触及上轨 + RSI 超买 ❌ 布林带挤压后向下突破 ❌ 价格跌破中轨(从上方) ❌ 上轨阻力确认

风险控制

  • 布林带不是止损位,需单独设置止损
  • 强趋势中价格可能长期在上轨/下轨运行
  • 挤压后突破方向不确定,需等待确认
  • 假突破风险:结合成交量和其他指标

最佳实践

  1. 标准设置:(20, 2, 2) 适合大多数情况
  2. 挤压交易:低波动后等待突破
  3. 均值回归:在震荡市场效果好
  4. 趋势跟随:突破上轨做多,跌破下轨做空
  5. 多重确认:结合 RSI、MACD、成交量

返回波动率指标目录 | 返回 TA 指标总目录