feature

First study guide

This walkthrough builds a small study that counts definition events and publishes a simple table of results.

Use it after you have Collect data for a definition. Studies answer “what usually happens next?” — they do not mark new setups on the chart.

What you need first

Steps

  1. Open the definition → create or open a study.

  2. Paste the script below. Change slug to your definition’s slug.

  3. Attach datasets and run on an instrument / range / timeframe.

  4. Open results. For chart pins, see Show studies on charts — that is a separate authoring step.

Full script

meta({ name: "Bull bar count", kind: "study" });

flows.declare({
  slug: "bull-bar",
  as: "bull",
});

results.declare({
  id: "summary",
  kind: "metric",
  title: "Event count",
});

results.declare({
  id: "rows",
  kind: "table",
  title: "Events",
});

export function onEvent(ctx) {
  if (ctx.event.flow !== "bull") return;
  ctx.collect("n", 1);
  ctx.collect("rows", {
    time: ctx.event.time,
    range: ctx.event.payload.range,
  });
}

export function onFinish(ctx) {
  const n = ctx.collected("n").length;
  ctx.publish("summary", { value: n, label: "Events" });
  ctx.publish("rows", ctx.collected("rows"));
}

Important distinctions

  • Studies consume flow events. They do not call ctx.emit.

  • ctx.publish belongs in onFinish (after aggregation). Use ctx.collect inside onEvent.

  • Showing results on the Terminal uses Put on chart (or the pin orchestrator) — not the study authoring agent. See Show studies on charts.

Next

  1. Study declarations

  2. Collect and publish

  3. Show studies on charts