Skip to content

TradingView's if/else statement: make code decisions between two options

TradingView’s if/else statement: make code decisions between two options

Section titled “TradingView’s if/else statement: make code decisions between two options”

TL;DR
if evaluates a condition; if it’s true, run one block of code, otherwise run the else block. Use else if for additional branches.

DifficultyBeginner
Time to implement10-15 min
CategoryLanguage Basics
//@version=5
indicator("If/Else demo", overlay=false)
priceColor = if close > open
color.green
else if close < open
color.red
else
color.gray
plot(close, "Close", color=color.new(priceColor, 0))
Tip. Indentation matters: Pine associates indented lines with the nearest `if`, `else if`, or `else` block.

Whether you’re colouring plots, switching between stop-loss styles, or triggering different orders, branching logic is essential. Without if/else, every bar would follow the same path regardless of market context.

  • The basic if/else structure and indentation rules.
  • Nesting and chaining conditions with else if.
  • Assigning values from if/else statements for cleaner code.
  • Practical examples for indicators and strategies.
PatternDescriptionExample
`if condition
// code` | Run code when condition is true. | `if high > hh
breakout := true` |

| if ... else | Choose between two blocks. | if isLong strategy.close("Long") else strategy.close("Short") | | if ... else if ... else | Multiple branches. | if rsi > 70 ... else if rsi < 30 ... else ... | | Assignment | Capture the value returned by each branch. | color = if trendUp then color.green else color.red |

  1. Start with a simple branch
    Evaluate a boolean expression and run one block when it’s true.

    if close > open
    label.new(bar_index, high, "Bullish")
  2. Add the alternative path
    Use else to handle the opposite case.

    if close > open
    label.new(bar_index, high, "Bullish")
    else
    label.new(bar_index, low, "Bearish")
  3. Return values directly
    Assign the outcome to a variable for later use.

    direction = if close > open
    1
    else
    -1
//@version=5
indicator("If/Else signal demo", overlay=true)
rsiLen = input.int(14, "RSI length")
overbought = input.int(70, "Overbought")
oversold = input.int(30, "Oversold")
rsiValue = ta.rsi(close, rsiLen)
signalLabel = if rsiValue > overbought
"Sell"
else if rsiValue < oversold
"Buy"
else
"Hold"
plot(rsiValue, "RSI", color=color.new(color.blue, 0))
hline(overbought, "OB", color=color.new(color.red, 60))
hline(oversold, "OS", color=color.new(color.green, 60))
if barstate.islast
label.new(bar_index, rsiValue, signalLabel,
style=label.style_label_left,
textcolor=color.white,
bgcolor=signalLabel == "Sell" ? color.new(color.red, 70) : signalLabel == "Buy" ? color.new(color.green, 70) : color.new(color.gray, 70))
Why this works.
  • `signalLabel` holds a different string for each condition, making it easy to reuse in labels, alerts, or strategy logic.
  • Chained `else if` keeps all RSI states in one tidy block.
  • Background colours reflect the decision, providing instant visual feedback.
  • Complex logic becomes hard to read quickly—extract repeated expressions into variables or helper functions.
  • For “value only” decisions, consider the ternary operator condition ? value1 : value2 to keep code concise.
  • The else branch is optional; omit it when you only need to act on the true condition.
  • In strategies, guard against duplicate orders by combining if/else with state flags (var bool inPosition).

I get an “Undeclared identifier” error inside else

Section titled “I get an “Undeclared identifier” error inside else”

Double-check indentation: if the else block isn’t indented, Pine thinks you’re declaring a new global variable.

How do I skip assignment entirely when a condition is false?

Section titled “How do I skip assignment entirely when a condition is false?”

Use na to represent the absence of a value: result = if condition value else na.

Yes, but keep readability in mind. Often, else if is clearer than nesting multiple if blocks.

  • if/else is the primary branching tool in Pine Script, letting you run different code paths per bar.
  • Keep indentation consistent and leverage assignments to return values directly from conditions.
  • Use else if and ternaries to handle multiple branches without deeply nested blocks.
  • Combine with plots, labels, or orders to make decisions visible and actionable.

Adapted from tradingcode.net, optimised for Algo Trade Analytics users.