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

PineScript如何仅在首个同色bar绘制线条,而非全部同色bar

PineScript 趋势首次触发标记绘制修复方案

问题根源

原代码的绘图触发条件为所有处于多头/空头状态的bar,因此每一根同色bar都会生成标记,与仅在趋势切换的第一个变色bar绘制的预期不符。

修改方案

新增趋势切换判定逻辑,仅当当前bar首次进入多头/空头状态时才触发绘制,其余逻辑完全保留原有设置。

修改后完整代码

//@version=4
study(title="XTL + TQR", shorttitle= "XTL", overlay=true)

//Expert Trend Locator (XTL)
len     = input(34, minval=1, title = "XTL Period")
hLmt    = input(34, title="Threshold Value", minval=10)

xtl_src = hlc3
XTL = cci(xtl_src,len)

bull = XTL > hLmt
bear = XTL < -hLmt
neutral = (XTL <= hLmt) and (XTL >= -hLmt)

upcolor = color.rgb(0, 135, 225)
dwncolor = color.rgb(255, 0, 0)
ntlcolor = color.rgb(255, 255, 255, 30)

barcolor(bull? upcolor : neutral? ntlcolor : dwncolor)

//tqr
range = high-low
tqr = 1.5
tqr2 = 0.5
tqr3 = 3

// 仅修改此处触发条件,增加首次趋势判断
plot((bull and not bull[1])? (low+range*tqr) : (bear and not bear[1])? (low-range*tqr2) : na , color = color.white, style = plot.style_cross)
plot((bull and not bull[1])? (low-range*tqr2) : (bear and not bear[1])? (low+range*tqr) : na, color = color.red, style = plot.style_cross)
plot((bull and not bull[1])? (low+range*tqr3) : (bear and not bear[1])? (low-range*tqr3) : na , color = color.yellow, style = plot.style_cross)

修改说明

仅调整了3个plot函数的触发条件:

  • 原多头触发条件bull替换为bull and not bull[1],代表当前bar是多头且前一根bar不是多头,即刚切换到多头趋势的第一个bar
  • 原空头触发条件bear替换为bear and not bear[1],代表刚切换到空头趋势的第一个bar

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 04:54:04