Topics like health, travel, finance, home, living, education and careers | Media Search Home

TradingView"s conditional ternary operator explained

The comparison and logical operators in TradingView return either a true or false value. But how can we simulate if/else logic with those Boolean values in TradingView Pine?

IN THIS ARTICLE:

  • The conditional ternary operator in TradingView Pine explained
  • Setting a variable"s value with the conditional operator
  • Using the conditional operator to colour a TradingView chart
  • Nesting conditional ternary operators in TradingView
  • Summary
# The conditional ternary operator in TradingView Pine explained

It"s hard to find a TradingView script that doesn"t use a single operator. An operator is a code element that performs a certain action on one or multiple values, and the values that it acts on are called operands (Sharp, 2013; Stephens, 2014).

Most TradingView operators require two operands, but there"s one that requires three: the conditional ternary operator (that"s why it"s called a ternary operator). The conditional operator (?:) evaluates a condition and returns one of two values depending on whether that condition is true or false (Pine Script Language Tutorial, n.d.). It works like an if/else statement which the TradingView scripting language doesn"t have.

The conditional operator has the following syntax (Pine Script Language Tutorial, n.d.):

condition ? result1 : result2

When its condition evaluates to true, result1 is returned; otherwise result2 is returned (Pine Script Language Tutorial, n.d.). Its logic is: “when this condition is true, return the first value; else return the second value”, which makes it works the same as the iff() function.

The values that the conditional operator can returned need to have the same type. So when result1 is an integer, result2 needs to be an integer type also. And when result1 is set to a string, result2 cannot be a colour.

The condition that"s evaluated by this operator can be a Boolean true/false expression (like close > open) or a numerical expression (such as high - high[1]). In this latter case, values of 0, NaN, and +Infinity and -Infinity are considered false, while other numerical values are seen as true (TradingView, n.d.).

As a memory aid, remember that the conditional operator asks “is this true?” and then chooses one of two options. So the question mark (?) comes before the colon (:).

# Setting a variable"s value with the conditional operator

Since the conditional operator always returns one of two values, it"s often used in Pine for setting the value of a variable. For instance:

barColour = (close > open) ? blue : red

Here the conditional operator evaluates if the bar"s closing price (close) is greater than (>) its opening price (open). When it is, the blue colour is returned and stored in the barColour variable; otherwise, red is assigned to this variable.

# Using the conditional operator to colour a TradingView chart

An example indicator that uses the conditional operator is:

study(title="Conditional operator - example 1", overlay=true) background = (volume > volume[1]) ? green : yellow bgcolor(color=background, transp=85)

We first use the mandatory study() function to set the script"s characteristics (TradingView, n.d.). With its title argument we specify the name of the indicator. And with overlay set to true the indicator is displayed on the price chart and not in a separate chart pane (TradingView, n.d.).

We then create a variable (background) whose value is set by the conditional operator. The condition evaluated by this operator is whether the current bar"s volume (volume) is greater than that of the previous bar, which we retrieve with volume and the history referencing operator set to 1 (volume[1]). When this condition is true, the conditional operator returns green and that colour is then stored in the background variable. And if the current bar"s volume is not more than the previous bar"s volume, yellow is assigned to that variable.

The last statement sets the chart"s background with bgcolor() (TradingView, n.d.). This function"s color argument specifies the colour and we set that to the background variable. The transp argument sets the background transparency ranging from 0 (not transparent) to 100 (invisible) (TradingView, n.d.), and with a value of 85 we get a highly-transparent background.

This example indicator added to a 60-minute Microsoft chart looks as follows (the histogram at the bottom is the default ‘Volume’ indicator):

TradingView example of conditional ternary operatorTradingView example of conditional ternary operator# Nesting conditional ternary operators in TradingView

The conditional operator can also be used to test several conditions, like this example script shows:

study(title="Conditional operator - example 2", overlay=true) height = input(title="Arrow height", type=integer, defval=20) arrowPlot = (close > close[1] and close[1] > close[2]) ? 1 : (high > high[1] and high[1] > high[2]) ? 1 : (close < close[1] and close[1] < close[2]) ? -1 : (low < low[1] and low[1] < low[2]) ? -1 : na plotarrow(series=arrowPlot, colordown=red, colorup=lime, maxheight=height, minheight=height)

We first set the script"s characteristics with study(). That required function"s title argument defines the indicator"s name and with overlay set to true the indicator plots on the data series and not in a separate chart pane (TradingView, n.d.).

The input() function then adds a manual input option to the script (TradingView, n.d.). The input"s name is set to “Arrow height” and its value is an integer with a default value (defval) of 20. The height variable is given the value returned by this function, which is whatever value the input is set to (TradingView, n.d.).

