Pine script: plotting horizontal lines at every pivothigh/pivotlow value

Viewed 26

I'm trying to plot a horizontal line (extended in both directions) at every pivothigh/pivotlow value. I'm almost there but I'm getting stuck at the last point. Could someone help me out? This is just a dummy code using only ta.pivothigh() to show where I'm getting stuck.

//@version=5
indicator(title="test", shorttitle="test", overlay=true)

pivotLength = 100
highOffset = high[pivotLength]
pivotHigh = ta.pivothigh(high, pivotLength, pivotLength)
yValues = ta.valuewhen(not na(pivotHigh), highOffset, 0)
line levelLine = line.new(bar_index, yValues, bar_index - 1, yValues, extend = extend.both) // <= getting stuck here

Unfortunately, I'm only repeatedly plotting the last horizontal line (levelLine). I don't know how to plot the previous horizontal lines (yes, I'm aware that if there are a lot of pivot values, not all horizontal lines will be displayed due to Pine Script's limitations).

2 Answers

I figured it out:

//@version=5
indicator(title="test", shorttitle="test", overlay=true)

pivotLength = 100
highOffset = high[pivotLength]
if ta.pivothigh(high, pivotLength, pivotLength)
    line.new(bar_index, highOffset , bar_index - 1, highOffset , extend = extend.both, color = color.red) 
//@version=5
indicator(title="test", shorttitle="test", overlay=true)

pivotLength = 100
highOffset = ta.pivothigh(high, pivotLength, pivotLength)
if not(na(highOffset))
    line levelLine = line.new(bar_index, highOffset, bar_index - 1, highOffset, extend = extend.both, color = color.red) 
Related