Skip to main content

iff

Syntax Format

iff(condition, then, else)

Overview

The iff function is a functional version of the if … then … else … operator. The function returns the series with the values from the then and else arguments, depending on the condition. This is the safest and the shortest version of the ternary operator (?:), as Lua does not support ternary operators. This function makes the conditions clear and easy to access in a script.

Parameters

ParameterPurpose
conditionThe condition expression. Condition is considered to be false, if it is nilnanfalse or 0, true otherwise
thenA value to be set for the current bar of the returned series if the condition is true
elseA value to be set for the current bar of the returned series if the condition is false

Returns: series if any of the arguments is seriesnumeric otherwise.

Example

uptrend = conditional(close > open)
color = iff(uptrend [1], 'green', 'red')