> ## Documentation Index
> Fetch the complete documentation index at: https://gocharting.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Operators and expressions

> The operators with their exact precedence, logic with na, the conditional expression, the history operator x[n] and string joining.

An expression combines values with operators. Use parentheses whenever two operators of different kinds meet.

## Precedence

| Level | Operators |
| - | - |
| 1 | unary `-`, unary `+`, `not` or `!` |
| 2 | `*` `/` `%`, left to right |
| 3 | `+` `-` |
| 4 to 7 | `>=`, then `<=`, then `>`, then `<`: each is a level of its own |
| 8, 9 | `==`, then `!=` |
| 10 | `and` or `&&` |
| 11 | `or` or `\|\|` |
| 12 | `c ? a : b`, right to left |

`not` binds tighter than a comparison: `not a > b` is `(not a) > b`, so write `not (a > b)`. Comparisons do not chain, because `a < b` is
a `bool` and the next `<` wants a number:

```lipi-invalid theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Chained comparison")
plot(low < close < high ? 1 : 0)
```

The compiler says: Cannot call `operator <` with argument `expr0`=`low<close`. An argument of `series bool` type was used but a `series int` is expected.
Write `low < close and close < high`.

## Arithmetic

* `int` with `int` gives an `int`, except `/`, which always gives a `float` (see [Values and types](https://gocharting.com/docs/scripting/language/values-and-types.md)).
  Any `float` operand gives a `float`. `bool` and `string` take no part in arithmetic.
* `%` is the remainder. There is no power operator: `x = close ^ 2` gives extraneous input '^' expecting \{\<EOF>, NEWLINE}. Write
  `math.pow(x, 2)`.
* An operand that is `na`, or a division by zero, gives `na`.

## Comparison and logic

* `==` and `!=` work on numbers, strings and enum members; `<` `>` `<=` `>=` on numbers.
* `and`, `or`, `not` are the same as `&&`, `||`, `!`. Both sides of `and` and `or` are always evaluated (no short-circuit), and if
  either side is `na` the result is `na`, which an `if` treats as false.
* An `if` or `while` condition is a `bool` or a number (zero is false); a string is refused.

## The conditional expression

`condition ? a : b` is `a` when the condition is true and `b` otherwise (`na` counts as false). Only the chosen branch runs, which
matters when it calls a `talib` function (see [Calls that must run on every bar](https://gocharting.com/docs/scripting/evaluation/call-contract.md)). It nests to the right:
`x > 0 ? 1 : x < 0 ? -1 : 0`. The branches must have compatible types.

## The history operator

`x[n]` is the value `x` had `n` bars ago; `x[0]` is the current value. It works on series values, your own variables, call results
and parenthesised expressions, not on a literal or `na`. The index rules and the 500-bar reach are in [Referencing previous bars](https://gocharting.com/docs/scripting/evaluation/referencing-past-bars.md).

```lipi-fragment theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
float change = close - nz(close[1])
float lagged = talib.sma(close, 5)[1]
```

`"Bar " + bar_index` is refused: `+` joins two strings, so write `"Bar " + str.tostring(bar_index)`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.