Pharos
TATrend

TA.EMA()

指数移动平均 (Exponential Moving Average)

对近期价格赋予更高权重的移动平均线,对价格变化更敏感。

语法

python
TA.EMA(data, timeperiod=30)

参数

参数名类型必选默认值说明
dataany-价格数据数组
timeperiodany-时间周期,默认 30

返回值

返回 EMA 值数组

计算方法

EMA(t) = EMA(t-1) + α × (Price(t) - EMA(t-1))

其中 α = 2 / (N + 1),N 为周期数

特点

  • 权重递减: 近期数据权重大,远期数据权重小
  • 反应快: 比 SMA 更快响应价格变化
  • 常用于 MACD: MACD 指标基于 EMA 计算

基础示例

python
def main():
    records = exchange.GetRecords()
    closes = [r['Close'] for r in records]
    
    # 计算 12 日和 26 日 EMA
    ema12 = TA.EMA(closes, 12)
    ema26 = TA.EMA(closes, 26)
    
    # EMA 金叉死叉
    if ema12[-1] > ema26[-1] and ema12[-2] <= ema26[-2]:
        Log("EMA 金叉,买入信号")
    elif ema12[-1] < ema26[-1] and ema12[-2] >= ema26[-2]:
        Log("EMA 死叉,卖出信号")

高级应用

1. EMA 三线系统

python
def main():
    records = exchange.GetRecords()
    closes = [r['Close'] for r in records]
    
    ema12 = TA.EMA(closes, 12)
    ema26 = TA.EMA(closes, 26)
    ema50 = TA.EMA(closes, 50)
    
    # 多头排列
    if ema12[-1] > ema26[-1] > ema50[-1]:
        Log("EMA 多头排列")
        
        # 回踩确认
        if closes[-1] < ema12[-1] and closes[-1] > ema26[-1]:
            Log("回踩EMA12-26区间,买入机会")
    
    # 空头排列
    elif ema12[-1] < ema26[-1] < ema50[-1]:
        Log("EMA 空头排列")

2. EMA 动态止损

python
def main():
    records = exchange.GetRecords()
    closes = [r['Close'] for r in records]
    current_price = closes[-1]
    
    ema20 = TA.EMA(closes, 20)
    
    # 使用EMA作为追踪止损
    if current_price < ema20[-1]:
        Log(f"价格跌破EMA20 ({ema20[-1]:.2f}),触发止损")

3. EMA 带状区间

python
def main():
    records = exchange.GetRecords()
    closes = [r['Close'] for r in records]
    
    ema5 = TA.EMA(closes, 5)
    ema20 = TA.EMA(closes, 20)
    ema60 = TA.EMA(closes, 60)
    
    # 判断趋势强度
    ema_range = abs(ema5[-1] - ema60[-1]) / ema60[-1]
    
    if ema_range > 0.05:
        Log("EMA 带状展开,趋势强劲")
    elif ema_range < 0.02:
        Log("EMA 带状收缩,趋势减弱或震荡")

常用周期

周期名称用途
12短期MACD快线
26中期MACD慢线
50中长期趋势判断
200长期牛熊分界

与其他指标配合

EMA + MACD

python
ema50 = TA.EMA(closes, 50)
macd, signal, hist = TA.MACD(closes, 12, 26, 9)

# 趋势 + 动量双确认
if closes[-1] > ema50[-1] and macd[-1] > signal[-1]:
    Log("EMA50之上 + MACD金叉,强买入")

EMA + RSI

python
ema20 = TA.EMA(closes, 20)
rsi = TA.RSI(closes, 14)

# 回调买入机会
if closes[-1] > ema20[-1] and rsi[-1] < 40:
    Log("趋势向上,RSI回调,买入机会")

EMA + BOLL

python
ema20 = TA.EMA(closes, 20)
upper, middle, lower = TA.BOLL(closes, 20, 2)

# EMA作为趋势过滤
if closes[-1] > ema20[-1] and closes[-1] <= lower[-1]:
    Log("上升趋势中触及布林下轨,买入")

EMA vs SMA

特性EMASMA
权重近期权重大权重均等
灵敏度
滞后性
稳定性较低
适用趋势跟踪支撑阻力

注意事项

⚠️ 重要提醒

  1. 更敏感: EMA 对价格变化反应快,但也更容易产生假信号
  2. 初始值: 首个 EMA 值通常使用 SMA 初始化
  3. 不可逆: EMA 计算是累积的,历史数据会持续影响
  4. 震荡市: 快速反应在震荡市中可能频繁交易

优缺点

优点

  • 反应迅速
  • 重视近期价格
  • 适合趋势跟踪

缺点

  • 假信号较多
  • 震荡市表现差
  • 计算复杂

相关指标

返回目录

← 返回趋势指标目录