Sidechair

ta.highestbars()

Browse the reference

Returns how many bars back the highest value of source in the last length bars occurred, as 0 or a negative number.

Try it in the editor

ta.highestbars returns 0 when the highest value in the window is on the current bar, -1 when it was one bar ago, and so on to -(length - 1). When several bars tie for the highest value, the most recent one wins.

With a single argument, ta.highestbars(length), the source is high. Early bars, before the window is full, return na.

Syntax

Pine Script
ta.highestbars(source: series int/float, length: series int) -> series int

Arguments

NameTypeDefaultDescription
sourceseries int/floatrequiredSeries to scan. Leave it out, passing only the length, to scan high.
lengthseries intrequiredNumber of bars in the window. A whole number of 1 or more.

Returns

series int: the bar offset of the window's highest value: 0 for the current bar, negative for earlier ones.

Remarks

  • Use the negative offset to read the bar itself: close[-ta.highestbars(high, 10)] is the close on the highest-high bar.

Example

Pine ScriptBars since the 20-bar highOpen in editor
//@version=6
indicator("Bars since high")
back = -ta.highestbars(high, 20)
plot(back, "Bars since 20-bar high", color = color.orange, style = plot.style_columns)

See also

Runs on Sidechair