回顾第一个绿色条的时间

looking back in time for first green bar

我正在玩一些代码(对 Pine 来说非常新),我在其中寻找连续的 3 根绿色蜡烛,并为第一根蜡烛添加标签。 bar_index-2 这部分很简单。 但是,假设连续有 5 根绿色蜡烛。我有多个标签,但我只希望标签位于连续 5 个开始的第一根绿色蜡烛上。我如何获得该信息? 感谢您的帮助! 罗布

这需要一个下降条来重置条件:

//@version=4
study("", "", true)

// Define our boolean condition for an up bar.
upBar = close > open

// Here we do many things in one line:
// 1. Using `sum()`, we will add a value for the last 3 bars.
//    That's the `sum(..., 3)` part.
// 2. The value we add for each bar is the evaluation of an expression.
//    That expression is a ternary: `upBar ? 1 : 0`.
//    It evaluates to `1` if the current bar is an upBar,
//    and to zero when it is not.
// 3. We use the `==` boolean operator to compare the result of `sum()` to `3`.
//    When it is equal to `3`, `true` will be assigned to `upBars3`.
//    This will occur when `upBar` was true on each of the last 3 bars.
upBars3 = sum(upBar ? 1 : 0, 3) == 3

// Here we generate a signal when our `upBars3` boolean variable is `true`,
// and it was not `true` on the previous bar, which is the `not upBars3[1]` part.
signal = upBars3 and not upBars3[1]

// Here we plot an arrow 2 bars back when our signal triggers.
// Note that if you publish a script plotting in the past like this,
// it is expected that you will explain this in your script's description
// so you do not mislead traders into thinking your script is prescient.
plotchar(signal, "signal", "▲", location.top, size = size.tiny, offset = - 2)