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

Pine Script Barcolor问题:如何仅标记条件首次出现的K线而非全部?

解决方案

要实现仅标记首次满足条件的K线,核心是判断「当前K线符合条件,但前一根K线不符合」,避免连续标记。以下是修改后的代码:

//@version=5
indicator("My script", overlay = true)
rcan = input(false, "Rcan")

// 原条件判断
bR = close > close[1] and low > low[1] and high > high[1]
Rb = close < close[1] and low < low[1] and high < high[1]

// 新增:判断首次出现的条件
firstBR = bR and not bR[1]  // 当前满足上涨条件,前一根不满足
firstRB = Rb and not Rb[1]  // 当前满足下跌条件,前一根不满足

// 仅在首次满足时赋予颜色
rcol = firstBR ? color.green : firstRB ? color.red : na

barcolor(rcan ? rcol : na)

关键改动说明

  • 新增firstBR和firstRB变量:通过not bR[1]和not Rb[1]排除连续满足条件的情况,只保留条件触发的第一根K线
  • 调整rcol赋值逻辑:仅当首次触发条件时才返回对应颜色,其余情况返回na(不标记)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 06:10:02