有没有办法检查收盘价时间序列的一定数量的指数是否已经超过 above/below Pine Script 中的移动平均线系列?

Is there a way to check if a certain number of indices of the close price timeseries have crossed above/below a moving average series in Pine Script?

我正在尝试检查收盘价是否超过或低于 200 周期均线一定数量的柱线。

我知道内置的 ta.crossover() 函数只根据当前和以前的索引计算布尔值,但我想知道是否可以根据一定数量的还有历史吧。

非常感谢。

您可以为此使用 var 计数器。

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © vitruvius

//@version=5
indicator("My script", overlay=true)

sma = ta.sma(close, 50)

var cnt_up = 0
cnt_up := (close > sma) ? cnt_up + 1 : 0                    // Increase the counter if the price is above the SMA, reset otherwise

bg_col = (cnt_up > 5) ? color.new(color.green, 85) : na     // Check if the last 5 bars were above the SMA

plot(sma)
bgcolor(bg_col)