Search code examples
pine-scriptpine-script-v5

Plotting pivot high/lows with offset vs Actually saving the price of pivot high/low


I am trying to make an array of pivot highs and pivot lows in pinescript to check ifthe price went above/below them.

Plotting the pivot points with offset works without any issues but I cannot seem to find what is the correct price value of the pivot highs/lows

Plotting a shape above the pivot high/low works as it should:

ph = ta.pivothigh(high, pivot_offset, pivot_offset)
pl = ta.pivotlow(low, pivot_offset, pivot_offset)
plotshape(ph, style=shape.circle,location=location.abovebar,color=color.red, size = size.tiny, offset = -pivot_offset)

Correctly marked pivot high with plothsape offset

The problem is when I try to save the pivothigh as a value to check if price went above it by adding this code, it still returns the same thing as ph even though I added the offset as high[offset]:

if na(ph) == false
    pivothigh := high[pivot_offset]

plotshape(pivothigh, style=shape.circle,location=location.abovebar,color=color.red, size = size.tiny

Incorrectly marked pivot high with candle offset

How can I save the correct pivot high candle to use it for checking the price crossings? And if it's not possible, how can I check if price went above/below the plotshape?


Solution

  • ta.pivothigh()
    This function returns price of the pivot high point. It returns 'NaN', if there was no pivot high point.

    ta.pivothigh() returns the pivot price. To use this price, no offset is needed. Offset is just there for visuals.

    Below code will plot the same:

    //@version=5
    indicator("My script", overlay=true)
    
    rb = 10
    
    ph = ta.pivothigh(10, rb)
    var float pivothigh_1 = na
    var float pivothigh_2 = na
    
    if (not na(ph))
        pivothigh_1 := ph
        pivothigh_2 := high[rb]
    
    plot(pivothigh_1, "pivothigh_1", color.green, 1, plot.style_circles)
    plot(pivothigh_2, "pivothigh_2", color.red, 1, plot.style_circles)