Then several conditional operators are combined to determine the value of the arrowPlot variable, with which we plot an arrow later on. The first evaluates two expressions: whether the current bar"s close (close) is greater than (>) the previous bar"s close (close[1]), a historical value that"s accessible with the history referencing operator ([ ]) (Pine Script Language Tutorial, n.d.). The second expression checks if the previous bar"s close (close[1]) is greater than the bar before that (close[2]). With the logical and operator between the two expressions, both need to be true before the condition is true (Pine Script Language Tutorial, n.d.). When both are, the conditional operator returns a value of 1; otherwise, the next conditional operator is evaluated.

The second conditional operator evaluates if the current bar"s high is above the high of the previous bar (high > high[1]) and if that high was also greater than the one from 2 bars ago (high[1] > high[2]). When this is the case, this conditional operator returns a value of 1; otherwise, the third conditional operators is evaluated. That conditional operator checks if the current bar"s close is below the close of the bar before it (close < close[1]) and if the previous bar"s close was below its preceding bar (close[1] < close[2]). When that evaluates to true, the third conditional operator returns -1, else the fourth and final conditional operator is executed.

The last conditional operator also returns -1, but this time when the current low is below the previous low (low < low[1]) while the low of the previous bar was less than the bar that preceded it (low[1] < low[2]). Otherwise, this conditional operator returns na. That means “not a number” or NaN for short (TradingView, n.d.), which is the absence of a value (Pine Script Language Tutorial, n.d.).

When several conditional operators are stringed together, the very last argument acts as the default value for when none of the conditions evaluate to true.

After the conditional operators are evaluated, the arrowPlot variable has a value of 1, -1, or na. This variable is then used with plotarrow(). That function plots arrows depending on the value of its series argument: positive numbers give up arrows, negative numbers down arrows, and with na no arrow is plotted (TradingView, n.d.). And so the conditional arrows determine the kind of arrow (if any) is plotted on a price bar.

We specify the arrow colour with the colordown and colorup arguments, which are set to red and lime respectively. Normally an arrow"s length is based on the series argument"s absolute value (TradingView, n.d.), but here we set minheight and maxheight both to the value of the height input variable. That causes all arrows to have the same size, which we can then easily change through the indicator"s ‘Input’ settings.

Added to a Microsoft 60-minute chart, the above TradingView example looks like:

Example of the conditional ternary operator in TradingViewExample of the conditional ternary operator in TradingView

If we change the ‘Arrow height’ input option to 10 the arrows change to:

Using the conditional ternary operator to plot arrowsUsing the conditional ternary operator to plot arrows

And setting them to 50 creates big arrows:

Example of the conditional ternary operator in TradingView PineExample of the conditional ternary operator in TradingView Pine

An alternative to the conditional operator is the iff() function. Conditions that are evaluated by the conditional operator or iff() can contain comparison operators, and with logical operators we can combine several true/false expressions in one condition.

# Summary

The conditional operator is a compact if/else statement that evaluates a condition and returns the first value when the condition is true and the second when it evaluates to false. And so this operator"s logic is: “when the condition is true, return the first value; otherwise, return the second”. The condition that"s evaluated by this operator can be a true/false Boolean expression or a numerical value. When conditional operators are combined, the last argument of the last conditional operator is a default value that"s returned when none of the conditions evaluate to true.

References

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

Sharp, J. (2013). Microsoft Visual C# 2013 Step by Step. Microsoft Press.

Stephens, R. (2014). C# 5.0 Programmer Reference. Indianapolis, IN: John Wiley & Sons.

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

Published October 31, 2015.
# Related TradingView tutorials
  • The iff() function: an alternative to the conditional operator

    In this programming article we learn about the iff() function in TradingView Pine, which allows us to implement if/else logic in our trading scripts.

  • Creating functions with TradingView"s function declaration operator

    In this TradingView tutorial we discuss how to create single-line and multi-line functions with the function declaration operator (=>).

  • Comparing values for true or false with TradingView"s comparison operators

    In this TradingView programming article we look at the comparison operators, including several script examples that show how these operators work.

  • Working with logical operators in TradingView Pine

    This TradingView programming tutorial discusses how the logical operators work, and how they can combine several true/false values into one Boolean.

  • Getting historical data with the history referencing operator

    In this programming article we discuss TradingView"s history referencing operator, and how we can use it to get data from previous price bars.

« All TradingView operators articles


Email: contact@about.com.vn - Phone: 818.333.007 - Website: Media Search Home
Copyright © 2007 - Noos. All rights reserved