I would like to get the bar index of the last occurrence of pivothigh so I can use it to plot an indicator starting from that index. Not familiar with pinescript and the references are limited
Asked
Active
Viewed 3,296 times
2 Answers
2
You can usesr the built-in barssince
function and use the result in the history reference operator []
barssincePhigh = barssince(phigh)
barssincePlow = barssince(plow)
However your pivots trigger after 10 bars by default, add leftbars
value to the barsisnce result to show the high/low pivot:
//@version=4
study("Pivot Prices", overlay=true)
leftbars = input(10, minval=1, title='Bars to the left')
rightbars = input(10, minval=1, title='Bars to the right')
phigh = pivothigh(high, leftbars, rightbars)
plow = pivotlow(low, leftbars, rightbars)
barssincePhigh = barssince(phigh) + leftbars
barssincePlow = barssince(plow) + leftbars
if phigh
label1 = label.new(bar_index[barssincePhigh], high[barssincePhigh], text=tostring(high[barssincePhigh]), style = label.style_labeldown, color = color.orange)
if plow
label2 = label.new(bar_index[barssincePlow], low[barssincePlow], text=tostring(low[barssincePlow]), style = label.style_labelup, color = color.green)

e2e4
- 3,428
- 6
- 18
- 30
0
I think this answer is wrong. ta.pivothigh This function returns the price of the pivot high point. It returns 'NaN', if there was no pivot high point.
[see the image to understand] : https://i.stack.imgur.com/BnnKL.png

Ahmed Elkady
- 1
- 1