Lipi Reference Lipi

Keywords

if

If statement defines what block of statements must be exeuted when conditions of the expression are satisfied.

Syntax

                if <condition> {
	<true_block>
} else if <condition> {
	<true_block>
} else {
	<false_block>
}
              

Example


          indicator("If Function Example - Moving Average Strategy", overlay=true)
// Define Moving Averages
shortMA = talib.sma(close, 10)
longMA = talib.sma(close, 50)
// Function to determine signal color
func getSignalColor() {
    if shortMA > longMA {
        return color.green
    } else {
        return color.blue
    }
}
// Plot Moving Averages
plot(shortMA, title="Short MA (10)", color=color.blue)
plot(longMA, title="Long MA (50)", color=color.orange)
bgcolor(getSignalColor())