I have a pretty standard bollinger indicator with lines plotted as "upper" and "lower", and I want to put a "tag" on the last occurrence of a cross above or below the band. I am using barssince to get high > upper and low < lower, and then adding labels with a bar index per the below code.
The calculations work, and it is printing the tags, but I was expecting only 1 high, and 1 low result from Barssince, but I am instead getting multiple highs and lows.
I have tried a few different ways from other answers, but can't seem to just get the LAST one only
Any help greatly appreciated.
Thanks
//@version=4
study(title="BB flag", shorttitle="BB flag", overlay=true)
source = close
Boll_Length = input(20, minval=1), Mult = input(2.0, minval=0.001, maxval=50)
basis = sma(source, Boll_Length)
dev = Mult * stdev(source, Boll_Length)
upper = basis + dev
lower = basis - dev
plot(upper, color=color.blue, title="Upper")
plot(lower, color=color.blue, title="Lower")
Last_Top = barssince(high >= upper)
Last_Bot = barssince(low <= lower)
label1 = label.new(bar_index[Last_Top] , high[Last_Top], text=tostring(high[Last_Top]), style = label.style_labeldown, color = color.orange)
label2 = label.new(bar_index[Last_Bot], low[Last_Bot], text=tostring(low[Last_Bot]), style = label.style_labelup, color = color.green)