When we code in TradingView Pine we often want to know how one value compares to another. Is the current bar’s close, for example, higher than the 20-period average? For that kind of comparisons we use the comparison operators.

In this article:

The comparison operators in TradingView: compare for true or false

An important part of what makes code work are operators. Those code elements perform some operation on one or several values, and those values that an operator ‘works on’ are called operands (Sharp, 2013; Stephens, 2014).

There are several kind of operators in TradingView, like the arithmetic operators and the assignment operator. But to make our scripts more complex we need to evaluate conditions. We do that with the comparison operators (also called relational operators). Those operators compare two operands against each other and return true or false depending on the result (Pine Script Language Tutorial, n.d.).

For example, the greater than operator (>) returns true when the value on its left is greater than the value on its right. And so 10 > 2 returns true while 10 > 19 evaluates to false.

TradingView Pine has the following comparison operators (Pine Script Language Tutorial, n.d.; TradingView, n.d.):

Operator Name Example and result
< Less than 10 < 3     True; 10 is less than 3.
close < close[2]     Returns true only when the current bar’s close is less than the close of two bars ago.
<= Less than or equal to close <= open     Returns only true when the bar’s close is lower than or equal to the bar’s open.
high <= high[1]     Only evaluates to true when the current bar’s high is less than or equal to the previous high.
!= Not equal volume != volume[1]     True for all price bars whose volume is different than the preceding bar; false for all other bars.
open != open[10]     Only returns true when the current bar’s open is unequal to the open of 10 bars ago.
== Equal high == high[1]     Results only in true when the current bar’s high is exactly the same as the high of the previous bar.
volume == sma(volume, 10)     Returns true when the current bar’s volume is identical to the average volume of the last 10 bars; returns false in all other cases.
> Greater than 10 > 3     False; 10 is not greater than 3.
close > ema(close, 30)     Returns only true when the current bar’s close is greater than the closing price’s Exponential Moving Average (EMA) of the last 30 bars.
>= Greater than or equal to high >= low[1]     Returns only true when the high of the current bar is greater than or equal to the previous bar’s low.
sma(close, 10) >= sma(close, 50)     Only returns true whenever closing price’s Simple Moving Average (SMA) is greater than or equal to the SMA of the last 50 closes.

Note that most comparison operators are composed of two characters. Like the greater than or equal to operator (>=) that contains the greater-than symbol (>) and the equal sign (=). These symbols need to appear in that order for the operator to be valid (Liberty & MacDonald, 2009); =< isn’t valid and => is the function declaration operator.

Now, let’s look at a few TradingView examples of these comparison operators.

Example of TradingView’s comparison operators

One way to use comparison operators is to see if the current bar is a new low, like this:

study(title="Relational operators - 1", overlay=true)

plotCircle = low < lowest(low, 20)[1]

circleColour = iff(tr >= sma(tr, 30)[1], red, fuchsia)

plotshape(series=plotCircle, style=shape.circle, 
     location=location.belowbar, color=circleColour)

We first set the indicator’s properties here with study(). Then we make the plotCircle variable that’s given a true/false value based on a comparison with the less than (<) operator. That operator checks here if the current bar’s low (low) is less than the lowest low of the preceding 20 bars.

That latter value is calculated here with the lowest() function and the values of low and 20 between its parentheses. By placing the history referencing operator ([ ]) with a value of 1 behind that function, lowest() returns the lowest low of the 20 bars before the current bar (so without including the current bar) (see Pine Script Language Tutorial, n.d.).

Then we create the circleColour variable that we give a value with the iff() function. That function evaluates a condition and returns the second value (red here) when that condition is true. When it isn’t, the third value (fuchsia) is returned (Pine Script Language Tutorial, n.d.). The condition that we check here is whether tr (the bar’s range; TradingView, n.d.) is greater than or equal to (>=) the average bar range of the previous 30 bars. We calculate that latter with the sma() (Simple Moving Average) function that has the values of tr and 30 passed in. We again use the history referencing operator to get the average values up to the current bar.

So what happens here is the following. When the current bar’s range is greater than or equal to the average range of the last 30 bars, iff() puts the red colour in the circleColour variable. Otherwise, the fuchsia colour is stored in this variable.

We end the example with plotshape(), a function that plots a shape on the chart whenever its series argument is true (Pine Script Language Tutorial, n.d.). In our case, we set series to the plotCircle variable that’s true whenever a new low is made. By setting style to shape.circle we plot circle shapes and we place these below the bar with location.belowbar. The colour of the shapes is set to circleColour, which we set to red or fuchsia earlier.

When we add this example to a chart, it looks like:

Example of TradingView's comparison operators

Checking for consecutive lower lows with the comparison operators

As another example, let’s say we want to highlight all price bars that closed lower for three bars in a row. For that we use the comparison operators like this:

study(title="Relational operators - 2", overlay=true)

closedLower = close < close[1]

backgroundColour = iff(closedLower == true and 
        closedLower[1] == true and 
        closedLower[2] == true,
     red, na)
    
bgcolor(color=backgroundColour, transp=70)

We first use the study() function to set the script’s properties. The variable that we create next (closedLower) holds true whenever the current bar’s close (close) is below the close of the previous bar (close[1]).

Because close is a variable series, closedLower will be a variable series also (Pine Script Language Tutorial, n.d.). That means we can access the values that this variable held on previous bars. We do that by placing a positive integer in the square brackets ([ ]) of the history referencing operator. For example, closedLower[1] returns the value that this variable held on the previous bar.

In the next statement we create the backgroundColour variable. We assign this variable the colour of red or the na value that acts as transparent colour (see Pine Script Language Tutorial, n.d.). The condition evaluated by iff() is whether the closedLower variable is true on the current bar (closedLower == true), true on the previous bar (closedLower[1] == true), and true on the bar before that (closedLower[2] == true). Those three expressions are combined with the and logical operator, which means that all three of them need to be true before the entire condition is true (Pine Script Language Tutorial, n.d.).

What this means is that the backgroundColour variable holds the red colour only when the current bar, the previous bar, and the bar before that all closed lower.

Our example ends with bgcolor(), a function that colours the chart’s background (Pine Script Language Tutorial, n.d.). We set this function’s color argument to the backgroundColour variable so that the background is either red or with a default colour. With the transp argument, which set the transparency from 0 (not transparent) to 100 (fully transparent) (TradingView, n.d.), the background is lightly shaded.

Added to a chart, the above example looks as follows:

Script example with the comparison operators in TradingView

TradingView has, besides comparison operators, also the logical operators and, or, and not (Pine Script Language Tutorial, n.d.). The difference between these is that comparison operators compare values against each other (like if the close is less than the previous close). Logical operators combine true/false expressions (Dorman, 2010; Stephens, 2014), such as whether the bar closed lower and the volume is higher than the previous bar.

The comparison and logical operators are often used with the iff() function and the conditional ternary operator (?:). Other helpful TradingView Pine operators are the arithmetic operators for performing math and the assignment operator for storing values into variables.

Summary

Comparison operators compare two values against each other and return true or false depending on the result. These operators are less than (<), less than or equal to (<=), not equal (!=), equal (==), greater than (>), and greater than or equal to (>=).


References

Dorman, S. (2010). Sams Teach Yourself Visual C# 2010 in 24 Hours. Indianapolis, IN: Sams/Pearson Education.

Liberty, J. & MacDonald, B. (2009). Learning C# 3.0: Master the Fundamentals of C# 3.0. Sebastopol, CA: O’Reilly Media.

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 September 4, 2015, from https://www.tradingview.com/study-script-reference/