Back to TradingView PineScript for CFD Traders
Part 2

Conditional logic: if/else for trade signals

if/else blocks, switch expressions, and how to use them to encode trade rules without spaghetti.

Intermediate7 min readBeginner → AdvancedLesson 06 / 17
Try it in TradingView
Regime classification with persistent state

var bool inLong keeps its value across bars — that's the difference between a per-bar test and an actual position state. Watch the entry/exit triangles.

//@version=5
indicator("Lesson 6 - Conditional logic", overlay=true)

ema50 = ta.ema(close, 50)

// Classify the regime on every bar (chained ternary).
regime = close > ema50 ? "uptrend"
       : close < ema50 ? "downtrend"
       : "range"

plot(ema50, "EMA 50", color=color.orange, linewidth=2)
plot(close, "Close",
     color = regime == "uptrend"   ? color.green
           : regime == "downtrend" ? color.red
           : color.gray)

// Persistent state — survives across bars, unlike a plain if.
var bool inLong = false
if not inLong and close > ema50
    inLong := true
else if inLong and close < ema50
    inLong := false

bgcolor(inLong ? color.new(color.green, 92) : color.new(color.red, 92))

plotshape(inLong and not inLong[1], "Enter long", shape.triangleup,
          location.belowbar, color=color.green)
plotshape(not inLong and inLong[1], "Exit long", shape.triangledown,
          location.abovebar, color=color.red)
To run it
  1. 1. Open any chart in TradingView and click Pine Editor at the bottom.
  2. 2. Select everything in the editor and paste this over it, then click Save.
  3. 3. Click Add to chart. Open the Strategy Tester tab if it is a strategy.

if/else blocks

Pine's if statement works on series — the branch is taken bar by bar. A common pattern:

signal = close > ta.ema(close, 50) ? 1 : -1

This assigns 1 when close is above the 50-EMA, else -1. The ternary is the cleanest form for two-way decisions.

Multi-branch if/else

For three or more branches, if/else if/else reads better:

regime = close > ta.ema(close, 200) ? "uptrend"
     : close < ta.ema(close, 200) ? "downtrend"
     : "range"

Switch expressions

Pine v5 has a switch expression that is cleaner when one variable drives the branch. The MA example from Part 1 used it:

ma = switch maType
    "SMA" => ta.sma(src, length)
    "EMA" => ta.ema(src, length)
    "WMA" => ta.wma(src, length)

State matters

Plain if only changes a variable on the bar the condition is true. If you need state that persists across bars (like "I am currently in a long"), use var:

var bool inLong = false
if not inLong and close > ta.ema(close, 50)
    inLong := true
else if inLong and close < ta.ema(close, 50)
    inLong := false

Common mistake: missing an else

Inside an if without an else, a variable assignment only takes effect on the bar the condition is true. On other bars the variable keeps its previous value — which is sometimes what you want and sometimes a bug. Be explicit.

What to remember

Series-aware if + var for persistent state is the spine of every strategy. Master it before moving to functions.

What you just did

Lesson 06 of 17 in TradingView PineScript for CFD Traders. When you have run the examples or read the section, tick it off and move to the next lesson.