Skip to main content

Operators and Control Flow 🔄

Operators

Arithmetic Operators

//@version=5
indicator("Arithmetic Operators")

// Basic arithmetic
a = 10
b = 5

sum = a + b // Addition
diff = a - b // Subtraction
prod = a * b // Multiplication
quot = a / b // Division
mod = a % b // Modulus

plot(sum)

Comparison Operators

//@version=5
indicator("Comparison Operators")

price = close

isHigher = price > open // Greater than
isLower = price < open // Less than
isEqual = price == open // Equal to
isNotEqual = price != open // Not equal to
isGreaterEqual = price >= open // Greater than or equal
isLessEqual = price <= open // Less than or equal

plotchar(isHigher, "Higher", "▲", location.belowbar, color.green)

Logical Operators

//@version=5
indicator("Logical Operators")

condition1 = close > open
condition2 = volume > volume[1]

andResult = condition1 and condition2 // Both conditions must be true
orResult = condition1 or condition2 // At least one condition must be true
notResult = not condition1 // Inverts the condition

plot(andResult ? 1 : 0)

Control Flow

If Statements

//@version=5
indicator("If Statements")

// Simple if
if (close > open)
label.new(bar_index, high, "Bullish")

// If-else
if (close > open)
label.new(bar_index, high, "Bullish")
else
label.new(bar_index, low, "Bearish")

// If-else if-else
if (close > high[1])
label.new(bar_index, high, "New High")
else if (close < low[1])
label.new(bar_index, low, "New Low")
else
label.new(bar_index, close, "Inside Bar")

Switch Statement

//@version=5
indicator("Switch Example")

trend = switch
close > high[1] => "Bullish"
close < low[1] => "Bearish"
=> "Neutral" // default case

label.new(bar_index, close, trend)

For Loops

//@version=5
indicator("For Loop Example")

// Calculate average of last 5 closes
var float sum = 0.0
for i = 0 to 4
sum := sum + close[i]
avgPrice = sum / 5

plot(avgPrice)

Conditional Operators

Ternary Operator

//@version=5
indicator("Ternary Operator")

// Syntax: condition ? value_if_true : value_if_false
color barColor = close > open ? color.green : color.red

plotcandle(open, high, low, close, color=barColor)

Practice Exercises

1. Trend Classification

//@version=5
indicator("Trend Classifier")

// Define conditions
isUptrend = close > open and close > close[1]
isDowntrend = close < open and close < close[1]

// Classify trend
trend = if isUptrend
"Uptrend"
else if isDowntrend
"Downtrend"
else
"Sideways"

// Plot with different colors
plotchar(true, "Trend", "•", location.belowbar,
color = switch trend
"Uptrend" => color.green
"Downtrend" => color.red
=> color.gray)

2. Volume Analysis

//@version=5
indicator("Volume Analysis")

// Calculate average volume
lengthInput = input(20, "Length")
avgVol = ta.sma(volume, lengthInput)

// Classify volume
volClass = switch
volume >= avgVol * 2 => "Very High"
volume >= avgVol * 1.5 => "High"
volume <= avgVol * 0.5 => "Low"
=> "Normal"

// Plot volume bars with colors
plot(volume, style=plot.style_columns,
color = switch volClass
"Very High" => color.green
"High" => color.lime
"Low" => color.red
=> color.gray)
Pro Tips
  • Use switch statements for multiple conditions
  • Combine operators for complex conditions
  • Keep control flow structures simple
  • Use ternary operators for simple conditions

Common Pitfalls

Watch Out For

❌ Forgetting break statements in switch ❌ Complex nested if statements ❌ Infinite loops ❌ Unnecessary complex conditions

Next Steps

Now that you understand operators and control flow, let's move on to built-in variables in Pine Script! 🚀