TAVolatility
波动率指标综合策略示例
本文档提供波动率指标(ATR、NATR、TRANGE、BBANDS)的实战策略示例。
策略 1:ATR 动态止损策略
策略逻辑
使用 ATR 设置动态止损和跟踪止损,根据市场波动性调整风险控制。
python
def main():
position = None
entry_price = 0
stop_loss = 0
take_profit = 0
atr_stop_multiplier = 2 # 止损倍数
atr_profit_multiplier = 3 # 止盈倍数
while True:
records = exchange.GetRecords()
if len(records) < 50:
Sleep(1000)
continue
highs = [r['High'] for r in records]
lows = [r['Low'] for r in records]
closes = [r['Close'] for r in records]
current_price = closes[-1]
# 计算 ATR
atr = TA.ATR(highs, lows, closes, 14)
current_atr = atr[-1]
# 入场逻辑(示例:突破 20 日高点)
if position is None:
resistance = max(highs[-20:])
if current_price > resistance:
# 开多仓
position = "long"
entry_price = current_price
stop_loss = entry_price - atr_stop_multiplier * current_atr
take_profit = entry_price + atr_profit_multiplier * current_atr
Log("✅ 开多仓")
Log(f"入场价: {entry_price}")
Log(f"止损: {stop_loss} (-{atr_stop_multiplier} ATR)")
Log(f"止盈: {take_profit} (+{atr_profit_multiplier} ATR)")
# 持仓管理
elif position == "long":
# 止损检查
if current_price < stop_loss:
Log(f"❌ 触发止损: {current_price}")
# exchange.Sell(...)
position = None
# 止盈检查
elif current_price > take_profit:
Log(f"✅ 触发止盈: {current_price}")
# exchange.Sell(...)
position = None
# 跟踪止损
else:
new_stop = current_price - atr_stop_multiplier * current_atr
if new_stop > stop_loss:
stop_loss = new_stop
Log(f"📈 移动止损至: {stop_loss}")
Sleep(5000)策略优势
- 根据波动性动态调整止损
- 避免市场噪音导致过早止损
- 跟踪止损锁定利润
策略 2:布林带挤压突破策略
策略逻辑
利用布林带收窄(挤压)识别低波动期,等待突破方向后入场。
python
def main():
squeeze_threshold = 0.05 # 挤压阈值
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_width = (upper[-1] - lower[-1]) / middle[-1]
# 计算历史平均宽度
bb_widths_hist = []
for i in range(-50, 0):
width = (upper[i] - lower[i]) / middle[i]
bb_widths_hist.append(width)
avg_width = sum(bb_widths_hist) / len(bb_widths_hist)
# 检测挤压
if bb_width < avg_width * 0.5:
if not squeeze_detected:
Log("⚠️ 布林带挤压,波动性极低")
Log(f"当前宽度: {bb_width:.4f}, 平均宽度: {avg_width:.4f}")
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)策略优势
- 捕捉大波动前的平静期
- 突破方向确认后入场
- 高胜率策略
策略 3:布林带 + RSI 双重确认策略
策略逻辑
结合布林带和 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 < lower[-1] and rsi[-1] < 30:
Log("✅ 双重确认超卖:布林带下轨 + RSI < 30")
Log("强烈买入信号")
# exchange.Buy(...)
# 超买 + 布林带上轨(卖出信号)
elif current_price > upper[-1] and rsi[-1] > 70:
Log("❌ 双重确认超买:布林带上轨 + RSI > 70")
Log("强烈卖出信号")
# exchange.Sell(...)
# 价格回归中轨(平仓信号)
elif abs(current_price - middle[-1]) / middle[-1] < 0.01:
Log("💰 价格回归中轨,考虑平仓")
Sleep(60000)策略优势
- 双重确认减少假信号
- 布林带提供价格位置
- RSI 提供超买超卖
策略 4:ATR + 布林带波动性组合策略
策略逻辑
结合 ATR 和布林带,全面分析市场波动性。
python
def main():
while True:
records = exchange.GetRecords()
if len(records) < 100:
Sleep(1000)
continue
highs = [r['High'] for r in records]
lows = [r['Low'] for r in records]
closes = [r['Close'] for r in records]
current_price = closes[-1]
# ATR
atr = TA.ATR(highs, lows, closes, 14)
atr_percent = (atr[-1] / current_price) * 100
# 布林带
upper, middle, lower = TA.BBANDS(closes, 20, 2, 2)
bb_width = (upper[-1] - lower[-1]) / middle[-1] * 100
Log(f"ATR 百分比: {atr_percent:.2f}%")
Log(f"布林带宽度: {bb_width:.2f}%")
# 判断市场状态
if atr_percent < 2 and bb_width < 5:
Log("💤 低波动环境(ATR + 布林带双重确认)")
Log("策略:等待突破,减少交易频率")
elif atr_percent > 5 and bb_width > 15:
Log("⚠️ 高波动环境(ATR + 布林带双重确认)")
Log("策略:缩小仓位,扩大止损")
else:
Log("📊 正常波动环境")
Log("策略:标准仓位和止损")
# 突破确认
if current_price > upper[-1] and atr[-1] > sum(atr[-20:])/20:
Log("✅ 向上突破 + ATR 扩张,强势信号")
Sleep(60000)策略优势
- 多维度波动性分析
- 动态调整交易策略
- 根据市场状态优化参数
策略 5:NATR 资产筛选 + ATR 止损策略
策略逻辑
使用 NATR 筛选合适波动性的资产,然后用 ATR 设置止损。
python
def select_and_trade():
"""资产筛选和交易"""
symbols = ["BTC_USDT", "ETH_USDT", "LTC_USDT", "XRP_USDT"]
selected = None
# 第一步:资产筛选
for symbol in symbols:
# exchange.SetSymbol(symbol)
records = exchange.GetRecords()
if len(records) < 50:
continue
highs = [r['High'] for r in records]
lows = [r['Low'] for r in records]
closes = [r['Close'] for r in records]
# 计算 NATR
natr = TA.NATR(highs, lows, closes, 14)
# 选择波动性适中的资产(2-4%)
if 2 < natr[-1] < 4:
Log(f"✅ 选中资产: {symbol}, NATR: {natr[-1]:.2f}%")
selected = symbol
break
if not selected:
Log("未找到合适的交易标的")
return
# 第二步:交易策略
while True:
records = exchange.GetRecords()
if len(records) < 50:
Sleep(1000)
continue
highs = [r['High'] for r in records]
lows = [r['Low'] for r in records]
closes = [r['Close'] for r in records]
current_price = closes[-1]
# 使用 ATR 设置止损
atr = TA.ATR(highs, lows, closes, 14)
stop_distance = 2 * atr[-1]
# 交易逻辑(示例)
# if buy_condition:
# entry = current_price
# stop = entry - stop_distance
# Log(f"开仓: {entry}, 止损: {stop}")
Sleep(60000)策略优势
- 选择合适波动性的资产
- 避免过高或过低波动
- 动态止损管理
策略 6:布林带均值回归策略
策略逻辑
在震荡市场中,利用价格向中轨回归的特性进行交易。
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)
# 计算价格在布林带中的位置 (%B)
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"价格位置 %B: {bb_percent:.2%}")
# 均值回归策略
if bb_percent > 0.9:
Log("⚠️ 价格接近上轨(%B > 0.9),做空")
Log("止损: 上轨之上")
Log("止盈: 中轨")
# exchange.Sell(...)
elif bb_percent < 0.1:
Log("💡 价格接近下轨(%B < 0.1),做多")
Log("止损: 下轨之下")
Log("止盈: 中轨")
# exchange.Buy(...)
elif 0.45 < bb_percent < 0.55:
Log("📊 价格在中轨附近,考虑平仓")
Sleep(60000)策略优势
- 适合震荡市场
- 风险收益比明确
- %B 指标量化价格位置
策略参数建议
ATR 止损参数
| 风格 | ATR 倍数 | 适用场景 |
|---|---|---|
| 激进 | 1-1.5 | 短线交易、日内 |
| 平衡 | 2-2.5 | 波段交易 |
| 保守 | 3-4 | 长线持仓 |
布林带参数
| 参数 | 推荐值 | 适用场景 |
|---|---|---|
| (20, 2, 2) | 标准 | 平衡策略 |
| (20, 1.5, 1.5) | 较窄 | 频繁信号 |
| (20, 2.5, 2.5) | 较宽 | 减少假信号 |
NATR 筛选标准
| NATR 范围 | 策略选择 |
|---|---|
| < 1% | 避免交易 |
| 1-2% | 长线策略 |
| 2-4% | 波段策略 |
| 4-6% | 短线策略 |
| > 6% | 降低仓位 |
风险控制要点
止损设置
- ATR 止损:2-3 倍 ATR
- 布林带止损:上轨/下轨之外
- 百分比止损:2-5%
仓位管理
- 高波动(NATR > 5%):50% 仓位
- 正常波动(NATR 2-5%):100% 仓位
- 低波动(NATR < 2%):观望或小仓位
市场环境
- 趋势市场:使用突破策略
- 震荡市场:使用均值回归策略
- 低波动期:等待挤压突破
实战技巧总结
买入时机
- ✅ 布林带下轨 + RSI 超卖
- ✅ 挤压后向上突破
- ✅ %B < 0.1 + 均值回归
- ✅ ATR 扩张 + 突破阻力
卖出时机
- ❌ 布林带上轨 + RSI 超买
- ❌ 挤压后向下突破
- ❌ %B > 0.9 + 均值回归
- ❌ 触及 ATR 止损
观望时机
- ⚠️ 极低波动(NATR < 1%)
- ⚠️ 极高波动(NATR > 6%)
- ⚠️ 布林带挤压中(未突破)
- ⚠️ 信号矛盾