无论时间范围如何,每次更改时都会更新变量

Update variables at every change, regardless of timeframe

我想显示一个带有当前 close 的标签,该标签根据相对于 close 的最后(即柱内)值的变化进行着色,从而复制符号值在关注列表中的着色方式(值的每次更改)

但是,Pine Script 似乎只会在新柱的开始更新 user/script 变量(相对于图表的时间帧分辨率)。

尽管 close 将始终 return 最新值,但无法(我发现)访问 非常最后的 close— 只有 close[1],这是最后一个 bar 的 收盘价而不是close 的真实前值。我需要一些类似于 close[0,1] 的东西,形式为 source[bar_index, array_index_of_changes_within_bar_index].

我已经尝试了几种方法来解决这个问题:数组,var 和非 var 变量的组合,security() 1s 分辨率(这本来是一个不合格的解决方案,但不管, Pine 脚本不允许)。

关于这是否可以在 Pine Script 中实现有什么建议吗?

您可以像这样使用 varip 声明的数组来进行紧密比较 intrabar

var label close_label = label.new(x = bar_index, y = close, style = label.style_label_left, size = size.normal, textcolor = color.white)
varip float[] intrabar_closes = array.new_float()

array.unshift(intrabar_closes, close)
if array.size(intrabar_closes) > 2
    array.pop(intrabar_closes)
col = array.size(intrabar_closes) > 1 ? array.get(intrabar_closes, 0) >= array.get(intrabar_closes, 1) ? color.lime : color.red : na
label.set_xy(close_label, x = bar_index, y = close)
label.set_color(close_label, color = col)
label.set_text(close_label, text = tostring(close))