Comparing ta.barssince and ta.valuewhen in Pine Script v6
Tracking past market conditions without manual loop indexing is vital for time-based exits, pullback validation, and multi-stage trade confirmation.
| Function | Arguments | Returned value | Typical trading use case |
|---|---|---|---|
| ta.barssince(condition) | condition (series bool) | Integer bar count or na | Enforcing a 5-bar cooldown between consecutive trade signals |
| ta.valuewhen(condition, source, 0) | condition, source, occurrence | Source value at most recent event | Anchoring stop loss to the breakout candle low |
| ta.valuewhen(condition, source, 1) | condition, source, occurrence | Source value at 2nd most recent event | Comparing current swing high against the previous swing high |
| ta.highestbars(source, length) | source, length | Negative offset to highest bar | Locating exact candlestick of highest peak within 20 bars |