pooya1072
Pine Script Rookie
Pine Script Rookie
Posts: 1
Joined: May 17th, 2023

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

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)

Steve Burman
Moderator
Moderator
Posts: 109
Joined: January 13th, 2023

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

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?

Deep-Wave
Pine Script Master
Pine Script Master
Posts: 44
Joined: September 4th, 2020
Contact: TradingView Profile

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

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)

Return to “Pine Script Q&A”