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

求助:如何在MACD线等于零(中线)时绘制箭头?

问题:修正Pine Script中MACD线触达中线时绘制箭头的代码

原始需求

希望在MACD线恰好位于中线(即MACD线等于0)时绘制一个箭头,以下是原始代码:

//@version=5
indicator(title="Moving Average Convergence Divergence", shorttitle="MACD", timeframe="", timeframe_gaps=true)
// Getting inputs
fast_length = input(title="Fast Length", defval=12)
slow_length = input(title="Slow Length", defval=26)
src = input(title="Source", defval=close)
signal_length = input.int(title="Signal Smoothing",  minval = 1, maxval = 50, defval = 9)
sma_source = input.string(title="Oscillator MA Type",  defval="EMA", options=["SMA", "EMA"])
sma_signal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"])
// Plot colors
col_macd = input(#2962FF, "MACD Line  ", group="Color Settings", inline="MACD")
col_signal = input(#FF6D00, "Signal Line  ", group="Color Settings", inline="Signal")
col_grow_above = input(#26A69A, "Above   Grow", group="Histogram", inline="Above")
col_fall_above = input(#B2DFDB, "Fall", group="Histogram", inline="Above")
col_grow_below = input(#FFCDD2, "Below Grow", group="Histogram", inline="Below")
col_fall_below = input(#FF5252, "Fall", group="Histogram", inline="Below")
// Calculating
fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)
macdline = fast_ma - slow_ma
signal = sma_signal == "SMA" ? ta.sma(macdline, signal_length) : ta.ema(macdline, signal_length)
hist = macdline - signal
hline(0, "Zero Line", color=color.new(#787B86, 50))
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)))
plot(macdline, title="MACD", color=col_macd)
plot(signal, title="Signal", color=col_signal)

//plot arrow when macd is equal to midline
midline = 0
plotshape(macd=midline),style=shape.triangleup, location=location.bottom, color=color.orange, size=size.small)

错误提示

No value assigned to the series parameter

问题分析与修正代码

错误原因

  1. plotshape函数的核心参数是布尔序列(series),用来指定在哪些K线触发形状绘制,原始代码错误地使用了macd=midline的参数形式,且语法存在括号不匹配问题。
  2. 直接用macdline == 0判断浮点数相等容易因精度问题触发失败,建议用极小阈值判断或交叉信号。

修正后的完整代码

//@version=5
indicator(title="Moving Average Convergence Divergence", shorttitle="MACD", timeframe="", timeframe_gaps=true)
// Getting inputs
fast_length = input(title="Fast Length", defval=12)
slow_length = input(title="Slow Length", defval=26)
src = input(title="Source", defval=close)
signal_length = input.int(title="Signal Smoothing",  minval = 1, maxval = 50, defval = 9)
sma_source = input.string(title="Oscillator MA Type",  defval="EMA", options=["SMA", "EMA"])
sma_signal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"])
// Plot colors
col_macd = input(#2962FF, "MACD Line  ", group="Color Settings", inline="MACD")
col_signal = input(#FF6D00, "Signal Line  ", group="Color Settings", inline="Signal")
col_grow_above = input(#26A69A, "Above   Grow", group="Histogram", inline="Above")
col_fall_above = input(#B2DFDB, "Fall", group="Histogram", inline="Above")
col_grow_below = input(#FFCDD2, "Below Grow", group="Histogram", inline="Below")
col_fall_below = input(#FF5252, "Fall", group="Histogram", inline="Below")
// Calculating
fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)
macdline = fast_ma - slow_ma
signal = sma_signal == "SMA" ? ta.sma(macdline, signal_length) : ta.ema(macdline, signal_length)
hist = macdline - signal
hline(0, "Zero Line", color=color.new(#787B86, 50))
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)))
plot(macdline, title="MACD", color=col_macd)
plot(signal, title="Signal", color=col_signal)

// 绘制MACD线触达中线的箭头
// 用极小阈值判断MACD线接近0,避免浮点精度问题
macdNearZero = math.abs(macdline) < 1e-5
// 也可以用交叉信号:MACD线从下往上穿过0或从上往下穿过0
// macdCrossZero = ta.crossover(macdline, 0) or ta.crossunder(macdline, 0)

// 绘制箭头,根据MACD线位置选择上下箭头
plotshape(macdNearZero and macdline[1] < 0, style=shape.triangleup, location=location.belowbar, color=color.orange, size=size.small, title="MACD上穿零轴")
plotshape(macdNearZero and macdline[1] > 0, style=shape.triangledown, location=location.abovebar, color=color.orange, size=size.small, title="MACD下穿零轴")

关键修正点

  • 定义macdNearZero作为触发条件,用math.abs(macdline) < 1e-5判断MACD线接近零轴,解决浮点精度问题。
  • 区分上穿和下穿零轴的情况,分别绘制向上和向下箭头,提升实用性。
  • 修正plotshape的参数格式,将布尔序列作为第一个参数传入,语法完全符合Pine Script v5要求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 08:30:02