Skip to main content
if and switch choose which statements run on a bar. Both are statements with no value, so x = if ... is a syntax error; to choose a value use c ? a : b (see Operators and expressions).

if, else if, else

The condition is a bool or a number (zero is false, and so is na). A string is refused: if conditional expression must be of a bool type!. Braces and line breaks follow Statements and lines. A variable declared inside a block is not visible after it, so declare it before the if and assign it with :=.

switch

switch value { is followed by case x: lines and an optional default:.
  • The first matching case runs and the switch ends: there is no fall-through and no break is needed.
  • A case takes exactly one value (case 0, 1: is a syntax error). Strings and enum members work as values; the value must have the type of the subject (a float subject takes an int case).
  • At most one default: Only one default case is allowed in a switch statement.

What may go inside a branch

The refused calls draw one series for the whole script, so they belong at the top level:
The compiler says: plot() must be called in the global scope. The editor’s check reports it, and the name in the message is the call you wrote. Put the condition into an argument and give na for the bars you want empty: plot(close > open ? close : na, title="Up close").