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 secondifinside it. Both must be true before your nested logic runs.
At a Glance
Section titled “At a Glance”| Difficulty | Beginner |
| Time to implement | 10-15 min |
| Category | Language Basics |
Quick Actions
Section titled “Quick Actions”Quick Start
Section titled “Quick Start”//@version=5indicator("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")Why It Matters
Section titled “Why It Matters”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.
What You’ll Learn
Section titled “What You’ll Learn”- The basic pattern for nesting
ifstatements (includingelse 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.
When to Nest vs. Chain
Section titled “When to Nest vs. Chain”| 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. |
Example: Highlight strong breakouts
Section titled “Example: Highlight strong breakouts”//@version=5indicator("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))Example: Strategy filter
Section titled “Example: Strategy filter”//@version=5strategy("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)Pro Tips & Pitfalls
Section titled “Pro Tips & Pitfalls”- 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 theifblock so nested code can update them safely. - Combine with logical operators wisely. Sometimes a single condition like
if a and bis cleaner than nesting—choose the option that reads best. - Beware of repeated triggers. Use
barstate.isnewor store flags when nested checks can fire multiple times on the realtime bar.
Troubleshooting & FAQ
Section titled “Troubleshooting & FAQ”The inner block runs unexpectedly
Section titled “The inner block runs unexpectedly”Verify indentation. The nested block must be indented relative to the outer block; otherwise Pine treats it as a separate statement.
Should I nest inside else?
Section titled “Should I nest inside else?”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.
Key Takeaways
Section titled “Key Takeaways”- Nested
ifstatements 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 ifor logical operators. - Visualise nested logic with labels/plots to confirm behaviour before deploying to live charts.
Keep Going
Section titled “Keep Going”- Open AI Editor to experiment interactively
- Learn more control flow tools
- Connect to Live Trading when your decision logic is production ready
Adapted from tradingcode.net, optimised for Algo Trade Analytics users.