Skip to content
Algo Trade Analytics Docs

switch - Pine Script Keyword

The switch operator transfers control to one of the several statements, depending on the values of a condition and expressions.

[variable_declaration = ] switch expression
value1 => local_block
value2 => local_block
=> default_local_block
[variable_declaration = ] switch
condition1 => local_block
condition2 => local_block
=> default_local_block

The value of the last expression in the local block of statements that is executed.

  • Only one of the local_block instances or the default_local_block can be executed. The default_local_block is introduced with the => token alone and is only executed when none of the preceding blocks are executed. If the result of the switch statement is assigned to a variable and a default_local_block is not specified, the statement returns na if no local_block is executed. When assigning the result of the switch statement to a variable, all local_block instances must return the same type of value.
//@version=6strategy("Switch without an expression", overlay = true)bool longCondition  = ta.crossover( ta.sma(close, 14), ta.sma(close, 28))bool shortCondition = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))switch    longCondition  => strategy.entry("Long ID", strategy.long)    shortCondition => strategy.entry("Short ID", strategy.short)