Sidechair

ta.barssince()

Browse the reference

Returns the bars elapsed since condition was last true: 0 on a true bar, na before the first one.

Try it in the editor

ta.barssince counts up by one each bar and resets to 0 whenever condition is true. The count includes the current bar, so the answer on the bar where the condition holds is 0.

Until the condition has been true once, the result is na. Compare against a number only after checking for na, for example nz(ta.barssince(cond), 9999).

Syntax

Pine Script
ta.barssince(condition: series bool) -> series int

Arguments

NameTypeDefaultDescription
conditionseries boolrequiredBoolean series. Each true bar resets the count to zero.

Returns

series int: bars since the condition last held, or na until it first holds.

Remarks

  • Compare with ta.valuewhen, which reports a value from the last true bar rather than a distance.

Example

Pine ScriptBars since the last crossoverOpen in editor
//@version=6
indicator("Bars since cross")
fast = ta.ema(close, 8)
slow = ta.ema(close, 21)
since = ta.barssince(ta.cross(fast, slow))
plot(since, "Bars", color = color.blue, style = plot.style_columns)

See also

Runs on Sidechair