TAMomentum
TA.MACD()
移动平均收敛散度 (Moving Average Convergence/Divergence)
通过快慢 EMA 的差值判断趋势和动能,最常用的趋势跟踪指标之一。
语法
python
TA.MACD(data, fastperiod=12, slowperiod=26, signalperiod=9)参数
| 参数名 | 类型 | 必选 | 默认值 | 说明 |
|---|---|---|---|---|
| data | any | 否 | - | 价格数据数组(通常使用收盘价) |
| fastperiod | any | 否 | - | 快线周期,默认 12 |
| slowperiod | any | 否 | - | 慢线周期,默认 26 |
| signalperiod | any | 否 | - | 信号线周期,默认 9 |
返回值
返回 (macd, signal, histogram) 元组:
macd: MACD 线(DIF)= EMA(12) - EMA(26)signal: 信号线(DEA)= EMA(MACD, 9)histogram: 柱状图(MACD - Signal)
计算方法
- DIF (MACD线) = EMA(Close, 12) - EMA(Close, 26)
- DEA (信号线) = EMA(DIF, 9)
- MACD柱 = DIF - DEA
信号解读
金叉死叉
- 金叉: MACD 上穿 Signal,买入信号
- 死叉: MACD 下穿 Signal,卖出信号
零轴穿越
- 上穿零轴: 中长期转为多头
- 下穿零轴: 中长期转为空头
柱状图
- 柱状图放大: 动能增强
- 柱状图缩小: 动能减弱
- 柱状图变色: 趋势可能反转
背离
- 顶背离: 价格创新高但 MACD 未创新高,可能反转
- 底背离: 价格创新低但 MACD 未创新低,可能反弹
基础示例
python
def main():
records = exchange.GetRecords()
closes = [r['Close'] for r in records]
macd, signal, hist = TA.MACD(closes, 12, 26, 9)
# MACD 金叉
if macd[-1] > signal[-1] and macd[-2] <= signal[-2]:
Log("MACD 金叉,买入信号")
# MACD 死叉
elif macd[-1] < signal[-1] and macd[-2] >= signal[-2]:
Log("MACD 死叉,卖出信号")
# 柱状图变化
if hist[-1] > 0 and hist[-1] > hist[-2]:
Log("MACD 柱状图放大,多头动能增强")
elif hist[-1] < 0 and hist[-1] < hist[-2]:
Log("MACD 柱状图放大,空头动能增强")高级应用
1. MACD 零轴策略
python
def main():
records = exchange.GetRecords()
closes = [r['Close'] for r in records]
macd, signal, hist = TA.MACD(closes, 12, 26, 9)
# 零轴上方金叉(强势)
if macd[-1] > 0 and macd[-1] > signal[-1] and macd[-2] <= signal[-2]:
Log("零轴上方金叉,强势买入")
# 零轴下方死叉(弱势)
elif macd[-1] < 0 and macd[-1] < signal[-1] and macd[-2] >= signal[-2]:
Log("零轴下方死叉,弱势卖出")
# 穿越零轴
if macd[-1] > 0 and macd[-2] <= 0:
Log("MACD 上穿零轴,中期转多")
elif macd[-1] < 0 and macd[-2] >= 0:
Log("MACD 下穿零轴,中期转空")2. MACD 背离检测
python
def detect_macd_divergence(prices, macd_values, period=10):
"""检测 MACD 背离"""
if len(prices) < period or len(macd_values) < period:
return None
# 顶背离
if (prices[-1] > max(prices[-period:-1]) and
macd_values[-1] < max(macd_values[-period:-1])):
return "顶背离"
# 底背离
if (prices[-1] < min(prices[-period:-1]) and
macd_values[-1] > min(macd_values[-period:-1])):
return "底背离"
return None
def main():
records = exchange.GetRecords()
closes = [r['Close'] for r in records]
macd, signal, hist = TA.MACD(closes, 12, 26, 9)
divergence = detect_macd_divergence(closes, macd, 10)
if divergence == "顶背离":
Log("MACD 顶背离,价格可能回调")
elif divergence == "底背离":
Log("MACD 底背离,价格可能反弹")3. MACD 柱状图策略
python
def main():
records = exchange.GetRecords()
closes = [r['Close'] for r in records]
macd, signal, hist = TA.MACD(closes, 12, 26, 9)
# 柱状图由负转正
if hist[-1] > 0 and hist[-2] <= 0:
Log("柱状图由负转正,短期买入信号")
# 柱状图由正转负
elif hist[-1] < 0 and hist[-2] >= 0:
Log("柱状图由正转负,短期卖出信号")
# 柱状图连续放大
if hist[-1] > hist[-2] > hist[-3] and hist[-1] > 0:
Log("柱状图连续放大,多头动能强")4. 多周期 MACD 共振
python
def main():
# 不同周期的K线
records_1h = exchange.GetRecords(PERIOD_H1)
records_4h = exchange.GetRecords(PERIOD_H4)
records_1d = exchange.GetRecords(PERIOD_D1)
# 计算不同周期的 MACD
macd_1h, signal_1h, _ = TA.MACD([r['Close'] for r in records_1h], 12, 26, 9)
macd_4h, signal_4h, _ = TA.MACD([r['Close'] for r in records_4h], 12, 26, 9)
macd_1d, signal_1d, _ = TA.MACD([r['Close'] for r in records_1d], 12, 26, 9)
# 多周期金叉共振
if (macd_1h[-1] > signal_1h[-1] and
macd_4h[-1] > signal_4h[-1] and
macd_1d[-1] > signal_1d[-1]):
Log("多周期MACD金叉共振,强买入信号")参数优化建议
| 交易周期 | 推荐参数 | 特点 |
|---|---|---|
| 超短线 | (6, 13, 5) | 反应快,信号多 |
| 短线 | (8, 17, 9) | 较灵敏 |
| 标准 | (12, 26, 9) | 经典配置 |
| 中长线 | (19, 39, 9) | 平滑,假信号少 |
与其他指标配合
MACD + RSI
python
macd, signal, hist = TA.MACD(closes, 12, 26, 9)
rsi = TA.RSI(closes, 14)
# 双重确认
if macd[-1] > signal[-1] and rsi[-1] < 30:
Log("MACD金叉 + RSI超卖,强买入")MACD + ADX
python
macd, signal, hist = TA.MACD(closes, 12, 26, 9)
adx = TA.ADX(highs, lows, closes, 14)
# 趋势 + 强度确认
if macd[-1] > signal[-1] and adx[-1] > 25:
Log("MACD金叉 + 强趋势,高质量信号")MACD + BOLL
python
macd, signal, hist = TA.MACD(closes, 12, 26, 9)
upper, middle, lower = TA.BOLL(closes, 20, 2)
# 金叉 + 突破中轨
if macd[-1] > signal[-1] and closes[-1] > middle[-1]:
Log("MACD金叉 + 突破布林中轨")注意事项
⚠️ 重要提醒:
- 震荡市场: MACD 容易产生假信号,建议配合 ADX 使用
- 滞后性: MACD 是滞后指标,适合趋势确认而非预测
- 参数敏感: 不同周期需要调整参数
- 背离信号: 背离是强反转信号,需重点关注
- 柱状图: 柱状图变化往往领先于金叉死叉
相关指标
- MACDEXT - MACD扩展版 - 可自定义MA类型
- PPO - 价格振荡百分比 - MACD的百分比版本
- APO - 绝对价格振荡器 - MACD的简化版本