ta.valuewhen()Value When
Returns the value of the source series on the bar where the condition was true.
Syntax
ta.valuewhen(condition, source, occurrence) → series float / int / bool / colorArguments
| Parameter | Type | Description |
|---|---|---|
| condition | series bool | Condition that marks relevant bars. |
| source | series int/float/bool/color | Series value to recall when condition is true. |
| occurrence | simple int | Nonnegative 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
//@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
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.
Related Pine Script Functions
ta.barssince() - Bars Since
Learn how to use ta.barssince() in Pine Script. Count bars since a condition was last 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.