'labels on tops and bottoms

please help me to plot labels on tops and bottoms based on this short indicator I hope the result will be like this pic lables. I tried but it plots labels on all candles not on pivots tops and bottoms only.

//@version=5
indicator('piv lbl', overlay=true, max_lines_count=500)

period   = input.int(200, title="Period")

    
hh = ta.highest(high, period)
ll = ta.lowest(low,   period)

hsrc =  input.source(high , title="High source")
lsrc =  input.source(low  , title="Low  source")

newhh   = high > hh[1]
newll   = low  < ll[1]

hhb = ta.barssince(hh)
plot(hhb)


if hh
    label.new(bar_index, na, yloc=yloc.abovebar) 
if ll
    label.new(bar_index, na, yloc=yloc.belowbar)


Solution 1:[1]

You can use the ta.pivothigh() and ta.pivotlow() functions to find the pivots.

//@version=5
indicator("PivotHigh", overlay=true)
leftBars = input(10)
rightBars=input(10)
ph = ta.pivothigh(leftBars, rightBars)
pl = ta.pivotlow(leftBars, rightBars)
plotshape(ph, "Pivot High", shape.triangledown, location.abovebar, color.green, -rightBars, "Pivot\nHigh", size=size.small)
plotshape(pl, "Pivot Low", shape.triangleup, location.belowbar, color.red, -rightBars, "Pivot\nLow", size=size.small)

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 vitruvius