Branching
Lipi has two ways to choose which statements run: if and switch.
if
An if takes a condition and a braced block. After it you may write any number of else if arms, and one else arm at the end. Both are optional.
if close > close[1] {
float gain = close - close[1]
} else if close < close[1] {
float loss = close[1] - close
} else {
float flat = 0.0
}if is a statement. It decides which block runs; it does not itself produce a value. When what you want is a value rather than a block, use the conditional expression instead.
Chart output cannot be branched
Every chart output function has to be called at the top level of a script. A branch opens a scope of its own, so a call placed inside one is rejected by name.
That changes how you write a conditional plot. Keep the call at the top level and put the condition inside an argument:
color shade = close > close[1] ? #188A6B : #A6323C
plot(close, color = shade)switch
switch is the other branching statement. Its arms are written with case, and a final arm may be written with default.
No worked switch example is given here. How an arm is punctuated is not documented, and a guessed example would be worse than none.
A complete script
indicator("Direction", "dir", false)
float change = close - close[1]
float signal = change
if change > 0 {
signal := 1.0
} else if change < 0 {
signal := -1.0
} else {
signal := 0.0
}
plot(signal)