I am just starting out learning PineScript and sorry if this question is very elementary.
I want to compare three bars. If the middle bar’s high is greater than two others, then plot a shape above it by ‘plotshape’ command. The problem is that in the code below if the condition is true then a shape is plotted on the first bar. How can I change this code?
//@version=5
indicator("My script" , overlay = true)
swing_highs=(high[1]>high[0] and high[1]>high[2])
plotshape(swing_highs)
>Solution :
You can add not barstate.islast
into your plot statement to avoid plotting on the last bar of the chart. Using the offset will also place the shape above the peak bar.
plotshape(not barstate.islast and swing_highs, offset = -1)
Before: