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:
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).
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:
leftbars
period.rightbars
period.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)