You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pine Script实现MACD交叉背景染色及修复高亮延迟问题

MACD背景染色功能修正方案

原代码核心问题

  • 指标计算错误:MACD值的计算逻辑写错,原代码写为macd = fast_ma - slow_length,实际应为快慢均线的差值fast_ma - slow_ma,错误的计算会导致指标完全失真,是信号延迟错误的核心原因
  • 交叉判断逻辑错误:MACD金叉/死叉指的是MACD线与信号线(signal)的交叉,原代码错误判断为MACD线与直方图数值(hist)的交叉,完全不符合指标定义
  • 缺少0轴位置限制:原代码没有叠加MACD在0轴上方/下方的判断条件,无法匹配需求的染色规则

修正后完整代码

// Getting inputs
fast_length = input(title="Fast Length", type=input.integer, defval=12)
slow_length = input(title="Slow Length", type=input.integer, defval=26)
src = input(title="Source", type=input.source, defval=close)
signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval=9)
sma_source = input(title="Oscillator MA Type", type=input.string, defval="EMA", options=["SMA", "EMA"])
sma_signal = input(title="Signal Line MA Type", type=input.string, defval="EMA", options=["SMA", "EMA"])
// 染色持续周期设置(不需要可以删除)
bg_duration = input(title="染色持续K线数", type=input.integer, defval=1, minval=1)
// Plot colors
col_macd = input(#2962FF, "MACD Line  ", input.color, group="Color Settings", inline="MACD")
col_signal = input(#FF6D00, "Signal Line  ", input.color, group="Color Settings", inline="Signal")
col_grow_above = input(#26A69A, "Above   Grow", input.color, group="Histogram", inline="Above")
col_fall_above = input(#B2DFDB, "Fall", input.color, group="Histogram", inline="Above")
col_grow_below = input(#FFCDD2, "Below Grow", input.color, group="Histogram", inline="Below")
col_fall_below = input(#FF5252, "Fall", input.color, group="Histogram", inline="Below")
// 自定义染色颜色
col_bullish_bg = input(color.new(color.aqua, 90), title="0线下金叉背景色", group="染色设置")
col_bearish_bg = input(color.new(color.orange, 90), title="0线上死叉背景色", group="染色设置")
// Calculating
fast_ma = sma_source == "SMA" ? sma(src, fast_length) : ema(src, fast_length)
slow_ma = sma_source == "SMA" ? sma(src, slow_length) : ema(src, slow_length)
macd = fast_ma - slow_ma // 修正原计算错误
signal = sma_signal == "SMA" ? sma(macd, signal_length) : ema(macd, signal_length)
hist = macd - signal

plot(macd, title="MACD", color=col_macd)
plot(signal, title="Signal", color=col_signal)
plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below)))

// 信号判断:符合需求的染色规则
bullish_signal = crossover(macd, signal) and macd < 0 // 0线下方金叉
bearish_signal = crossunder(macd, signal) and macd > 0 // 0线上方死叉

// 染色逻辑,按设置的持续周期显示
bullish_go = barssince(bullish_signal) < bg_duration
bearish_go = barssince(bearish_signal) < bg_duration

bgcolor(bullish_go ? col_bullish_bg : na)
bgcolor(bearish_go ? col_bearish_bg : na)

可选调整说明

如果不需要持续染色,仅在信号触发的单根K线显示背景,可以删掉bg_duration相关参数,直接把信号判断赋值给染色变量即可:

bgcolor(bullish_signal ? color.new(color.aqua,90) : na)
bgcolor(bearish_signal ? color.new(color.orange,90) : na)

内容的提问来源于stack exchange,提问作者Blind Blake

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 23:45:03