Appearance
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] recalculatedRegular 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 = 3Historical 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});
}| Mode | bar.isNew() | bar.isConfirmed() | Updates per bar |
|---|---|---|---|
| Historical | true | true | 1 |
| Real-time | true on first tick | true on last tick | multiple |
Summary
| Type | Behavior on bar updates |
|---|---|
ts(), ta.* | Reset automatically, [0] recalculated |
| JS variables | Persist, accumulate across ticks |
bar.isNew() | true only on first tick of bar |
bar.isConfirmed() | true only on final tick |