Search code examples
pine-script

pinescript: ta.pivothigh misunderstanding


I'm trying to understand what the ta.pivothigh method is doing in pinescript V5.

I was expecting that it was looking nearby for the highest price, but when I use a simple script like this:

float ph = ta.pivothigh(0, 2)
plot(ph, color = color.blue ,  linewidth = 2)

The pivot points are not on the highest price, but there is an offset on the right of 2 bars. This offset is controlled by the '2' value.

Like this:

enter image description here

I was expecting that the pivot point was exactly where I put the red arrow.

Does someone has en explanation please?

(I have search on stackoverflow, on the official doc etc... but I do not understand).


Solution

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

    ARGUMENTS
    leftbars (series int/float) Left strength.
    rightbars (series int/float) Right strength.

    It takes two (or three with a source) arguments. It will find the pivot high based on the left and right bars arguments.

    You can break it down as follows:

    1. Pick a candle.
    2. There should be no higher high to the left side within the leftbars period.
    3. There should be no higher high to the right side within the rightbars period.
    4. If all true, then the selected candle is a pivot high.

    Looking right is where the delay/offset comes from. If you want to find a pivot point, you need to wait for a few bars (rightbars) to confirm the price action. Therefore, there will be some delay and it will be reflected in the plot.

    However, plot*() functions has an argument called offset which you can use.

    If you call your plot() with the offset=-2, it will shift your plot two bars to the left and then your line will align with the pivot points.

    //@version=5
    indicator("My script", overlay=true)
    
    float ph = ta.pivothigh(0, 2)
    plot(ph, color = color.blue ,  linewidth = 2)
    plot(ph, color = color.green ,  linewidth = 2, offset=-2)
    

    enter image description here