Utilities / Reference
switch
TradingView-compatible spelling: switch, runs without warnings
Pine-style switch with indent-based body. Cases are evaluated top-to-bottom; first match wins. A trailing `=>` arm (no pattern) acts as the default case. Works as a statement OR as an expression (assignable to a variable). The subject may be omitted, in which case every case is a boolean condition and may open with `not`. `type` and `method` are soft keywords, so either can be used as an ordinary variable or parameter name — `f_ma(src, len, type) => switch type` is accepted. An arm may open an indented BLOCK instead of a single expression — declare locals, branch, and the block's last statement is the arm's value — and a pattern may itself be an enum member (`Model.rolling =>`) or start with a unary minus (`-1 =>`).
switch <expr>
<pattern> => <body>
<pattern> =>
<indented block, last statement is the value>
=> <default> → anyParameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
expr | any | Yes | — | Value being matched. May be omitted for boolean-chain style (each case is a boolean condition). |
Returns
anyExample
preset = "Default"int length = switch preset "Scalping" => 10 "Default" => 14 "Swing" => 21 => 14 // default arm — fires when no pattern matches // Subject-less form: each case is a condition.painting = truecolor tone = switch not painting => na close > open => color.green => color.red // An arm may open an indented block; its last statement is the value.float share = switch math.sign(close - open) 1 => float total = math.sum(volume, 20) total > 0 ? volume / total : na -1 => 0.0 => naNames and aliases
Guides and navigation are localized. Built-in reference descriptions and code examples are maintained in English.