for 循环显示 x 条柱线或蜡烛收盘上布林线

the for loop to show x numbers of bars or candles closed upper bollinger

我可以在图表上显示,当 x 根蜡烛图或柱形图(比方说 x=5)在布林线上限上方连续收盘时,通过创建一个像这样的变量:

x_closed_above_upper = close > upper and close[1] > upper[1] and close[2] > upper[2] and close[3] > upper[3] and close[4] > upper[4]

是否可以使用for循环显示相同的东西,例如for i=0 to x(=5, in this case) by 1if close[i] > upper

我试过了,但是我对for循环的使用知识很有限。如果有人知道答案,我将不胜感激。非常感谢!

//@version=5
indicator(title="x numbers of candles closed consecutively above the upper bollinger band", overlay=true)



length = input.int(20, minval=1)
src = input(close, title='Source')
mult = input.float(2.0, minval=0.001, maxval=50, title='StdDev')
basis = ta.sma(src, length)
dev = mult * ta.stdev(src, length)
upper = basis + dev
lower = basis - dev
offset = input.int(0, 'Offset', minval=-500, maxval=500)
plot(basis, 'Basis', color=color.new(#FF6D00, 0), offset=offset)
p1 = plot(upper, 'Upper', color=color.new(#2962FF, 0), offset=offset)
p2 = plot(lower, 'Lower', color=color.new(#2962FF, 0), offset=offset)
fill(p1, p2, title='Background', color=color.rgb(33, 150, 243, 95))




// Red and Green candles

RedCandle = close <= open
GreenCandle = close >= open


// x numbers of candles closed consecutively above the upper bollinger band

`x_closed_above_upper = close > upper and close[1] > upper[1] and close[2] > upper[2] and close[3] > upper[3]
     and close[4] > upper[4]`


plotshape(x_closed_above_upper ? high : na, title='5 bars closed above the upper bollinger', text='5 bars\nclosed above\nupper bollinger', style=shape.labeldown, location=location.abovebar, color=color.new(color.red, 0), textcolor=color.new(color.white, 0), size=size.tiny)

// How is it possible to get the same data by using 
// for i=0 to x(here x would be 5) by 1, etc.?"
// if close[i] > upper ?
// Thank you for your help

这里不需要 for 循环。只需使用计数器并在每次价格收于上限上方时增加其价值。否则重置它。

cnt_target = 5
var up_cnt = 0
up_cnt := close > upper ? up_cnt + 1 : 0

plotshape(up_cnt == cnt_target, title='5 bars closed above the upper bollinger', text='5 bars\nclosed above\nupper bollinger', style=shape.labelup, location=location.belowbar, color=color.new(color.green, 0), textcolor=color.new(color.white, 0), size=size.tiny)