The conditional ternary operator provides us with a concise way to make if/else statements in TradingView, but its syntax is not always straightforward. Luckily, there’s a more easy-to-use function that has the same capabilities.

In this article:

A compact if/else statement with TradingView’s iff() function

TradingView Pine doesn’t have if/else statements, contrary to other languages like C# or PowerLanguage. Instead, we can mimic that branching statement with the conditional ternary operator (?:) and the iff() function (see Pine Script Language Tutorial, n.d.). The iff() function and the conditional operator have the same functionality, but iff() is easier to use while the conditional operator is more efficient (TradingView, n.d.; Pine Script Language Tutorial, n.d.).

The iff() function requires three arguments and has the following syntax (Pine Script Language Tutorial, n.d.):

iff(condition, result1, result2)

The iff() function returns a value that depends on its condition: when the condition evaluates to true, result1 is returned; otherwise, result2 is returned (Pine Script Language Tutorial, n.d.). In other words, the its logic is: “if this condition is true, return the first value; otherwise, return the second value”.

Tip: The condition doesn’t have to be a Boolean true/false expression; it can also be a number. In that case; zero, NaN, positive and negative infinity (+Infinity and -Infinity) are considered false, while any other numerical value is considered true (TradingView, n.d.).

Both result1 and result2 need to have the same type. And so the iff() function does not accept a number and colour like this:

higherBar = iff(close > close[1], 20, blue)

Setting a variable’s value with iff() in TradingView

Since iff() returns either one of two values depending on its condition, it can be used as a function argument or when assigning a value to a variable, like setting the length of an Exponential Moving Average (EMA):

emaLength = iff(isdaily, 10, 21)

Here we create a variable named emaLength that’s given the value returned by the iff() function. The condition to evaluate in iff() is isdaily, a built-in variable that returns true when the symbol’s resolution is a daily time frame (TradingView, n.d.). When this condition is true (so when the chart is a daily chart), the value of 10 is put into the emaLength variable; otherwise, the emaLength variable is given the value of 21.

A basic example of the iff() function in TradingView Pine

An example indicator that uses the iff() function is:

study(title="iff() basic example", overlay=true)

emaValues = ema(close, 10)

plotColour = iff(close > emaValues,      // if
     green,                              // then
     red)                                // else

plot(series=emaValues, linewidth=4, color=plotColour)

Here we first set the script’s characteristics with the required study() annotated function: with its title argument we set the script’s name to “iff() basic example” and with overlay set to true the indicator plots on the data series and not in a separate chart pane (see TradingView, n.d.).

Then we use the ema() function to calculate the EMA values. This function’s first argument, the series of values to process (TradingView, n.d.), is set to the symbol’s closing price (close). Its second argument is the number of bars to calculate the EMA of (TradingView, n.d.) and that’s set here to 10 for a 10-period EMA.

We then set the plotColour variable’s value to whatever value is returned by the iff() function. The condition we evaluate is whether the symbol’s closing price is greater than (>) the EMA variable’s current value (emaValues). When it is, the bar closed above the EMA and iff() returns the green colour; otherwise, red is returned. The comments in the example highlight how iff() corresponds to an if…then…else statement: if the condition evaluates to true, then the variable is assigned green; else red is stored in it.

After that we plot the EMA values with plot(). This function’s series argument specifies the series to be plotted (TradingView, n.d.), and we set that argument to emaValues here. The linewidth argument, that sets the plot’s width from 1 (thinnest) to 4 (thickest) (TradingView, n.d.), is set to 4 while the color argument is set to our plotColour variable.

And so this example indicator plots an EMA line that’s red when it’s above the symbol’s price and green when the line is below the close:

TradingView example of the iff() function

Nesting the iff() function for checking several conditions

The iff() function can also be nested in order to evaluate several conditions. For example:

study(title="Momentum with iff()")

horizLine = iff(isweekly, 0,
     iff(isdaily, -10,
     iff(isintraday, -20,
     5)))

plot(series=mom(close, 20), color=blue)
plot(series=horizLine, color=gray)

First we set the script’s properties with study(), a required that’s function for every script (TradingView, n.d.). With it we set the indicator’s title to “Momentum with iff()”.

We then use three iff() functions to determine the value of the horizLine variable, which is used to plot a horizontal line later on. First we evaluate if the isweekly built-in variable is true, which it is when the chart’s symbol is plotted with a weekly resolution (TradingView, n.d.). The iff() function returns a 0 when that’s the case; otherwise the second iff() function is executed. That function checks if the chart has a daily time frame, in which case isdaily returns true (TradingView, n.d.) and this iff() function returns -10.

When the chart’s time frame is neither weekly or daily, the third iff() function checks if the chart’s resolution is an intra-day chart. For this it uses the isintraday built-in variable that returns true on intra-day charts (TradingView, n.d.). And so when the chart’s resolution is lower than a day, this last iff() returns -20; otherwise, 5 is returned. This 5 acts as the default value: for any time frame that’s not weekly, daily, or intra-bar, the value of 5 is put into the horizLine variable.

Tip: The very last argument of nested iff() functions act as a default: when none of the iff() functions’ conditions evaluates to true, this default value is returned. To prevent a parentheses mismatch, this default value needs to be followed by as many parentheses as iff() functions used.

We then plot a momentum line with plot() and its series argument set to the value returned by mom(). This latter function, which requires a series of values to process and the number of bars to calculate the momentum between (TradingView, n.d.), has its arguments set to close and 20 so that it calculates the chart’s 20-bar closing price momentum. The plot() function’s color argument is set to blue to display this momentum line in blue. The second plot() function call plots a grey horizontal line with its series argument set to the horizLine variable.

When we add this example to an intra-day gold chart, the horizontal line is plotted at -20:

Plotting momentum with the iff() function

On a daily chart, the horizontal line is plotted at -10:

Using the iff() function in a TradingView script

And on a weekly chart, our horizontal line has a value of 0:

Example of the iff() function in TradingView Pine

To learn more, see the conditional ternary operator in Pine for an alternative to iff(). Also interesting is that the condition evaluated by iff() can contain several sub-conditions by using TradingView’s logical operators. Other TradingView operators are comparison operators and arithmetic operators.

Summary

The iff() function is a compact if/else statement. The first of its three arguments is the condition to evaluate, which can either be a true/false expression or a numerical value. The second argument is the value to return when the condition is true, while the third argument is returned when the condition is false. As such, its logic is: “when this condition is true, return the first value; otherwise, return the second”. Since iff() always returns one of two values, it’s used to set a variable’s value conditionally. When iff() functions are nested, the last argument of the last iff() function is a default value that’s returned when none of the conditions evaluated to true.


References

Pine Script Language Tutorial (n.d.). Retrieved on August 13, 2015, from https://docs.google.com/document/d/1sCfC873xJEMV7MGzt1L70JTStTE9kcG2q-LDuWWkBeY/

TradingView (n.d.). Script Language Reference Manual. Retrieved on June 10, 2015, from https://www.tradingview.com/study-script-reference/