Skip to content

Writing studies. Your own, on the chart.

Write a study in Pine or JavaScript and it runs on the chart like any other: inputs on its card, its value in the legend, alerts on any plot.

Where scripts live

Open Indicators and pick My scripts. New script opens the editor; Import reads a .pine, .js or .txt file. A saved script is a study like any other: its inputs and colours are on its card, and its value is in the legend.

The editor checks the script as you type. It says how many outputs and inputs it found, or the line it could not read; press that line to go to it. Save keeps it in this browser. Add to chart saves it and puts it on the chart.

A script that stops answering for 20 seconds is stopped, and its card says so.

Pine

Paste a Pine v5 or v6 indicator. It is translated to the same engine the JS scripts use. What the translator does not support is named with its line.

A JS script

JavaScript
({  meta: {    id: "my-ema", title: "My EMA", overlay: true, renderKind: "plot",    provenance: { source: "js" },    inputs: [{ id: "len", label: "Length", type: "int", default: 20, min: 1 }],    plots: [{ id: "ema", title: "EMA", kind: "line", defaultColor: "#4DA3FF" }],  },  onBar(ctx) {    ctx.plot("ema", ctx.ta.ema(ctx.close.get(0), ctx.input.get("len")));  },})
  • meta.id and meta.title: a short id and the name on the card.
  • meta.overlay: true draws on the price chart, false gives the script its own pane.
  • meta.renderKind: "plot" for lines and columns, "shape" for marks.
  • meta.provenance: { source: "js" }.

Inputs and outputs

Each input is a row on the card. Types: int and float (default, min, max, step), bool, enum (options), source (open, high, low, close, hl2, hlc3, ohlc4). Read one with ctx.input.get("id").

Each plot in meta.plots is one output: kind "line", "hist" (columns), "shape" (marks) or "band", with a title and a defaultColor. Write to it by its id:

  • ctx.plot(id, value): this bar's value for a line or hist.
  • ctx.plotShape(id, condition, opts): a mark on this bar when condition is true. opts.shape: circle, triangleup, triangledown, arrowup, arrowdown or cross; opts.location: abovebar, belowbar or atprice with opts.atPrice; opts.color, opts.size, opts.label.
  • ctx.hline(id, price), ctx.bgcolor(color), ctx.fill(id) for levels, a bar's background and the area of a band.
  • ctx.warn(text): a note on the card.

Reading bars

onBar(ctx) runs once for every bar, oldest first, then again as the last bar forms. ctx.close.get(0) is this bar, get(1) the one before; open, high, low, volume, hl2, hlc3, ohlc4 and time read the same way. ctx.i is the bar's index and ctx.bar says isFirst, isLast, isConfirmed and isSessionStart.

ctx.ta has the usual tools, each a running value for this bar: ema, sma, rma, wma, vwma, hma, rsi, atr, stdev, macd, bb, kc, vwap, highest, lowest, change, roc, crossover, crossunder, stoch, supertrend and more. ctx.varSlot(key, start) keeps a value from bar to bar.

The options bar: ctx.opt

Add optionsBar: true to meta and ctx.opt reads the chart's options bar: gex(), dex(), vex() and chex(), the net gamma, delta, vanna and charm exposure over the chain's expiries, as the bar prints them. They are the figures as they stand now, never a history: on an older bar they read today's, so use them on the newest bar. On a chart with no options chain, and before the first figures arrive, they read NaN.

JavaScript
({  meta: {    id: "gex-below-zero", title: "Net GEX below 0", overlay: false, renderKind: "plot",    optionsBar: true,    provenance: { source: "js" },    inputs: [],    plots: [{ id: "neg", title: "Below 0", kind: "hist", defaultColor: "#EF5350" }],  },  onBar(ctx) {    const gex = ctx.opt.gex();    if (ctx.bar.isLast && Number.isFinite(gex)) ctx.plot("neg", gex < 0 ? 1 : 0);    else ctx.plot("neg", NaN);  },})

Alerts from a study

An alert can watch any plot of a script. Open Alerts, pick Indicator, then the script and its plot. A line or column is read on the newest bar. A mark plot reads as the number of its marks on the newest bar, 0 with none, so Crosses above 0.5 fires when a mark lands.

For an event, give it a column of its own that writes 1 on the bar it happens and 0 otherwise, and set the alert to cross above 0.5.