> Every Taji docs page as Markdown: https://docs.taji.is/llms.txt

# Write, bind and test your first indicator

You will have an indicator called Pulse drawn under a chart, checked and tested by the kernel, and see the kernel refuse a version that calls `Math.random`. Your agent writes scripts like Pulse and makes these same calls over MCP; here you make them yourself on a headless kernel, so you see what it sees.

You need Taji built from source ([Build from source](https://docs.taji.is/guide/install.md)), `jq`, the network (the keyless kernel reads Yahoo's delayed bars), and Taji's Python, which fetches those bars and decodes the answer:

```sh
bash engine/crates/taji/scripts/python.sh .python
cargo build -p taji-open
```

Every command runs from the repository root, in one shell.

## 1. Boot a kernel

`boot` starts a headless kernel on throwaway state; `TAJI_WIRE=0` takes a free port. `get` and `v` call the wire, which authenticates every request by `x-taji-token`. The kernel opens a desk with a daily chart of `equity:SPY`, and `DESK` names that desk.

```sh
W=$(mktemp -d)
boot() {
  mkdir -p "$W/state" "$W/store" "$W/home"
  export TAJI_NO_UPDATE_CHECK=1 TAJI_NO_REPORT=1 TAJI_STATE_DIR=$W/state TAJI_STORE_DIR=$W/store TAJI_HOME=$W/home
  TAJI_HEADLESS=1 TAJI_WIRE=0 target/debug/taji > "$W/boot.log" 2>&1 &
  until [ -s "$W/state/wire-token" ] && curl -sf -o /dev/null -H "x-taji-token: $(cat "$W/state/wire-token")" "localhost:$(cat "$W/state/wire-port")/"; do sleep 1; done
  PORT=$(cat "$W/state/wire-port"); TOKEN=$(cat "$W/state/wire-token")
}
get() { curl -s -H "x-taji-token: $TOKEN" "localhost:$PORT/$1"; echo; }
v() { curl -s -H "x-taji-token: $TOKEN" -H 'content-type: application/json' -X POST "localhost:$PORT/verb/$1" --data-binary "$2"; echo; }
boot
DESK=$(get "" | python3 -c 'import json,sys; print(json.load(sys.stdin)["active"])')
```

- A kernel runs on its own state, store and home under `$W`: nothing touches your own Taji.

## 2. Write the script

Pulse plots each bar's percent change from the bar before, and an exponential average of that change:

```ts file="examples/first-indicator/pulse.ts" title="pulse.ts"
import { indicator, input, plot, ta } from "@taji/indicators";

export default indicator({
  name: "Pulse",
  pane: "own",
  inputs: { length: input.int(9, { min: 2 }) },
  plots: { change: plot.line({ title: "Change %" }), avg: plot.line({ title: "Average" }) },
  setup: ({ inputs }) => ({ prev: NaN, avg: ta.ema(inputs.length) }),
  next(bar, s) {
    const change = (bar.close / s.prev - 1) * 100;
    s.prev = bar.close;
    return { change, avg: s.avg.next(change) };
  },
});
```

1. **`pane: "own"`** draws Pulse in a pane under the chart, not over the price.
2. **`inputs`** declares `length`, a whole number from 2 up, 9 by default.
3. **`setup`** returns the state: the previous close, which the script keeps itself because a number has no history, and a `ta` handle, which only `setup` may create.
4. **`next`** runs once per bar and returns one number per plot.

Copy it beside the kernel's state:

```sh
cp docs/indicators/examples/first-indicator/pulse.ts "$W/pulse.ts"
```

## 3. Have the kernel check it

`write` sends the source (and, later, a test) to `indicators/write`, and the kernel answers its check report:

```sh
write() { v desk/$DESK/indicators/write "$(jq -n --rawfile source "$1" --rawfile test "${2:-/dev/null}" '{name: "pulse", source: $source} + (if $test == "" then {} else {test: $test} end)')"; }
write "$W/pulse.ts" | jq .
```

You see the report:

```json file="examples/first-indicator-check/check.json"
{
  "definition": "pulse@1",
  "report": {
    "bars": 1024,
    "drawings": {
      "bar_categories": 0,
      "boxes": 0,
      "cards": 0,
      "labels": 0,
      "lines": 0,
      "polylines": 0,
      "tables": 0
    },
    "fault": null,
    "invariants": [
      {
        "detail": null,
        "name": "determinism",
        "ok": true
      },
      {
        "detail": null,
        "name": "prefix",
        "ok": true
      },
      {
        "detail": null,
        "name": "ticks_then_close",
        "ok": true
      }
    ],
    "logs": [],
    "ms": 1.878417,
    "plots": [
      {
        "first": 1705655700,
        "last": -0.2274607113316729,
        "max": 0.4196255648805769,
        "min": -0.4309880400818922,
        "na": 1,
        "name": "change"
      },
      {
        "first": 1705662900,
        "last": -0.03842140794957199,
        "max": 0.21412616231653991,
        "min": -0.24276065717267192,
        "na": 9,
        "name": "avg"
      }
    ],
    "tests": []
  }
}
```

*Captured 2026-12-03 on a keyless headless kernel. The check runs on the kernel's own fixture bars, so your report matches but for `ms`.*

- The kernel compiled the source, ran it over a fixed fixture of 1,024 synthetic 15-minute bars (not your chart's), and stored it as `pulse@1`: `definition` names it.
- `fault` is `null`, and each of the three invariants, `determinism`, `prefix` and `ticks_then_close`, holds.
- Each plot reports its na count, the open time of its first value, its last value and its range: `change` is na on the first bar, which has no previous close, and `avg` stays na until it has seen nine changes.

## 4. Bind it to the chart

Pane 1 is the first chart. The answer's `node` ends in the binding's id:

```sh
v desk/$DESK/chart/1/add_indicator '{"pane_id":1,"definition":"pulse","params":{}}' | tee "$W/bound.json"
BINDING=$(jq -r '.node | split("/") | last' "$W/bound.json")
```

The window, were it open, now draws Pulse in its own pane under SPY:

![Pulse in its own pane under a daily SPY chart: the percent change and its 9-bar average](https://raw.githubusercontent.com/TajiAI/Taji/main/docs/indicators/examples/first-indicator/shot.png)

- The definition `pulse` follows the latest version, and `params: {}` takes the defaults, so the legend reads `Pulse 9`.

## 5. Read its rows

`GET /get/indicators` answers Arrow IPC with no JSON form; the kernel serves the window the same rows. `rows.py` prints the newest ten as JSON, or, given `definition`, the version that drew them. Yahoo's bars load after boot, so `rows` asks again until some arrive:

```sh
cat > "$W/rows.py" <<'PY'
import json, sys, pyarrow as pa

t = pa.ipc.open_stream(sys.stdin.buffer).read_all()
if t.num_rows == 0:
    sys.exit(1)
if sys.argv[1:] == ["definition"]:
    print(t.schema.metadata[b"definition"].decode())
else:
    rows = t.slice(max(0, t.num_rows - 10)).to_pylist()
    print(json.dumps(rows, indent=2, default=lambda v: v.isoformat()))
PY
rows() {
  until curl -s -H "x-taji-token: $TOKEN" "localhost:$PORT/get/indicators?chart=desk/$DESK/chart/1&binding=$BINDING" | .python/python "$W/rows.py" "$@" 2>/dev/null; do sleep 1; done
}
rows
```

You see the newest ten rows; yours differ with the market:

```json file="examples/first-indicator/pulse.json"
[
  {
    "start_dt": "2026-11-19T00:00:00+00:00",
    "change": 1.7250510705909106,
    "avg": 0.44362014179233367
  },
  {
    "start_dt": "2026-11-20T00:00:00+00:00",
    "change": 0.6842692450725218,
    "avg": 0.4917499624483713
  },
  {
    "start_dt": "2026-11-23T00:00:00+00:00",
    "change": 1.0268153948437497,
    "avg": 0.598763048927447
  },
  {
    "start_dt": "2026-11-24T00:00:00+00:00",
    "change": 0.4094764551038299,
    "avg": 0.5609057301627236
  },
  {
    "start_dt": "2026-11-25T00:00:00+00:00",
    "change": 1.6093795514127596,
    "avg": 0.7706004944127308
  },
  {
    "start_dt": "2026-11-27T00:00:00+00:00",
    "change": -0.01433383501756591,
    "avg": 0.6136136285266715
  },
  {
    "start_dt": "2026-11-30T00:00:00+00:00",
    "change": 0.30105368790769305,
    "avg": 0.5511016404028758
  },
  {
    "start_dt": "2026-12-01T00:00:00+00:00",
    "change": 0.35017508754375815,
    "avg": 0.5109163298310523
  },
  {
    "start_dt": "2026-12-02T00:00:00+00:00",
    "change": 0.5483549351944284,
    "avg": 0.5184040509037275
  },
  {
    "start_dt": "2026-12-03T00:00:00+00:00",
    "change": 1.0128196047878735,
    "avg": 0.6172871616805568
  }
]
```

*Captured 2026-12-04 on a headless kernel reading a made-up daily series for SPY.*

- Each row is keyed by the bar's open time, `start_dt`, with one column per plot. The schema's metadata carries the definition, and `fault` too when the instance has one.

## 6. Write version 2 with a test

A test is a module whose default export maps names to `(t) => void`. `t.run(params, closes)` runs the indicator over bars made from `closes`, and `t.near` and `t.assert` check the result:

```ts file="examples/first-indicator-check/pulse.test.ts" title="pulse.test.ts"
import type { T } from "@taji/indicators";

export default {
  "a flat close does not move"(t: T) {
    const r = t.run({ length: 3 }, Array(12).fill(100));
    t.assert(Number.isNaN(r.change[0]), "the first bar has no previous close");
    t.near(r.change[r.change.length - 1], 0, 1e-12, "no change");
  },
  "a 1% rise reads 1"(t: T) {
    const r = t.run({ length: 3 }, [100, 101]);
    t.near(r.change[1], 1, 1e-9, "one percent");
  },
};
```

Copy it beside the source, write both, and ask the binding which version it draws:

```sh
cp docs/indicators/examples/first-indicator-check/pulse.test.ts "$W/pulse.test.ts"
write "$W/pulse.ts" "$W/pulse.test.ts" | jq .
rows definition
```

You see the report, now with its tests, and then `pulse@2`:

```json file="examples/first-indicator-check/tested.json"
{
  "definition": "pulse@2",
  "report": {
    "bars": 1024,
    "drawings": {
      "bar_categories": 0,
      "boxes": 0,
      "cards": 0,
      "labels": 0,
      "lines": 0,
      "polylines": 0,
      "tables": 0
    },
    "fault": null,
    "invariants": [
      {
        "detail": null,
        "name": "determinism",
        "ok": true
      },
      {
        "detail": null,
        "name": "prefix",
        "ok": true
      },
      {
        "detail": null,
        "name": "ticks_then_close",
        "ok": true
      }
    ],
    "logs": [],
    "ms": 0.9577089999999999,
    "plots": [
      {
        "first": 1705655700,
        "last": -0.2274607113316729,
        "max": 0.4196255648805769,
        "min": -0.4309880400818922,
        "na": 1,
        "name": "change"
      },
      {
        "first": 1705662900,
        "last": -0.03842140794957199,
        "max": 0.21412616231653991,
        "min": -0.24276065717267192,
        "na": 9,
        "name": "avg"
      }
    ],
    "tests": [
      {
        "message": null,
        "name": "a flat close does not move",
        "ok": true
      },
      {
        "message": null,
        "name": "a 1% rise reads 1",
        "ok": true
      }
    ]
  }
}
```

- `definition` reads `pulse@2`, and each test reports `ok: true` with a `null` message. The source did not change, but source and test together did, so this is the next immutable version.
- `rows definition` prints `pulse@2`: the binding named `pulse` followed the latest version without a change.

## 7. Cause a fault

Make the previous close a little random, and write it:

```sh
sed 's|s.prev = bar.close;|s.prev = bar.close * (1 + Math.random() * 1e-6);|' "$W/pulse.ts" > "$W/noisy.ts"
write "$W/noisy.ts" "$W/pulse.test.ts" | jq -c '{error, reason}'
```

- The kernel refuses with status 422: `error` is `check`, and `reason` says the script threw `not deterministic: Math.random` at the first fixture bar's time.
- The kernel makes `Math.random` throw, so the history run faults and no invariant runs. The refusal's `report` carries the same `fault`. Nothing was stored: `rows definition` still prints `pulse@2`.

## 8. Fix it

Write the unchanged source again, then list what the desk holds:

```sh
write "$W/pulse.ts" "$W/pulse.test.ts" | jq -c '{definition}'
find "$W/state/desks" -path "*indicators*" -type f | sed "s|.*/indicators/|indicators/|"
```

- `definition` reads `pulse@2`: the source equals the latest version, so the kernel answers that version and stores nothing.
- The listing holds `pulse/1.ts`, `pulse/2.ts` and `pulse/2.test.ts`: only what passed.

Stop the kernel and remove the throwaway state:

```sh
v ui/quit '{}'
wait
rm -rf "$W"
```

## If something goes wrong

- **`boot` never returns.** The kernel did not start: read `$W/boot.log`, and check that `target/debug/taji` exists: `cargo build -p taji-open` above builds it.
- **`rows` never returns.** The chart has no bars: the keyless kernel reads them from Yahoo, so it needs the network.
- **`write` prints `"error":"check"`.** The kernel refused the source and stored nothing: `reason` says why, and `.report.fault` where. Fix the source and write it again.

## Next

- [The indicator SDK](https://docs.taji.is/indicators/sdk.md): what a script may contain and what it may not.
- [How an indicator executes](https://docs.taji.is/indicators/execution.md): why a tick never changes a closed bar, and what a fault does.
- [Inputs](https://docs.taji.is/indicators/inputs.md): `length` as a setting, and the bars and data a script reads.
- [Outputs](https://docs.taji.is/indicators/outputs.md): plots, colour, drawings and what the window draws.
