Highest High in a Range Pine Script
I Am Trying to Label and Change the Color of the Highest Bar in the Range. Instead, It Paints All Bars with the Color and Does Not Plot the Label on the...
I am trying to label and change the color of the highest bar in the range. Instead, it paints all bars with the color and does not plot the label on the highest bar but on the last bar?
//@version=5
indicator(title='Label', overlay=true)
highestHigh = ta.highest(high, 20)
lowestLows = ta.lowest(low, 20)
PmeU = if highestHigh
label.new(bar_index, na, "Highest pirce was: " + str.tostring(highestHigh), color=color.green, textcolor=color.black, style=label.style_label_down, yloc=yloc.abovebar)
label.delete(PmeU[1])
barcolor(highestHigh ? color.blue : na)
1 Answer
ta.highest() returns the highest price within the lookback period. It does not return if the current high is the highest. You need an additional check for that.
Coming to the issue with the label, it is because you always delete the previous one. I don't think you want to delete any label in your case.
//@version=5
indicator(title='Label', overlay=true)
highestHigh = ta.highest(high, 20)
lowestLows = ta.lowest(low, 20)
is_high = high >= highestHigh
PmeU = if is_high
label.new(bar_index, na, "Highest pirce was: " + str.tostring(highestHigh), color=color.green, textcolor=color.black, style=label.style_label_down, yloc=yloc.abovebar)
barcolor(is_high ? color.blue : na)