Skip to main content
On this page

Pine Script If Statements: Write Smarter Conditional Logic

· 4 min read
Pineify Team
Pine Script and AI trading workflow research team

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 if and else with their corresponding if.
  • Keep functions such as plot(), barcolor() and alertcondition() 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.

Can if return a value?
Yes. Assign the structure's result to a variable and make the branches return compatible types.
Why did my color variable not recolor candles?
A variable alone produces no visual output. Pass the selected color to a global barcolor call.
Does var recalculate its initial if expression on every bar?
No. It initializes once for that scope. Use an ordinary declaration for a result that must be calculated on each execution.