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

如何让Buy/Sell信号仅触发一次而非每根K线都显示?

解决Pine Script信号重复显示问题

原代码中买入/卖出信号会在每根符合条件的K线上重复显示,问题出在信号判断逻辑仅检查当前K线状态,未检测状态切换。要实现信号仅触发一次,需捕捉上涨/下跌状态的切换节点:

修改后的核心信号逻辑

  • 买入信号:当前K线为LinReg上涨蜡烛,且前一根为LinReg下跌蜡烛(状态从跌转涨的瞬间)
  • 卖出信号:当前K线为LinReg下跌蜡烛,且前一根为LinReg上涨蜡烛(状态从涨转跌的瞬间)

完整修改代码

study(title="Humble LinReg Candles", shorttitle="LinReg Candles ", format=format.price, max_labels_count=100, precision=4, overlay=true)

signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 200, defval = 11)
sma_signal = input(title="Simple MA (Signal Line)", type=input.bool, defval=true)

lin_reg = input(title="Lin Reg", type=input.bool, defval=true)
linreg_length = input(title="Linear Regression Length", type=input.integer, minval = 1, maxval = 200, defval = 11)

bopen = lin_reg ? linreg(open, linreg_length, 0) : open
bhigh = lin_reg ? linreg(high, linreg_length, 0) : high
blow = lin_reg ? linreg(low, linreg_length, 0) : low
bclose = lin_reg ? linreg(close, linreg_length, 0) : close

r = bopen < bclose

signal = sma_signal ? sma(bclose, signal_length) : ema(bclose, signal_length)

plotcandle(r ? bopen : na, r ? bhigh : na, r ? blow: na, r ? bclose : na, title="UpCandle", color=#19968c, wickcolor=#19968c, bordercolor=#19968c, editable= true)
plotcandle(r ? na : bopen, r ? na : bhigh, r ? na : blow, r ? na : bclose, title="DownCandle", color=#b12a72, wickcolor=#b12a72, bordercolor=#b12a72, editable= true)

//////////////////////////////////////////////////////
//Buy Sell Signals
// 捕捉状态切换的信号,仅在首次触发时显示
buySignal = r and not r[1]
sellSignal = not r and r[1]

plotshape(buySignal, title="Buy", text="B", location=location.belowbar, style=shape.labelup, size=size.normal, color=#24c491, textcolor=color.white)
plotshape(sellSignal, title="Sell", text="S", location=location.abovebar, style=shape.labeldown, size=size.normal, color=#DA1D81, textcolor=color.white)

alertcondition(buySignal, title="LG Buy", message="Buy")
alertcondition(sellSignal, title="LG Sell", message="Sell")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 01:20:23