Bentrix Docs
HomePricingDashboard

The language

Values and series, the history operator, var and varip, operators, if, switch and loops, functions and methods, tuples, types and enums.

A script runs once per bar

The whole script is run on every bar of the chart, from the oldest to the newest; while the newest bar forms, it is run again at each tick. Everything else follows from this. A variable assigned a value is a new value on each bar, and the collection of those values, bar by bar, is a series.

range = high - low          // a value on each bar: a series
avg = ta.sma(range, 20)     // the average of the last 20 of those values

01Types

TypeHoldsExample
inta whole number14
floata number1.0825
booltrue or falseclose > open
colora colourcolor.new(color.blue, 50)
stringtext"Fast"
array<T>, matrix<T>, map<K, V>collectionsarray.new<float>(10)
line, label, box, table, linefill, polylinedrawingsline.new(…)

A value also has a qualifier that says when it is known: const (when the script is written), input (when its settings are set), simple (on the first bar), series (on each bar). A built-in that needs a length known in advance takes a simple int; passing a series where a simple value is required is an error the checker names.

02Absent values: na

A value can be absent — an average before it has enough bars, a high of a bar that does not exist. na is that value; na(x) asks whether x is absent, nz(x) replaces an absent value with zero (or with what you give), and fixnan(x) carries the last present value forward. Arithmetic with na gives na.

03History: [ ]

x[1] is the value x had on the bar before; x[n], n bars ago. It works on any series — a built-in, a variable, an expression in parentheses:

change = close - close[1]
higherHigh = high > high[1] and high[1] > high[2]

Reading further back than the chart's first bar gives na.

04var and varip

An ordinary variable is computed afresh on each bar. Declared with var, it is set once, on the first bar, and keeps its value from bar to bar until the script changes it — the way to count, accumulate or remember:

var int ups = 0
if close > open
    ups += 1

On the newest bar, which the script runs again at each tick, a var variable is put back to its value at the bar's open before each run, so a forming bar is counted once. varip ("intrabar persist") keeps its changes across those ticks too — on history, where each bar is run once, varip and var are the same.

05Operators

+ - * / % arithmetic; == != < > <= >= comparison; and, or, not; ? : the conditional (cond ? a : b); := reassigns a variable declared earlier (= declares); += -= *= /= %= update it.

06Control flow

if close > open
    col := color.green
else if close < open
    col := color.red
else
    col := color.gray

kind = switch
    close > high[1] => "breakout"
    close < low[1]  => "breakdown"
    => "inside"

total = 0.0
for i = 0 to 9
    total += volume[i]

i = 0
while i < 10 and not na(close[i])
    i += 1

if and switch are expressions as well as statements: their last value is their result. for … to … by …, for x in array and while loop within one bar; break and continue work in loops. Indentation (four spaces or a tab) marks a block.

07Functions

pctChange(src, len) =>
    (src - src[len]) / src[len] * 100

roc10 = pctChange(close, 10)

A function is declared with =>; a multi-line body is indented, and its last line is its result. A function that returns several values returns a tuple, and the caller unpacks it:

band(src, len, mult) =>
    mid = ta.sma(src, len)
    dev = mult * ta.stdev(src, len)
    [mid, mid + dev, mid - dev]

[m, up, dn] = band(close, 20, 2.0)

Each call site of a function keeps its own history: a ta.ema inside a function called twice is two averages.

08Types, methods and enums

A script can declare its own types, functions attached to them, and enums (version 6):

type Swing
    float price
    int   bar

method label(Swing s) =>
    str.tostring(s.price)

enum Side
    long  = "Long"
    short = "Short"

side = input.enum(Side.long, "Side")

09Names

Built-ins are grouped into namespaces — ta. for technical analysis, math., str., array., request. and the rest; the reference lists every one. A name you declare yourself always wins over a built-in name of the same spelling.

Bentrix 0.1.382Every number, name and default on these pages is read from the application itself.What is comingWhat changed