Skip to content

Real-time Behavior

This section explains how indicators update in real time to help you write correct code.

Bar Lifecycle

A bar progresses through several states:

Historical bars:     Bar 1 → Bar 2 → Bar 3 → Bar 4 → Bar 5
                     (confirmed, one tick each)

Real-time bar:       Bar 6

                     tick 1 (bar.isNew() = true)

                     tick 2 (market data update)

                     tick 3 (market data update)

                     tick 4 (bar.isConfirmed() = true)

Historical bars — processed once, already confirmed.

Real-time bar — receives multiple updates until confirmed. Each update calls onTick() again.

Built-in Functions

Functions from ts() and ta.* modules automatically reset on each tick. The current value [0] starts fresh, while historical values [1], [2], etc. remain stable:

javascript
function onTick() {
    const sma = ta.sma(close, 20);  // recalculated each tick
    const trend = ts();

    trend.set(close > sma ? 1 : -1);  // [0] set fresh each tick

    // trend[1] — previous bar, stable
    // trend[2] — two bars ago, stable

    spline(trend, {title: "Trend"});
}
Bar 6 tick 1: trend[0] = 1,  trend[1] = -1
Bar 6 tick 2: trend[0] = 1,  trend[1] = -1  ← [1] unchanged
Bar 6 tick 3: trend[0] = -1, trend[1] = -1  ← [0] recalculated

Regular JS Variables

Regular JS variables in the init section persist across onTick() calls:

javascript
// INIT
let tickCount = 0;

function onTick() {
    tickCount++;
    spline(tickCount, {title: "Ticks"});
}
Bar 6 tick 1: tickCount = 1
Bar 6 tick 2: tickCount = 2
Bar 6 tick 3: tickCount = 3

Historical vs Real-time

Check processing mode with bar.isHistory():

javascript
function onTick() {
    const sma = ta.sma(close, 20);
    const lineColor = bar.isHistory() ? color.GRAY : color.BLUE;
    spline(sma, {title: "SMA", color: lineColor});
}
Modebar.isNew()bar.isConfirmed()Updates per bar
Historicaltruetrue1
Real-timetrue on first ticktrue on last tickmultiple

Summary

TypeBehavior on bar updates
ts(), ta.*Reset automatically, [0] recalculated
JS variablesPersist, accumulate across ticks
bar.isNew()true only on first tick of bar
bar.isConfirmed()true only on final tick