Pine Script If Statements: Write Smarter Conditional Logic
A Pine Script if block selects which statements run or which value is returned. It does not draw or recolor anything until that result is passed to an output function. The distinction matters when a script compiles but the chart does not change.
A complete conditional-color indicator
Paste this standalone Pine v6 example into a new indicator:
//@version=6
indicator("Conditional candle colors", overlay=true)
color candleColor = if close > open
color.green
else if close < open
color.red
else
color.gray
barcolor(candleColor)
The first matching branch supplies the color. A doji is gray. barcolor() stays in global scope; placing it inside the if block is not allowed. While the realtime candle is forming, its close and color can change. This example deliberately colors the current candle; it is not a confirmed-entry signal.
Returning a value versus preserving state
A normal declaration evaluates again on each execution. A declaration with var initializes only the first time that scope executes and retains its value until reassigned. Therefore, adding var to a per-bar if result can accidentally freeze the initial result.
For persistent state, declare a variable first and reassign with := when the intended event occurs. For a calculation that should reflect each bar, omit var. Use compatible return types in the branches; mixing a string with a numeric result is not a sound way to model one output.
An omitted else does not universally mean “keep the previous value.” When an if expression returns a number and no branch matches, its result is na; in v6 a Boolean result defaults to false. A separate persistent variable may retain its previous value if no reassignment occurs. These are different mechanisms. See conditional structures and variable declarations.
Scope and calculation pitfalls
- Indent local blocks by four spaces or a tab. Align
else ifandelsewith their correspondingif. - Keep functions such as
plot(),barcolor()andalertcondition()in global scope; compute their input conditionally. - Evaluate history-dependent calculations consistently. If a moving average is needed later, calculate it outside a branch that runs only occasionally.
- In v6, use an explicit Boolean condition. Do not rely on a number being implicitly converted to true or false.
A hidden plot can use an na series value when it should not be drawn. That changes visibility without putting the plotting call inside a local block. State-changing actions such as a strategy order call have different scope rules and can be conditional.
Check the behavior, not just compilation
Compare an up candle, a down candle and a doji. Then observe a forming candle and a closed candle. If adapting the example to a moving average, calculate the average first and decide whether the comparison must wait for bar close. Record that rule before adding alerts.
The Pine beginner tutorial covers the surrounding script structure. Pineify can assist with generating conditional logic; generated output still needs syntax and chart-behavior checks.
