ta.barssince()Bars Since
Counts the number of bars since the last time the condition was true.
Syntax
ta.barssince(condition) → series intArguments
| Parameter | Type | Description |
|---|---|---|
| condition | series bool | The 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
//@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
Related Functions
Frequently Asked Questions
Generate ta.barssince() Code with AI
Draft Pine Script using ta.barssince() with Pineify's AI Coding Agent, then review the arguments and test the result on your chart. Usage depends on your plan and available credits.
Related Pine Script Functions
ta.valuewhen() - Value When
Learn how to use ta.valuewhen() in Pine Script. Get the value of a series when a condition was true.
ta.highest() - Highest Value
Learn how to use ta.highest() in Pine Script. Find the highest value over a lookback period for Donchian Channels.
ta.lowest() - Lowest Value
Learn how to use ta.lowest() in Pine Script. Find the lowest value over a lookback period.
ta.lowestbars() - Lowest Bars Offset
Learn how to use ta.lowestbars() in Pine Script. Find how many bars ago the lowest value occurred.