feature
onBar
onBar is the function Chartnaut calls once per bar for an indicator — this is where you read price, update memory, and plot values.
Bar fields, accum, drawings, MTF, and deps are documented in Shared runtime; this page is the checklist for wiring them inside an indicator’s onBar.
Signature
function onBar(ctx: ScriptedCtx): voidChecklist
Read
ctx.paramsfrom inputs.Keep state in
ctx.accum.Emit with
ctx.plot/ mark / … to declaredoutput.*.Optional:
security/tf,session.isStart,ctx.indicators.
Example
meta({ shortName: "EMA Cross", kind: "overlay" });
const length = input.number({ id: "length", label: "Length", default: 20, min: 1 });
const emaLine = output.line({ id: "ema", color: "#2196f3" });
output.markers({ id: "cross_up", on: emaLine, color: "#26a69a" });
layout(tab({ id: "main", title: "Main" }, section({ id: "p", title: "Parameters" }, length)));
function onBar(ctx) {
const k = 2 / (ctx.params.length + 1);
const ema = ctx.accum("ema", null, (prev) =>
prev == null ? ctx.close : prev + k * (ctx.close - prev)
);
ctx.plot("ema", ema);
const prevClose = ctx.accum("prevClose", null, () => ctx.close);
const prevEma = ctx.accum("prevEma", null, () => ema);
if (prevClose != null && prevEma != null && prevClose <= prevEma && ctx.close > ema) {
ctx.mark("cross_up", { price: ctx.close, text: "↑", time: ctx.time });
}
}Not a study
No onEvent, no ctx.emit. For collectable setups use a definition.
