Operators
21 operators. Assignment, comparison, arithmetic and conditional signs. What each one does, with a working example.
different = close != open plot(different ? 1 : 0, "different?")
Asks whether two values differ.
Gives the remainder of a division. Useful for doing something every N bars: when the remainder is zero you have landed on such a bar.
plot(barIndex % 5, "remainder mod five")
Gives the remainder of a division.
plot(atr(14) * 2, "doubled range")
Multiplies two values.
Multiplies the variable; a short form of `name := name * value`. Used in compounding calculations.
var product = 1.0 product *= 1.01 plot(product, "compound")
Multiplies the variable and writes the result back to it.
Adds two numbers. On series it adds bar by bar: adding two indicators sums their values on each bar. On text it joins the pieces end to end.
plot(high + low, "high plus low")
Adds two values; joins texts end to end.
Adds onto the variable; a short form of `name := name + value`. It reads well when accumulating.
var total = 0.0 total += close plot(total, "accumulation")
Adds to the variable and writes the result back to it.
plot(high - low, "bar range")
Subtracts the right value from the left one.
var remainder = 1000.0 remainder -= 1 plot(remainder, "falling")
Subtracts from the variable and writes the result back to it.
Reaches a field of a record or a member of a constant set. It is how you take the parts of an indicator that returns several values; colour and style constants are read the same way.
b = bb(close, 20, 2.0) plot(b.upper, "upper band")
Reaches a field of a record or a function of an object.
Divides the left value by the right one. ⚠ When the divisor is zero the result is `na`; when computing a ratio, consider the case where the denominator can be zero.
plot((high + low) / 2, "bar midpoint")
Divides the left value by the right one.
var quotient = 1000.0 quotient /= 1.01 plot(quotient, "falling")
Divides the variable and writes the result back to it.
Separates the two halves of a choice that starts with `?`: on its left the value for a true condition, on its right the value for a false one. It is not used on its own.
plot(close > open ? high : low, "tip by direction")
Separates the two branches of a conditional choice.
Changes the value of a variable that already exists. It is the only way to update a variable created with `var`; writing `=` would create a new variable and lose the value carried between bars. Increasing a counter or updating a peak is always done with this.
var counter = 0 counter := counter + 1 plot(counter, "bar count")
Changes the value of a variable that already exists.
Gives true when the left value is smaller than the right one. To accept equality as well, use `<=`.
plot(close < open ? 1 : 0, "down bar")
True when the left value is below the right one.
Gives true when the left value is smaller than or equal to the right one. Write it instead of `<` when touching a threshold should also count.
plot(close <= open ? 1 : 0, "down or equal")
True when it is below or equal — the boundary counts.
Gives a value to a variable and creates it for the first time. It is recomputed on every bar and does not carry the previous bar's value. To keep a value across bars, create it with `var` and change it with `:=`. ⚠ It does not test equality — that is `==`.
length = 20 plot(sma(close, length), "average")
Creates the variable for the first time; it is recomputed on every bar.
Asks whether two values are equal and gives true or false. ⚠ A single `=` assigns while a double `==` asks; mixing them up is a common mistake when writing conditions.
equal = close == open plot(equal ? 1 : 0, "equal?")
Asks whether two values are equal.
Binds an option to its result inside a `switch`. On its left the value to match, on its right the result when it matches. A line with an empty left side runs when no option matched.
direction = close > open ? 1 : -1 text = switch direction { 1 => "up" => "down" } plot(close, "price")
Separates a function's body from its header.
Gives true when the left value is greater than the right one. To accept equality as well, use `>=`.
plot(close > open ? 1 : 0, "up bar")
True when the left value is above the right one.
Gives true when the left value is greater than or equal to the right one. Write it instead of `>` when touching a threshold should also count.
plot(close >= open ? 1 : 0, "up or equal")
True when it is above or equal — the boundary counts.
Picks one of two values depending on a condition. It is shorter than an `if` block for choosing a single value and can sit right inside a calculation. Chaining them builds several steps. Its second half is separated with `:`.
plot(close > open ? high : low, "tip by direction")
Gives the first value when the condition holds, otherwise the second.