Pharos
TAVolatility

TA.ATR() - 平均真实波幅

平均真实波幅 (Average True Range)

衡量市场波动性的经典指标,常用于止损设置和仓位管理。

返回波动率指标目录


语法

python
TA.ATR(high, low, close, timeperiod=14)

参数

参数名类型必选默认值说明
highany-最高价数组
lowany-最低价数组
closeany-收盘价数组
timeperiodany-时间周期,默认 14

返回值

返回 ATR 值数组,单位与价格相同。

计算方法

ATR 是真实波幅(True Range)的移动平均值:

plaintext
True Range = MAX(
    High - Low,
    |High - PrevClose|,
    |Low - PrevClose|
)

ATR = MovingAverage(True Range, timeperiod)

ATR 考虑了跳空缺口,比简单的 High-Low 更准确地衡量波动性。

信号解读

波动性判断

  • ATR 上升:市场波动性增加,趋势可能加强或反转
  • ATR 下降:市场波动性减少,可能处于整理阶段
  • ATR 极低:市场平静,可能酝酿大行情
  • ATR 极高:市场剧烈波动,风险增大

应用场景

  1. 止损设置:使用 ATR 的倍数设置动态止损
  2. 仓位管理:根据波动性调整仓位大小
  3. 突破过滤:ATR 扩大时的突破更可靠
  4. 市场状态:判断市场是活跃还是沉寂

基础示例

ATR 计算与显示

python
def main():
    while True:
        records = exchange.GetRecords()
        if len(records) < 30:
            Sleep(1000)
            continue
        
        highs = [r['High'] for r in records]
        lows = [r['Low'] for r in records]
        closes = [r['Close'] for r in records]
        
        atr = TA.ATR(highs, lows, closes, 14)
        current_atr = atr[-1]
        current_price = closes[-1]
        
        # ATR 占价格的百分比
        atr_percent = (current_atr / current_price) * 100
        
        Log("当前价格:", current_price)
        Log("ATR:", current_atr)
        Log("ATR 百分比:", atr_percent, "%")
        
        # 判断波动性
        if atr_percent > 5:
            Log("高波动性市场")
        elif atr_percent < 2:
            Log("低波动性市场")
        else:
            Log("正常波动性市场")
        
        Sleep(60000)

高级应用

ATR 动态止损策略

python
def main():
    position = None  # 持仓状态
    entry_price = 0
    stop_loss = 0
    atr_multiplier = 2  # ATR 倍数
    
    while True:
        records = exchange.GetRecords()
        if len(records) < 30:
            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 = TA.ATR(highs, lows, closes, 14)
        current_atr = atr[-1]
        
        # 开仓逻辑(示例)
        if position is None:
            # 假设某个买入条件满足
            # if your_buy_condition:
            position = "long"
            entry_price = current_price
            stop_loss = entry_price - atr_multiplier * current_atr
            Log("✅ 开多单")
            Log("入场价:", entry_price)
            Log("止损:", stop_loss)
            Log("止损距离:", atr_multiplier * current_atr)
        
        # 止损检查
        elif position == "long":
            if current_price < stop_loss:
                Log("❌ 触发止损,价格:", current_price)
                # exchange.Sell(...)
                position = None
            else:
                # 移动止损(跟踪止损)
                new_stop = current_price - atr_multiplier * current_atr
                if new_stop > stop_loss:
                    stop_loss = new_stop
                    Log("📈 移动止损至:", stop_loss)
        
        Sleep(5000)

ATR 波动性突破策略

python
def main():
    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 = TA.ATR(highs, lows, closes, 14)
        current_atr = atr[-1]
        
        # 计算 ATR 均值
        atr_avg = sum(atr[-20:]) / 20
        
        # ATR 扩张(波动性增加)
        if current_atr > atr_avg * 1.5:
            Log("⚠️ ATR 扩张,波动性增加")
            
            # 检查价格突破
            resistance = max(highs[-20:])
            support = min(lows[-20:])
            
            if current_price > resistance:
                Log("✅ 高波动性 + 向上突破,强势信号")
                # exchange.Buy(...)
            elif current_price < support:
                Log("❌ 高波动性 + 向下突破,弱势信号")
                # exchange.Sell(...)
        
        # ATR 收缩(波动性减少)
        elif current_atr < atr_avg * 0.6:
            Log("💤 ATR 收缩,市场平静,可能酝酿大行情")
        
        Sleep(60000)

ATR 仓位管理

python
def main():
    total_capital = 10000  # 总资金
    risk_percent = 0.02  # 每次交易风险 2%
    
    while True:
        records = exchange.GetRecords()
        if len(records) < 30:
            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 = TA.ATR(highs, lows, closes, 14)
        current_atr = atr[-1]
        
        # 使用 ATR 计算仓位
        # 风险金额 / 止损距离 = 仓位
        risk_amount = total_capital * risk_percent
        stop_distance = 2 * current_atr  # 2 倍 ATR 止损
        
        position_size = risk_amount / stop_distance
        
        Log("当前价格:", current_price)
        Log("ATR:", current_atr)
        Log("止损距离:", stop_distance)
        Log("建议仓位:", position_size)
        
        # 波动性越大,仓位越小
        if current_atr > current_price * 0.05:
            Log("⚠️ 高波动性,建议减小仓位")
        
        Sleep(60000)

参数优化建议

周期推荐值适用场景
短期7-10日内交易、快速止损
中期14标准设置、波段交易
长期20-30长线持仓、趋势跟踪
止损倍数推荐值风险等级
激进1-1.5 倍 ATR高风险高收益
平衡2-2.5 倍 ATR中等风险
保守3-4 倍 ATR低风险

与其他指标配合

ATR + 移动平均线

python
atr = TA.ATR(highs, lows, closes, 14)
sma20 = TA.SMA(closes, 20)

# 趋势 + 波动性
if closes[-1] > sma20[-1] and atr[-1] > atr[-2]:
    Log("上升趋势 + 波动性增加")

ATR + 布林带

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

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 + RSI 超买超卖

python
atr = TA.ATR(highs, lows, closes, 14)
rsi = TA.RSI(closes, 14)

# 超卖 + 高波动
if rsi[-1] < 30 and atr[-1] > sum(atr[-20:]) / 20:
    Log("超卖反弹机会,但波动大需谨慎")

注意事项

  1. 单位问题:ATR 的单位与价格相同,不同价格资产不可直接比较
  2. 趋势无关:ATR 只衡量波动性,不判断趋势方向
  3. 滞后性:ATR 是移动平均,会有一定滞后
  4. 突发事件:重大新闻可能导致 ATR 突然暴涨
  5. 周期选择:短周期更敏感,长周期更平滑
  6. 止损幅度:ATR 倍数需根据市场和策略调整
  7. 仓位控制:高 ATR 时应减小仓位降低风险

相关指标

实战要点

ATR 的最佳用途

止损设置:根据市场波动动态调整止损 ✅ 仓位管理:波动大时减仓,波动小时加仓 ✅ 突破确认:ATR 扩张的突破更可靠 ✅ 市场状态:判断市场活跃度

常见错误

❌ 忽略 ATR 变化,使用固定止损 ❌ 高波动时使用大仓位 ❌ 将 ATR 用于方向判断 ❌ 不同价格资产直接比较 ATR

经验法则

  • 1-1.5 倍 ATR:激进止损,适合短线
  • 2-2.5 倍 ATR:标准止损,平衡风险收益
  • 3-4 倍 ATR:宽松止损,适合趋势跟踪
  • ATR < 价格的 2%:低波动,注意突破
  • ATR > 价格的 5%:高波动,谨慎交易

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