Conditional structures
Conditional Constructs
An Overview
In Pine Scriptβ’, the fundamental conditional constructs include if
and switch
. They have two primary functions:
To perform side effects, where they donβt return a value but instead execute actions like reassigning variable values or invoking functions.
To return a value or a tuple that can subsequently be assigned to one or more variables.
The local blocks within these conditional constructs must have an indentation of four spaces or a tab.
if
Construct
Utilizing if
for Side Effects
The if
construct, when used for side effects, follows this syntax:
The <expression>
should be of βboolβ type or should be auto-castable to that type, possible only for βintβ or βfloatβ values. <local_block>
consists of zero or more statements followed by a return value, which can be a tuple of values.
When the <expression>
following if
evaluates to true, the first local block is executed, and the value(s) evaluated at the end of the local block are returned. If <expression>
is false, the script evaluates successive else if clauses (if any exist). When an <expression>
is true, its local block is executed, and the value(s) evaluated at the end of the local block are returned. If no <expression>
has evaluated to true and an else clause exists, its local block is executed and the value(s) at the end of the local block are returned. If no <expression>
has evaluated to true and no else clause exists, na
is returned.
Utilizing if
to Return a Value
An if
construct that returns one or more values follows this syntax:
The value assigned to the variable is the return value of the <local_block>
, or na
if no local block is executed.
switch
Construct
The switch
construct is available in two forms. One form switches based on different values of a key expression, while the other form does not use an expression as a key but switches based on the evaluation of different expressions.
Only one local block of a switch
structure is executed, so break
statements are not necessary. If no local block is executed, na
is returned.
switch
with an Expression
An example of a switch
construct using an expression:
Sure, continuing from where we left off:
In this case, maType
is evaluated, and the corresponding code block is executed based on its value. If none of the defined case matches, the default case (represented by =>
) is executed.
switch
without an Expression
The switch
construct can also be used without an expression. An example is as follows:
This structure operates similarly to an if-else if-else
construct. The expressions are evaluated from top to bottom. When an expression evaluates to true
, its corresponding <local_block>
is executed and the value(s) at the end of the block are returned. If none of the expressions are true
and a default clause (represented by =>
) exists, its <local_block>
is executed.
Here is an example:
In this case, if isAboveMa
is true
, the bar color will be green. If isAboveMa
is false
but isBelowMa
is true
, the bar color will be red. If neither of these conditions is met, the bar color will be blue.
Last updated