UtilityPine Script v6

ta.barssince()

Bars Since

Counts the number of bars since the last time the condition was true.

Syntax

Syntax
ta.barssince(condition) → series int

Arguments

ParameterTypeDescription
conditionseries boolThe condition to check for.

Returns

Number of bars since condition was true.

Remarks

If the condition has never been met prior to the current bar, the function returns na. Please note that using this variable/function can cause indicator repainting.

Code Examples

Pine Script v6 Example
//@version=6
indicator("ta.barssince")
// get number of bars since last green bar
plot(ta.barssince(close >= open))

Trading Applications

Count bars since a signal occurred

Build time-based exit rules

Measure duration since last crossover

Create cooldown periods between signals

Frequently Asked Questions

Generate ta.barssince() Code with AI

Skip the manual coding. Use Pineify's AI Coding Agent to generate Pine Script code using ta.barssince() and other built-in functions instantly.