UtilityPine Script v6

ta.valuewhen()Value When

Returns the value of the source series on the bar where the condition was true.

TradingView language reference

Syntax

Syntax
ta.valuewhen(condition, source, occurrence) → series float / int / bool / color

Arguments

ParameterTypeDescription
conditionseries boolCondition that marks relevant bars.
sourceseries int/float/bool/colorSeries value to recall when condition is true.
occurrencesimple intNonnegative occurrence index (0 = most recent matching bar, including the current bar).

Returns

The value of source when condition was true.

Remarks

Evaluate on every bar; avoid conditional execution and loops that disrupt its history. Consecutive true bars count as separate occurrences, not a single transition. Realtime condition/source changes can change the result before bar close.

Code Examples

Pine Script v6 Example
//@version=6
indicator("ta.valuewhen")
fast = ta.sma(close, 7)
slow = ta.sma(close, 14)
plot(ta.valuewhen(ta.cross(slow, fast), close, 0))

Trading Applications

Get the price at the last crossover point

Reference historical signal values

Build pattern recognition based on past conditions

Create dynamic support/resistance from past events

Related Functions

Frequently Asked Questions

ta.valuewhen(condition, source, occurrence) retrieves source’s value from a matching bar where condition was true, which can be the current bar, selected by occurrence index (0 = latest match).

It selects which historical match to use: 0 is the most recent bar where condition was true, higher numbers walk further back through prior matches.

Yes—Pine notes repainting risk. If condition or source can change on the realtime bar, referenced values may shift. Validate on confirmed bars for consistency.

Generate ta.valuewhen() Code with AI

Draft Pine Script using ta.valuewhen() 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.