Skip to content

TradingView's nested if statement: if inside another

TradingView’s nested if statement: if inside another

Section titled “TradingView’s nested if statement: if inside another”

TL;DR
Check the broad condition first, then evaluate a second if inside it. Both must be true before your nested logic runs.

DifficultyBeginner
Time to implement10-15 min
CategoryLanguage Basics
//@version=5
indicator("Nested if demo", overlay=false)
var int count = 0
if close > ta.ema(close, 50) // higher-level filter
if volume > ta.sma(volume, 20) and close > high[1] // nested requirement
count += 1
plot(count, "Qualified signals")
Tip. Pine relies on indentation to determine block scope. Nested `if` blocks must be indented one level deeper than the outer block.

Nested if statements let you model tiered logic—first confirm trend, then confirm trigger; first check session, then check indicator. Clear nesting prevents duplicate code and keeps requirements explicit.

  • The basic pattern for nesting if statements (including else if).
  • When to prefer nesting versus combining conditions with and.
  • How to return values from nested branches cleanly.
  • Practical indicator and strategy examples that benefit from multi-step checks.
Use nesting when…Use else if / logical operators when…
You need to execute additional code inside the outer branch (e.g., bookkeeping, logging).You simply want mutually exclusive branches that return a value.
Later checks rely on state created in the outer block.Conditions are independent and can be combined with and / or.
//@version=5
indicator("Breakout highlighter", overlay=true)
fastLen = input.int(20, "Fast Donchian")
slowLen = input.int(60, "Slow Donchian")
volumeLen = input.int(30, "Volume SMA")
upperFast = ta.highest(high, fastLen)
upperSlow = ta.highest(high, slowLen)
volAvg = ta.sma(volume, volumeLen)
if high > upperFast[1]
if high > upperSlow[1] and volume > volAvg
label.new(bar_index, high, "Strong breakout",
style=label.style_label_down,
textcolor=color.white,
bgcolor=color.new(color.green, 70))
plot(upperFast, "Fast high", color=color.new(color.green, 40))
plot(upperSlow, "Slow high", color=color.new(color.orange, 40))
//@version=5
strategy("Nested filter entry", overlay=true)
emaFast = ta.ema(close, 21)
emaSlow = ta.ema(close, 55)
rsiVal = ta.rsi(close, 14)
if emaFast > emaSlow
if rsiVal < 60 and close > emaFast
strategy.entry("Long", strategy.long)
else
if rsiVal > 40 and close < emaFast
strategy.entry("Short", strategy.short)
  • Keep nesting shallow. If you find yourself three or four levels deep, extract helper functions or early-return guards for clarity.
  • Initialise state outside. Set variables (e.g., var bool triggered = false) before the if block so nested code can update them safely.
  • Combine with logical operators wisely. Sometimes a single condition like if a and b is cleaner than nesting—choose the option that reads best.
  • Beware of repeated triggers. Use barstate.isnew or store flags when nested checks can fire multiple times on the realtime bar.

Verify indentation. The nested block must be indented relative to the outer block; otherwise Pine treats it as a separate statement.

Yes—placing an if inside else is a common pattern when you want alternative checks only after the primary condition fails.

How do I return values from nested branches?

Section titled “How do I return values from nested branches?”

Use assignments: result = if condition if nested value1 else value2 else value3.

  • Nested if statements enforce multi-step requirements in Pine Script.
  • Indentation controls scope—double-check spacing to avoid surprises.
  • Use nesting when later checks depend on earlier ones; otherwise consider else if or logical operators.
  • Visualise nested logic with labels/plots to confirm behaviour before deploying to live charts.

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