User functions

Functions are declared at the top level and are pure. They may call builtins, including plot functions, and other user functions. They may not declare or assign series state, and recursion is rejected before the script runs.

fn midpoint(a: float, b: float) -> float {
  return (a + b) / 2
}

fn stronger(a: float, b: float) -> bool {
  return a > b
}

let middle: float = midpoint(high, low)
plot.line(middle, "#22C55E", 1, "dotted")
  • Every parameter needs a type. At most 16 parameters.
  • An omitted return type means void.
  • A non void function must return a matching value on every path it can take.
  • Function local declarations use let, are scalar, have no history, and are only visible after the line that declares them inside the same block. A local may be reassigned with := or a compound operator; a parameter may not. var is not allowed inside a function.
  • A function may contain loops, and its locals cannot reuse the name of a series, a global or another visible local.
  • Each call site keeps its own series state, so ta.ema(close, 9) and ta.ema(open, 9) never share an accumulator.

Every layer of the market, Orderflow to optionflow, on one chart.

Institutional grade Orderflow data with backtesting and journaling built in. Join the waitlist for early access.

Get Access