ScriptingWriting LipiOperators and expressions

Operators and expressions

An expression combines values into a new value. This page lists every operator Lipi has, and two that people expect and it does not have.

Arithmetic

OperatorWritten as
+expr0 + expr1
-expr0 - expr1
*expr0 * expr1
/expr0 / expr1
%expr0 % expr1

- is also the negation operator, written in front of a single expression.

There is no exponentiation operator

^ is not an operator in Lipi. A script containing it does not parse.

Comparison

Each of these produces a bool.

OperatorWritten as
==expr0 == expr1
!=expr0 != expr1
>expr0 > expr1
>=expr0 >= expr1
<expr0 < expr1
<=expr0 <= expr1

Logical operators have two spellings

Each logical operator can be written as a symbol or as a word. The two spellings are the same token, so the choice is one of house style and nothing else. There is no difference in behaviour to weigh up.

MeaningSymbolWord
both&&and
either||or
negation!not
bool up = close > close[1] and close > 0
bool upAgain = close > close[1] && close > 0

Pick one spelling for a script and keep to it.

Choosing between two values

The conditional expression takes a condition and two values, and produces one of them.

color shade = close > close[1] ? #188A6B : #A6323C

Use this rather than a branch when what you want is a value. Chart output functions have to be called at the top level of a script, so a condition that decides how something looks belongs in an argument, not around the call.

Declaring and reassigning

Two operators assign, and they do different jobs.

OperatorJob
=declares a name and gives it a value
:=gives a new value to a name that already exists

A declaration is an optional data type, a name, =, and an expression. It may also open with a keyword that decides whether the value survives from one bar to the next; that keyword is covered with persistence.

Five compound assignments are accepted: +=, -=, *=, /= and %=.

float total = close
total := total + close[1]
total += close[2]

Mathematical constants

Four constants are published. Each is a const float.

ConstantType
math.piconst float
math.econst float
math.phiconst float
math.rphiconst float

A complete script

indicator("Percent change", "pct", false)
float previous = close[1]
float change = close - previous
float pct = change / previous * 100
plot(pct)
Ready to put this into practice?
Try GoCharting Premium
Unlock advanced orderflow, market profile, options desk, and real-time data — everything you just read about, live in your charts.
Upgrade