Language Reference
Every keyword, namespace function and drawing argument the language defines, grouped in one place.
This page is the index. Each name is spelled the way a script has to spell it, grouped the way you would go looking for it, with the explanation on the page that owns the topic rather than repeated here.
Structure and state. A file opens with the pragma //@version=2 and exactly one declaration, indicator("Name", overlay = true) or strategy("Name", capital = ..., sizing = ..., commission = ..., slippage = ...). Names are bound with = to declare, series to declare a value that carries history, var to declare a value initialised once and carried across bars, and := to reassign an existing name. na(x) tests for a missing value. Comments start with //. History is read with square brackets, close[1]. Conditions are if and else if; ternaries are cond ? a : b; the logical operators are the words and, or and not. Loops are for i = 0 to n with both bounds inclusive, and while cond for the unbounded case. Functions are fn name(type param, ...) with a braced body and return. Blocks are written either indented or braced, consistently within one block.
Bars and built-in series. open, high, low, close, volume, and the derived hl2, hlc3 and ohlc4. Bar metadata is bar.index, bar.time and bar.isLast.
The ta namespace. Averages: ta.sma, ta.ema, ta.wma, ta.rma for Wilder smoothing, ta.hma, ta.vwma. Oscillators: ta.rsi, ta.stoch, ta.cci, ta.mfi, ta.macd. Volatility: ta.atr, ta.stdev, ta.bbands, ta.keltner. Structure: ta.highest, ta.lowest, ta.crossover, ta.crossunder, ta.change, ta.barsSince, ta.valueWhen, ta.pivotHigh, ta.pivotLow. Accumulation and distribution shape: ta.cum for a running total, ta.dev for mean absolute deviation, ta.percentrank for the position of a value in its own recent distribution, plus ta.vwap and ta.obv.
The math and array namespaces. math.abs, math.min, math.max, math.round, math.floor, math.ceil, math.pow, math.sqrt, math.log, math.exp, math.sign, math.clamp and math.sum, all elementwise over series and scalars alike. Arrays are fixed-size numeric buckets for the few studies that need one: array.zeros(n) to allocate, array.get(a, i) to read, array.sum, array.argmax, and array.spread(a, first, last, value) to distribute a quantity across a span of slots, which is how a volume profile fills its rows.
The input namespace. input.int, input.float, input.bool, input.string, input.enum, input.color, input.source, input.timeframe, input.time and input.symbol. Every one takes a default first and a label second. Numeric inputs take minval, maxval and step; input.enum takes options as a list of strings; group.settings("Title") opens a titled section for everything declared after it.
Drawing and alerts. plot(series, "Title", color =, width =, style =, offset =, when =, pane =) is the workhorse, with plot.profile(values, base, step, "Title", ...) for horizontal histograms. hline(level, "Title", color =, style =) draws a static level, fill(a, b, color =, when =) shades between two plotted series, and label.value("Title", value, corner = corner.top_right) prints a live readout. Styles are plot.line, plot.step, plot.dashed, plot.dotted, plot.dots, plot.histogram, plot.area, plot.cross for plots and line.solid, line.dashed, line.dotted for levels. Colours are color.bull, color.bear, color.accent, color.accent_soft, color.fg, color.fg_subtle, with color.fade(c, transparency) and color.new(c, alpha). alert.when(condition, "message") registers an alert, and any input or plot name in braces is interpolated into the message, as in "New {len}-bar high on {symbol}".
Time, higher timeframes and orders. time.within(window), time.crossed(boundary), time.changed(period) for anchor resets, plus time.dayOfWeek, time.hour, time.minute, time.isMonthEnd and time.since. data.request(timeframe, expression, session =) evaluates an expression on another timeframe and returns its last confirmed value. Strategies add strategy.entry(id, side, when =, comment =), strategy.exit(id, stop =, target =, trail =), strategy.close(id) and strategy.close.all(when =), with side.long and side.short, the sizing rules size.percent, size.fixed, size.cash and size.risk, and bps(n) for costs.