Conditional structures

Conditional Constructs

An Overview

In Pine Scriptβ„’, the fundamental conditional constructs include if and switch. They have two primary functions:

  1. To perform side effects, where they don’t return a value but instead execute actions like reassigning variable values or invoking functions.

  2. 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:

if <expression>
    <local_block>
{else if <expression>
    <local_block>}
[else
    <local_block>]

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:

[<declaration_mode>] [<type>] <identifier> = if <expression>
    <local_block>
{else if <expression>
    <local_block>}
[else
    <local_block>]

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:

float ma = switch maType
    "EMA" => ta.ema(close, maLength)
    "SMA" => ta.sma(close, maLength)
    "RMA" => ta.rma(close, maLength)
    "WMA" => ta.wma(close, maLength)
    => na

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:

[<declaration_mode>] [<type>] <identifier> = switch 
    <expression> => <local_block>
    [<expression> => <local_block> ...]
    [=> <local_block>]

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:

bool isAboveMa = close > ta.sma(close, 20)
bool isBelowMa = close < ta.sma(close, 20)

var color barColor = switch
    isAboveMa => color.green
    isBelowMa => color.red
    => color.blue

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