Overview
What an indicator is, how it runs, and the two patterns every indicator falls into.
StrategyTune custom indicators are small AssemblyScript programs that run in a WebAssembly sandbox on top of the chart. You write the math; the platform handles the chart, memory, the data feed, and the lifecycle. Indicators are the client-side kind of script — they run in your browser and draw on the chart, while Filters & Signals and Strategy scripts run on StrategyTune's cloud. See Scripts explained for the full picture.
What an indicator is
An indicator has two pieces:
- Metadata — a JSON description of name, inputs, plots, and optional bands/filled areas; it drives the settings dialog and how plots draw.
- Code — one AssemblyScript file with a single entry point,
function calc(ctx: Context): void, writing intoctx.plots.
The execution model
The chart calls your calc(ctx) function once per bar, oldest →
newest. On any recalculation — timeframe change, scrolling history, changing an
input — all WASM state is wiped and calc() is replayed across history from
scratch.
For the latest (live) bar, calc() is called many times as
price ticks come in:
ctx.isNewBar === true— first call for a bar.ctx.isNewBar === false— subsequent live ticks of the same bar.
ctx.plots is reset to NaN at the start of each new bar — not on
every call. If you return on a live tick without setting plots,
the forming bar freezes at a stale value — always assign ctx.plots.<name> on every call (except during warmup, where
leaving NaN means "don't draw").
Two computation patterns
Almost every indicator falls into one of two shapes. Picking the right one matters; the
wrong one either gives wrong values during live ticks or is needlessly slow. Need more
than 200 bars of history? Incremental is the only option. Either way, gate mutations of
committed state on isNewBar.
1. Lookback pattern
Stateless. Each call recomputes the value from the last N bars in ctx.bars.
Use it for: SMA, Bollinger Bands, standard deviation, anything that's a function of the last N closes.
2. Incremental pattern
Stateful. Keep a committed value in module-level variables; advance the committed value
only on ctx.isNewBar; on live ticks, recompute the current value from the
previously committed state plus the live price.
Use it for: EMA, RSI, MACD, ATR, anything where today's value depends on
yesterday's value. Don't iterate ctx.bars to recompute an EMA from
scratch each call — it's wrong (early bars don't see enough history) and slow.
The sandbox
- No DOM, no network, no clock. The only environment is the
Contextobject — indicators are deterministic functions of the bar series and inputs, so replays produce identical results every time. - Memory is GC'd. Allocate temporary arrays and objects freely; you don't free them.
- 200-bar history.
ctx.barsis a ring buffer with at most 200 bars; indicators that need more maintain their own state via the incremental pattern.
Where you write the code
Custom indicators live in the Script Editor inside the app, with a code pane and a
metadata pane. As you type, types tailored to your current metadata
(the exact Inputs and Plots classes you've declared) are
regenerated for autocomplete.
The editor compiles on save: AssemblyScript → WebAssembly. Compilation errors are reported as messages in the editor; line references may point into the generated wrapper rather than your own lines. Once the indicator compiles, it registers with the chart and you add it like any built-in.
The Indicators dialog
The Indicators button in the chart header opens the Indicators window — a floating window that leaves the chart visible, so changes apply in front of you. The left rail filters by source — TradingView built-ins, StrategyTune's curated set, your own, and Community (public sharing still in development). Search spans all of them.
The On chart section at the top lists every indicator instance currently on the chart: click one to edit its parameters in the right-hand column — changes apply to the chart instantly — or remove it. The same indicator can be on the chart more than once; browse rows show a ×N chip counting the instances. The hover + adds an indicator with its defaults. For custom indicators, selecting the row first lets you adjust parameters, then Add to chart; TradingView built-ins add with their defaults and become configurable once on the chart.
Your own indicators are private — only you see them, under My indicators.
Don't want to write code? Your connected AI can write indicators for you.
Something missing or wrong? Email support@strategytune.com.