switch

Syntax

plot <plot_name>;
switch (<enum input or enum_def>) {
case <enum value1>:
<plot_name> = <expression1>;
...
default:
<plot_name> = <expression>;
}

Description

The switch statement is used to control the flow of program execution via a multiway branch depending on the input value. In the switch statement you either need to define the case with all values from the enum or use the default statement to define actions for all enums that are not defined using the case. Note that in the latter approach you cannot use case with equal enums.

Example

input price = close;
input plot_type = {default SMA, "Red EMA", "Green EMA", WMA};
plot Avg;
switch (plot_type) {
case SMA:
Avg = Average(price);
case WMA:
Avg = wma(price);
default:
Avg = ExpAverage(price);
}
Avg.SetDefaultColor(
if plot_type == plot_type."Red EMA" then color.RED else
if plot_type == plot_type."Green EMA" then color.GREEN else
color.BLUE);

This example illustrates the usage of the switch reserved word to assign different values to plots. The default keyword must be used unless all possible values of variable are explicitly listed.