Historical

This section documents the hitorical (ta.*) functions.


ta.barssince

Returns the number of bars since a specified condition was true for the nth occurrence.

Syntax:

ta.barssince(condition)
ta.barssince(condition, occurrence)

Parameters:

  • condition: Boolean expression to evaluate

  • occurrence: (Optional) Which occurrence to find (0 = most recent, 1 = second most recent, etc.). Defaults to 0.

Examples:

// Bars since last green candle (most recent occurrence)
ta.barssince($BTC.close >= $BTC.open)

// Bars since second-to-last green candle
ta.barssince($BTC.close >= $BTC.open, 1)

// Bars since RSI was overbought (most recent)
ta.barssince(ta.rsi($BTC, 14) > 70)

// Bars since RSI was overbought (previous occurrence)
ta.barssince(ta.rsi($BTC, 14) > 70, 1)

// Bars since price crossed above moving average
ta.barssince(ta.crossover($BTC.close, ta.sma($BTC, 20)))

// Bars since previous confirmed pivot high
ta.barssince(ta.pivothigh($BTC, 5, 5) !== null, 0)

// Bars since volume spike
ta.barssince($BTC.volume > ta.sma($BTC.volume, 20) * 2)

// Bars since MACD signal
ta.barssince(ta.crossover(ta.macd($BTC, 12, 26, 9).macd, ta.macd($BTC, 12, 26, 9).signal))

Returns: Number of bars since condition was true at the specified occurrence (null if never true or occurrence not found)

Use Cases:

  • Time-based filters for signals

  • Avoiding repeated signals too close together

  • Measuring duration since key events

  • Exit timing based on time elapsed


ta.valuewhen

Returns the value of a source series when a specified condition was true for the nth occurrence.

Syntax:

Parameters:

  • condition: Boolean expression or series that determines when to capture the source value

  • source: The data series from which to retrieve values

  • occurrence: (Optional) Which occurrence to return (0 = most recent, 1 = second most recent, etc.). Defaults to 0.

Examples:

Returns:

  • Value of source when condition was true for the specified occurrence

  • undefined if the specified occurrence was not found or insufficient data

Advanced Examples:

Use Cases:

  • Reference levels from previous signals or events

  • Track entry prices for position management

  • Compare current conditions to historical occurrences

  • Set dynamic stop losses based on previous swing points

  • Analyze pattern consistency by comparing volumes or prices

  • Create conditional logic based on historical trigger levels

Last updated

Was this helpful?