Conditional Statements#
if / else#
Pine Script conditional syntax is similar to most programming languages:
if condition
// executed when condition is true
else if anotherCondition
// executed when anotherCondition is true
else
// executed when none of the above conditions are trueNote: Pine Script uses indentation (spaces or tabs) to delimit code blocks, similar to Python. Indentation must be consistent; 4 spaces is recommended.
Example — marking bullish/bearish signals on the chart based on price vs. moving average:
//@version=6
indicator("MA Trend Signal", overlay=true)
maLength = input.int(20, "MA Period")
ma = ta.sma(close, maLength)
plot(ma, "MA", color=color.orange)
if close > ma
label.new(bar_index, low, "▲", color=color.green, textcolor=color.white, style=label.style_label_up)
else if close < ma
label.new(bar_index, high, "▼", color=color.red, textcolor=color.white, style=label.style_label_down)if as an Expression#
In Pine Script, if can be used as an expression that directly returns a value:
// Traditional style
var color barColor = na
if close > open
barColor := color.green
else
barColor := color.red
// Using if as an expression (more concise)
barColor = if close > open
color.green
else
color.red
barcolor(barColor)Ternary Operator#
The ternary operator ? : is a shorthand for if/else, suitable for simple two-choice conditions:
// Syntax: condition ? value_if_true : value_if_false
barColor = close > open ? color.green : color.red
barcolor(barColor)Ternary operators can be nested, but too many levels reduce readability:
// Three colors: green for up, red for down, gray for flat
barColor = close > open ? color.green :
close < open ? color.red :
color.gray
barcolor(barColor)Comparison Operators#
| Operator | Description | Example |
|---|---|---|
== | Equal to | close == open |
!= | Not equal to | close != close[1] |
> | Greater than | close > ma |
>= | Greater than or equal to | rsi >= 70 |
< | Less than | close < ma |
<= | Less than or equal to | rsi <= 30 |
Logical Operators#
| Operator | Description | Example |
|---|---|---|
and | Both conditions must be true | close > ma and rsi > 50 |
or | Either condition must be true | close > high[1] or volume > avgVolume |
not | Negates the condition | not na(rsiValue) |
Practical Example: Golden Cross Signal#
Using the short-term MA crossing above the long-term MA (golden cross) as an example to demonstrate a complete conditional flow:
//@version=6
indicator("Golden Cross / Death Cross", overlay=true)
fastLength = input.int(5, "Fast MA")
slowLength = input.int(20, "Slow MA")
fastMA = ta.sma(close, fastLength)
slowMA = ta.sma(close, slowLength)
plot(fastMA, "Fast MA", color=color.blue)
plot(slowMA, "Slow MA", color=color.orange)
// ta.crossover(a, b): a crosses above b (golden cross)
// ta.crossunder(a, b): a crosses below b (death cross)
goldenCross = ta.crossover(fastMA, slowMA)
deathCross = ta.crossunder(fastMA, slowMA)
if goldenCross
label.new(bar_index, low, "Golden", color=color.green, textcolor=color.white, style=label.style_label_up)
if deathCross
label.new(bar_index, high, "Death", color=color.red, textcolor=color.white, style=label.style_label_down)switch Statement#
When there are many conditional branches, switch is cleaner than multiple if/else if chains:
//@version=6
indicator("Trend Detection")
maFast = ta.sma(close, 5)
maMid = ta.sma(close, 20)
maSlow = ta.sma(close, 60)
// Bullish alignment: fast > mid > slow
// Bearish alignment: fast < mid < slow
// Otherwise: consolidation
trendLabel = switch
maFast > maMid and maMid > maSlow => "Bullish Alignment"
maFast < maMid and maMid < maSlow => "Bearish Alignment"
=> "Consolidation"
// Display current trend on the rightmost bar
if barstate.islast
label.new(bar_index, high, trendLabel, style=label.style_label_left)