Skip to main content

indicator.onBar#

Per-bar entry point for custom indicators (modern model).

SYNTAX#

function onBar(i: number, ctx: OnBarCtx): void

ARGUMENTS#

  • i (number) — Zero-based index of the current candle within the candles array. Read candles[i] for the current bar.
  • ctx (OnBarCtx) — Per-bar context: { state, settings, isFirst, isLast, isHistory, isRealtime, symbol, exchange, timeframe, tickSize, stepSize }.

RETURNS#

void — emit results via plot(...), plot.histogram(...), plot.marker(...), plot.candle(...), or entity.<kind>(...) instead of returning.

REMARKS#

onBar cannot call source(...) — data dependencies must be declared at top-level so the host can pre-fetch before the warmup loop. The runtime throws if source() is invoked inside onBar. When both onBar and compute are exported, onBar wins.

EXAMPLE#

// @indicator { name: "EMA-20", overlay: true }
function getSettings() {
  return [{ key: 'length', type: 'number', default: 20, min: 1, max: 200, label: 'Length' }];
}
 
const closes = [];
function onBar(i, ctx) {
  closes[i] = candles[i].close;
  const v = ta.ema(closes, ctx.settings.length, i);
  if (v !== null) plot('ema', v, { color: '#00ff88', width: 2 });
}

SEE ALSO#

indicator.compute, indicator.plot, indicator.getSettings, data.source