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

# The indicator SDK

An indicator is plain TypeScript: Taji has no language of its own. What Taji adds is one module, `@taji/indicators`, the SDK every indicator imports, declared whole in [`taji.d.ts`](https://github.com/TajiAI/Taji/blob/main/agent/skills/taji/references/taji.d.ts) so your agent's editor checks each call before the write:

| Export | What it gives a script |
|---|---|
| `indicator` | The definition: name, pane, inputs, data, plots, `setup` and `next` |
| `input` | Settings the chart's dialog and your agent tune: numbers, choices, sources, anchors, times, prices, text |
| `data` | Other series a script reads: `data.bars`, `data.events`, `data.product` ([Inputs](https://docs.taji.is/indicators/inputs.md)) |
| `plot`, `fill`, `color` | What the script draws per bar, and its look ([Outputs](https://docs.taji.is/indicators/outputs.md)) |
| `ta` | Seven streaming primitives, the built-ins' own Rust compiled to WebAssembly |
| `series` | Comparisons and arithmetic over numbers where `NaN` is the missing value |
| `line`, `label`, `box`, `polyline`, `table`, `card`, `bars` | Drawings and tables the instance holds now |

The kernel holds a script to three rules: a number is a double and `NaN` is the missing value, state is plain data the kernel can copy, and every value comes from the bars. This page explains the SDK and those rules, for when your agent has written [your first indicator](https://docs.taji.is/indicators/first-indicator.md) and you want to know what it can write next. Every field, kind and default is in [the indicator reference](https://docs.taji.is/skill/references/indicators.md).

## How does Taji run your module?

```mermaid
flowchart LR
  src["Your .ts module"] -->|"types stripped"| js["JavaScript in V8"]
  js -->|"once per instance"| setup["setup"]
  setup --> state[("State")]
  bar["Each bar and tick"] --> next["next"]
  state <--> next
  next -->|"a number per plot"| row["Row on the chart"]
```

Read it from the left. Your agent writes one module whose default export is `indicator({...})`. The kernel strips its types and runs the JavaScript left over in V8, inside the kernel itself, so the figures it draws are the kernel's ([0001](https://github.com/TajiAI/Taji/blob/main/docs/decisions/0001-kernel-computes.md)). `setup` runs once per instance and returns the state. `next` runs once per bar, oldest first, and again on every tick of the forming bar; it reads and changes the state and returns one number per plot. Those numbers are the rows the chart draws and your agent reads.

## Why TypeScript?

Taji indicators and plugins are written for coding agents first, so Taji has no language of its own. An indicator is TypeScript and heavy computation is Rust: industry-standard languages whose compilers and type checkers give your agent the best tools there are for safety, performance and correct code. Your agent writes the indicator; you read it, review it and bind it.

The 106 built-in indicators are Rust compiled into the kernel (`substrate/taji-indicators/src/defs.rs`), and a script's seven `ta` primitives are that same Rust compiled to WebAssembly. Computation you want in Rust goes in a [feature plugin](https://docs.taji.is/plugins/feature.md), which serves a product your indicator reads with `data.product(...)` and draws: [dealer gamma](https://docs.taji.is/cookbook/gex-profile.md) and the [order-flow heatmap](https://docs.taji.is/cookbook/orderflow-heatmap.md) are built that way.

TypeScript's types are a contract your agent's editor checks before Taji sees the source. `taji.d.ts` declares `@taji/indicators` whole: point the editor's `paths` at it and type-check before every write.

The kernel never type-checks. It blanks the types with whitespace, so every remaining token keeps its line and column (`engine/crates/taji-script/src/strip.rs`), and a diagnostic or a fault points at the source as written. There is no build step.

The types catch more than misspelt names:

| What your agent writes | What the types say |
|---|---|
| A `source` or `anchor` input read in `setup` | An error: those inputs exist only per bar, in `next` |
| A `choice` input | The union of its options, not `string` |
| A `candle` or `bar` plot | `next` returns `{open, high, low, close}` for it |
| A returned key no plot names, beside one that does | An error |

They do not catch everything. A return whose every key is misspelt, such as `{ avgx: … }` for a plot named `avg`, passes the type check. The kernel ignores a key no plot names, so that plot draws nothing on any bar, and the write's report shows the plot `na` on every fixture bar.

## What does a module hold?

The definition is data and two functions:

| Field | What it is |
|---|---|
| `name`, `pane` | The legend title; `own`, a pane of its own, or `price`, over the bars |
| `inputs`, `data` | The settings a person or an agent tunes, and the other series and events it reads, declared up front ([Inputs](https://docs.taji.is/indicators/inputs.md)) |
| `plots`, `fills`, `max_lines` and the other caps | What it serves per bar, and the caps on its drawings ([Outputs](https://docs.taji.is/indicators/outputs.md)) |
| `setup({inputs})` | Runs once per instance; returns the state |
| `next(bar, s, {inputs, data, ctx, log})` | Runs per bar; returns one number per plot |

Inside `setup` and `next` your agent writes any JavaScript: helper functions, classes, arrays, objects. The limits are on what the state holds and where a value comes from.

## How does an indicator remember the last bar?

A JavaScript number is not a series. There is no series type and no `x[1]` history operator: history is state. This indicator keeps the last few closes in an array and plots the percent change over `length` bars:

```ts file="examples/language/lookback.ts" title="lookback.ts"
import { indicator, input, plot } from "@taji/indicators";

export default indicator({
  name: "Lookback",
  pane: "own",
  inputs: { length: input.int(5, { min: 1 }) },
  plots: { change: plot.line({ title: "Change %" }) },
  setup: () => ({ closes: [] as number[] }),
  next(bar, s, { inputs }) {
    s.closes.push(bar.close);
    if (s.closes.length > inputs.length + 1) s.closes.shift();
    const then = s.closes.length > inputs.length ? s.closes[0] : NaN;
    return { change: (bar.close / then - 1) * 100 };
  },
});
```

![Lookback in its own pane under a daily SPY chart: the percent change over five bars, swinging around zero](https://raw.githubusercontent.com/TajiAI/Taji/main/docs/indicators/examples/language/shot.png)

1. **`setup: () => ({ closes: [] as number[] })`** makes the state: one plain object holding an empty array. Each instance gets its own.
2. **`s.closes.push(bar.close)`** and **`s.closes.shift()`** keep the newest `length + 1` closes. `next` changes the state in place; nothing is returned for it.
3. **`const then = … : NaN`** is the close `length` bars back, or `NaN` while the array is too short. That is the warm-up, and it is your code's: a value you compute is ready when your code says so.
4. **`return { change: (bar.close / then - 1) * 100 }`** needs no check for the warm-up. Arithmetic on `NaN` gives `NaN`, and a plot that gets `NaN` draws nothing on that bar.

The first seven rows of the history show it. With `length` 5, the first five bars, 18 to 22 September 2017, have no close five bars back, so their `change` is missing, `null` in this JSON. The sixth bar, 25 September, is the first with a value:

```json file="examples/language/lookback.json"
[
  {
    "start_dt": "2017-09-18T00:00:00+00:00",
    "change": null
  },
  {
    "start_dt": "2017-09-19T00:00:00+00:00",
    "change": null
  },
  {
    "start_dt": "2017-09-20T00:00:00+00:00",
    "change": null
  },
  {
    "start_dt": "2017-09-21T00:00:00+00:00",
    "change": null
  },
  {
    "start_dt": "2017-09-22T00:00:00+00:00",
    "change": null
  },
  {
    "start_dt": "2017-09-25T00:00:00+00:00",
    "change": -2.00391569733962
  },
  {
    "start_dt": "2017-09-26T00:00:00+00:00",
    "change": -3.251287922152246
  }
]
```

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

A tick runs `next` on the forming bar, so it pushes a close too. That push does not pile up: a close commits the state, and every tick starts again from the committed copy ([Execution](https://docs.taji.is/indicators/execution.md)). The same mechanism covers a single `s.prev = bar.close` and a long array, so no code handles ticks.

## What is a missing value?

Every value a plot serves is a JavaScript number, an IEEE 754 double. The missing value is `NaN`, never `undefined` or `null`. A number plot draws nothing on a bar where `next` returns `NaN`, leaves its key out, or returns anything but a number.

A `ta` primitive answers `NaN` until it has enough bars, so its warm-up needs no code of yours. `bar.time` is the UTC seconds of the bar's open, a number like every other bar field.

`series` carries the helpers a missing value needs:

| Helper | What it does |
|---|---|
| `series.na(x)` | Whether `x` is missing |
| `series.nz(x, y)` | `x`, or `y` (default 0) when `x` is missing |
| `series.fixnan(x, prev)` | `x`, or `prev` when `x` is missing; keep `prev` in state |
| `series.eq`, `ne`, `gt`, `ge`, `lt`, `le` | Compare two numbers, a difference within 1e-10 counting as equal |

Every comparison with `NaN` is false except `ne`. JavaScript's own `==` and `<` stay exact, and so do `ta.crossUp` and `ta.crossDown`: choose the comparison you mean.

## What can state hold?

Plain data: numbers, strings, booleans, arrays and plain objects, plus the handles Taji makes, `ta` primitives and drawings. On a close the kernel copies the state in one walk over the value, and it refuses what it cannot copy faithfully, since a copy that flattened a class instance would lose it silently:

| In state | What happens |
|---|---|
| A function | Throws `state holds a function` |
| An object whose prototype is not `Object.prototype` | Throws `state holds a class instance`; a `Map`, a `Set`, a `Date` and a typed array are refused too |

Classes are fine as code: a class you construct inside `next` and never keep in state works. Every mutable value lives in state.

A `ta` handle is made in `setup` and kept in the state; calling `ta.sma` in `next` throws `ta.sma in setup only`. Each handle keeps its state inside the instance, committed and restored with your state. Feed it on every bar, never under an `if`: a bar it skips is a bar missing from its window.

## What does `ta` give you?

Seven primitives: `sma`, `ema`, `rma`, `rsi`, `change`, `crossUp` and `crossDown`. They are the Rust the built-in indicators run, compiled to WebAssembly (`substrate/taji-indicators-wasm/src/lib.rs`), so a script's `ta.rsi(14)` gives the built-in RSI's numbers for the same bars, bit for bit.

Every other function, such as a weighted average, a standard deviation or a highest high, your agent writes by hand in state from its formula, as the example above does for a change over `length` bars. A script never calls a built-in indicator; it reads one's plot through a `source` input ([Inputs](https://docs.taji.is/indicators/inputs.md)). [The reference](https://docs.taji.is/skill/references/indicators.md#not-built) lists what else is not built.

## How do strings, colours and `log` work?

Strings are JavaScript's, and appear in labels, table cells and `log` lines. `series.tostring(x, format)` prints a number by a pattern (`"#.##"`, `"0.00"`) or by `"mintick"`, `"percent"` or `"volume"`. `series.format(template, ...args)` fills numbered placeholders with numbers, patterns, dates or times, always in UTC. Their exact rounding is in [the reference](https://docs.taji.is/skill/references/indicators.md#the-model).

A colour is a string, `#rrggbb` or `#rrggbbaa`. `color.rgb(r, g, b, transp)` and `color.new(c, transp)` build one from channels or from another colour, with a transparency from 0 to 100. `color.from_gradient` interpolates between two colours by where a value sits in a range, and `color.r`, `g`, `b` and `t` read one back. How a colour reaches a plot is [Outputs](https://docs.taji.is/indicators/outputs.md).

`log(message)` comes in `next`'s third argument. A line is kept only on a close, never on a tick, and the newest 200 are kept; they appear in the write's report beside each plot's statistics.

## Why can't a script read the clock?

A script's values come from its bars, its inputs and its declared data, and from nothing else. A script that read the clock or a random number would compute differently in a replay than it did live. So before any script runs, `Math.random`, `Date.now`, `Date()` and an argument-less `new Date()` are replaced by functions that throw `not deterministic: <name>`. `new Date(ms)` still works: it reads only the number you give it, such as `new Date(bar.time * 1000)`.

Module-level mutable state breaks the same promise, and it is caught rather than refused. Every instance of one definition runs in one V8 context, so a `let` at the top of the module is shared between them and outlives a tick's restore. The write runs the script again from a fresh instance, again over the first half of the bars, and again with ticks before each close; a counter or a carried value at module level fails one of those runs (`engine/crates/taji-script/src/check.rs`). Constants at module level are fine.

## What does the kernel refuse at the write?

| Your agent writes | The kernel answers | Write instead |
|---|---|---|
| `enum`, `namespace`, or a constructor parameter property (`constructor(private x: number)`) | A diagnostic at its line and column: stripping removes type-only syntax, and these need code generated to run | A union of string words; a plain object; an assignment in the constructor |
| A value import from any module but `@taji/indicators` | `import: only @taji/indicators` | Your own code in the one module |
| A default export that is not `indicator({...})` | `default export: indicator({…})` | `export default indicator({...})` |

A type-only import vanishes with the types, so `import { type Line } from "@taji/indicators"` is fine.

## What does a script not get?

- **Asynchronous code.** No promise callback ever runs, so code after an `await` never runs, and an `async` `next` returns a promise, which serves no number.
- **Imports between indicators.** One indicator cannot import another; it reads another's plot through a `source` input.
- **Timers and the network.** `setTimeout` and `fetch` are not defined. `console` exists but prints nowhere: use `log`.
- **`ta` beyond the seven.** Write the rest by hand in state.

## See also

- [Execution](https://docs.taji.is/indicators/execution.md): how the kernel runs a definition, ticks, budgets and faults.
- [Inputs](https://docs.taji.is/indicators/inputs.md) and [Outputs](https://docs.taji.is/indicators/outputs.md): what an indicator reads and what it draws.
- [Writing an indicator](https://docs.taji.is/skill/references/indicators.md): every field, kind, default and limit.
- [Taji indicators and plugins vs Pine Script](https://docs.taji.is/indicators/pine.md): the two languages side by side.
