Script structure
A LipiScript follows this general structure:
<declaration_statement>
<code>
Declaration Statement
All Lipi scripts must contain a single declaration statement, which is a call to this function:
indicator()
The declaration statement:
- Defines the type of script, determining the allowable content and specifying how it can be used and executed.
- Sets essential properties, such as the script’s name, where it appears when added to a chart, the precision and format of displayed values, and runtime behavior parameters, including the maximum number of drawing objects displayed on the chart. For strategies, this includes backtesting parameters like initial capital, commission, and slippage.
Each type of script has specific requirements:
- Indicators must include at least one function call that produces output on the chart (e.g.,
plot()
,plotshape()
,barcolor()
, etc.).
Code
Lines in a script that are not comments or compiler annotations are statements, which implement the script’s algorithm. A statement can be one of the following:
- Variable declaration
- Variable reassignment
- Function declaration
- Built-in function call, user-defined function call, or library function call
if
,for
,while
,switch
,type
, orenum
structure
Statements can be arranged in multiple ways:
- Some statements can be expressed in one line, such as most variable declarations, lines containing only a function call, or single-line function declarations. Lines can also be wrapped (continued on multiple lines). Multiple one-line statements can be concatenated on a single line by using commas as separators.
- Other statements, such as structures or multi-line function declarations, always require multiple lines because they include a local block, which must be indented by a tab or four spaces. Each local block defines a distinct local scope.
Statements in the global scope of the script (i.e., those that are not part of local blocks) cannot begin with whitespace (a space or a tab). Their first character must also be the line’s first character. Lines starting in the first position are part of the script’s global scope by definition.
A simple, valid Lipi Script indicator can be generated in the Lipi Script Editor by using the “Open” button and choosing “New blank indicator.”
indicator("My Script");
plot(close);
This indicator includes three local blocks, one in the barIsUp()
function declaration, and two in the variable declaration using an if structure:
// Declaration statement (global scope)
indicator("Zoom", "", true)
barIsUp() {
// Local block (local scope)
return close > open
}
// Variable declaration (global scope)
plotColor = barIsUp() ?color.green:color.red
// Call to a built-in function (global scope)
bgcolor(color.new(plotColor, 70))
Comments
Double slashes (//
) define comments in Lipi Script. Comments must begin only on the new line.
indicator("");
// This line is a comment
a = close;
plot(a);