Page 1 of 1

Create an indicator to plot a shape above the swing highs.

Posted: Wed May 17, 2023 6:40 pm
by pooya1072
Hi
I am so glad to join this forum.
I'm very new in Pine Script so sorry if my question is very elementary. This is just to learning and understanding the concepts.
I want to write an indicator that searches for 3 consecutive Bar, that middle one's High is higher than two others, then plot a "xcross" shape above the middle Bar use "plotshape" command.
I used this code, but the "xcross" shape is plotted above the first candle, not the middle one.

Code: Select all

//@version=5
indicator("My script" , overlay = true)
swing_highs=(high[1]>high[0] and high[1]>high[2]) 
plotshape(swing_highs)

Re: Create an indicator to plot a shape above the swing highs.

Posted: Thu May 25, 2023 6:04 am
by Steve Burman
Hi Pooya1072,

Your question may seem elementary but it is providing a challenge because in order to mark the swing high with an X we need to know the next bar and then mark the previous bar. Can I get back to you on this?

Re: Create an indicator to plot a shape above the swing highs.

Posted: Thu May 25, 2023 8:12 am
by Deep-Wave
Hi Pooya1072,
All you have to do is define the offset parameter in the plot shape function to -1 and you're spot on!
This parameter shifts the plot backwards by one candle. Of course, the timing doesn't change, so it still will be plotted when the current bar closes, but this time on the correct previous bar.
Hope this helps, :cool:
Cheers,

Code: Select all

plotshape(swing_highs, offset = -1)