Indicators

Script API reference

Everything a TradingCurve script can use, version 1.

A script is JavaScript that calls defineIndicator exactly once. It can only use these names: defineIndicator, input, output, ta and Math.

defineIndicator

defineIndicator({
  name: 'My indicator',      // required, up to 40 characters
  shortName: 'MI',           // the short name in the legend
  description: '...',        // optional
  placement: 'price',        // 'price' over the candles, 'pane' in its own pane
  inputs: [ ... ],           // values the user can change
  outputs: [ ... ],          // what is drawn (at least one)
  levels: [30, 70],          // fixed horizontal lines (panes)
  range: { min: 0, max: 100 }, // fixed pane scale (optional)
  precision: 2,              // decimals (optional)
  calc(c, i) { ... }         // the math
});

Inputs (input)

FunctionWhat it creates
input.number(key, label, default, { min, max, step })A number
input.bool(key, label, default)A yes or no
input.color(key, label, default)A color
input.select(key, label, default, options)A list of options

The key is the name you read it with in calc: i.fast, i.slow...

Outputs (output)

FunctionWhat it draws
output.line(key, label, color, width)A line
output.histogram(key, label, color)Bars from zero

Colors: 'series1' to 'series6', 'accent', 'up', 'down', 'neutral' (they adapt to the light or dark theme) or a fixed color such as '#ff8800'.

The math: calc(c, i)

  • c are the candles, in columns: c.open, c.high, c.low, c.close, c.volume, c.time and c.length.
  • i are the values of your inputs.
  • Return an object with one array per output, as long as the candles: { fast: [...], slow: [...] }. Use null where there is no value.

ta functions

They all take a series (for example c.close) and return a series of the same length.

FunctionWhat it calculates
ta.sma(src, len)Simple moving average
ta.ema(src, len)Exponential moving average
ta.rma(src, len)Wilder's moving average
ta.wma(src, len)Weighted moving average
ta.change(src, len = 1)Difference from the value len candles ago
ta.highest(src, len)Highest of the last len candles
ta.lowest(src, len)Lowest of the last len candles
ta.stdev(src, len)Standard deviation
ta.rsi(src, len = 14)RSI
ta.atr(c, len = 14)ATR (takes the whole candles)
ta.crossover(a, b)1 where a crosses above b, 0 elsewhere
ta.crossunder(a, b)1 where a crosses below b, 0 elsewhere

Example: RSI with levels

defineIndicator({
  name: 'My RSI',
  placement: 'pane',
  inputs: [input.number('len', 'Length', 14, { min: 2, max: 200 })],
  outputs: [output.line('rsi', 'RSI', 'series3', 2)],
  levels: [30, 70],
  range: { min: 0, max: 100 },
  calc(c, i) {
    return { rsi: ta.rsi(c.close, i.len) };
  }
});

Example: momentum histogram

defineIndicator({
  name: 'Momentum',
  placement: 'pane',
  inputs: [input.number('len', 'Length', 10)],
  outputs: [output.histogram('mom', 'Momentum', 'accent')],
  calc(c, i) {
    return { mom: ta.change(c.close, i.len) };
  }
});

What is not allowed

For safety, a script cannot use eval, Function, constructor, prototype, window, self, globalThis, fetch, XMLHttpRequest, WebSocket or Worker. It has 3 seconds to load and 4 for each calculation.