With the comparison operators in TradingView Pine we compare values against each other and end up with a true or false value. But how can we combine several of those true/false values together?

In this article:

Logical operators in TradingView Pine: combining true/false values

Operators are an important part of what makes scripts work. These code elements perform an operation on one or several values (Stephens, 2014), like addition, assignment, and comparison. The values that an operator ‘operates on’ are what we call operands (Sharp, 2013).

The operators that we’ll frequently use in TradingView Pine are the arithmetic operators, the assignment operator, and the comparison operators. With those latter operators we compare two values against each other, and they return true or false as the result (Pine Script Language Tutorial, n.d.). But what if we want to combine several comparisons?

Combining several true and/or false values into a single true/false value is possible with two logical operators (Pine Script Language Tutorial, n.d.). The and logical operator returns true when both its left and right operands evaluate to true. The or logical operator, on the other hand, returns true when one of its operands evaluates to true (Pine Script Language Tutorial, n.d.).

With the and and or operators it’s possible to answer questions like “did the current bar close higher and is the volume increasing?” and “is the close below the Exponential Moving Average (EMA) or did the market gap down?“.

The third logical operator is the not logical negation operator (Pine Script Language Tutorial, n.d.). This operator works on just one operand and returns true when that operand is false and false when that operand is true. That means the not operator returns the logical opposite (true or false) of whichever value it’s placed for.

The and, or, and not logical operators in TradingView Pine

To summarise the above, TradingView has the following logical operators (Pine Script Language Tutorial, n.d.):

Operator Name and meaning Example Result
and Logical conjunction x and y Only returns true when both x and y are true. Returns false when x is false, y is false, or both are false.
or Logical disjunction x or y Returns true when x is true, when y is true, or when both are true. Only returns false when both x and y are false.
not Negation not x Returns true when x is false, and returns false when x is true.

The table below shows the different outcomes of the logical and, or, and not operators based on the values of two variables:

Values of x and y x and y x or y not x not y
x = true, y = true true true false false
x = true, y = false false true false true
x = false, y = true false true true false
x = false, y = false false false true true

Of course, the logical operators can also be used with more than two values. Let’s say a is true and both b and c are false. When we use the logical and operator to combine those values together (a and b and c), we get a false result. That’s because the and operator only returns true when all values are also true. Likewise, combining a, b, and c with or (so a or b or c) returns true. That’s because or returns true when at least one of its values is true.

Let’s look at a few examples that use these logical operators in actual TradingView Pine scripts.

TradingView example of the logical and & or operators

An example that uses and and or is:

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

emaPeriod = isdaily or isweekly ? 30 : 50

emaValues = ema(close, emaPeriod)

emaColour = 
     iff(close > emaValues and close[1] > emaValues[1], green, 
     iff(close < emaValues and close[1] < emaValues[1], red,
        blue))
    
plot(series=emaValues, color=emaColour, linewidth=2)

We start with the required study() function. Then we create the emaPeriod variable and set its value to 30 or 50 with the conditional ternary operator (?:). That operator evaluates a condition and returns its second operand when that condition is true and its third operand when it’s false (Pine Script Language Tutorial, n.d.).

The condition that we evaluate contains the logical or operator (isdaily or isweekly). isdaily is a built-in variable that returns true on daily charts, while isweekly returns true when the chart has a weekly resolution (TradingView, n.d.). Since we combine those values with or, the condition returns true when isdaily or isweekly returns true. Putting it all together, the conditional operator stores a value of 30 in the emaPeriod variable when the chart has a daily or weekly resolution, and puts a value of 50 into that variable for all other time frames.

Then we calculate the EMA with the ema() function that’s set to the bar’s closing price (close) and the emaPeriod number of bars. The value returned by ema() is put into the emaValues variable series.

Once we’ve calculated the EMA we determine its colour with two iff() functions. The first checks if the closing price (close) is greater than (>) the EMA (emaValues) and whether the previous close (close[1]) was above the previous bar’s EMA (emaValues[1]). When that condition is true, the green colour is returned by iff() and stored in the emaColour variable. When it’s false, the second iff() function checks if both the current (close < emaValues) and previous (close[1] < emaValues[1]) close were less than the EMA. If that’s the case, iff() returns red and we put that colour in the emaColour variable. When none of the two conditions was true, blue is returned as the default colour.

The example ends with plot(), a function that plots a series of data on the chart (TradingView, n.d.). We set its series argument to the EMA (emaValues) and its color argument to the emaColour variable that we’ve just gave one of three colours. With the linewidth argument set to 2 the plot is slightly thicker than usual.

The above example looks like this on an intra-day chart:

TradingView example that uses logical operators

And on a weekly chart, where the EMA is set to 30 bars, it looks like:

TradingView script that uses logical operators

The not operator in TradingView: evaluating to the logical opposite

The not operator works on a single operand. It returns true when its operand evaluates to false and returns false when its operand is true (Pine Script Language Tutorial, n.d.). That makes this a useful operator when something happened (like a gap up or down) and we want to do something when that did not happen. For example:

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

gapUp = (low > high[1])
gapDown = (high < low[1])

backgroundColour = (gapUp == true) ? green : 
     (gapDown == true) ? red :
     na

bgcolor(color=backgroundColour, transp=60)

noGap = not(gapUp or gapDown)

plotshape(series=noGap, style=shape.xcross, 
     location=location.abovebar)

We first set the script’s properties with study(). We then create the gapUp variable that’s true whenever a bar’s low is greater than (>) the high of the previous bar (high[1]). We get that latter value with the history referencing operator ([ ]). The second variable, gapDown, is true whenever the high is less than (<) the low of the preceding bar.

We then create the backgroundColour variable that’s either assigned green, red, or na by two conditional ternary operators (?:). That operator first checks if gapUp equals (==) true. When that’s the case, the colour green is returned and put into the variable. Otherwise, the second conditional operator checks if gapDown equals true. If that occurred, red is returned and stored in backgroundColour. When neither a gap up or gap down happened, the na value is put into that variable. That value acts as a transparent colour (see Pine Script Language Tutorial, n.d.).

The bgcolor() function then sets the chart’s background colour to whatever colour the backgroundColour variable holds. This way the chart’s background highlights any up or down gaps.

Then we work on highlighting the bars that didn’t gap. For that, we first create the noGap variable and give it value returned by the logical not operator placed before the (gapUp or gapDown) condition. That condition, with the logical or operator, returns true whenever there’s an up or down gap (and false when neither happened). By placing not before it we get its logical opposite. So when a gap happened, the condition returns true, but with not we’ll get false. Likewise, when there’s no gap and the condition returns false, not returns true. Those true values are what we’re interested in since they occur on bars without gaps.

We then use plotshape(), a function that plots a shape on the chart whenever its series argument is true (TradingView, n.d.). We set that argument to the noGap variable so that a shape is drawn when the bar didn’t gap. We set the shape’s style to a cross (shape.xcross) and place it above the bar (location.abovebar).

Added to a chart, this example highlights all gap bars with a coloured background and places a ‘x’ above the bars that didn’t gap:

Using the not logical operator in TradingView Pine

Besides the logical operators (and, or, and not), TradingView also has comparison operators (<, <=, !=, ==, >, and >=). The difference between these is the following. Comparison operators compare values against each other, such as whether the bar’s close is greater than the EMA of the last 30 bars. Logical operators, on the other hand, combine true/false values into a single true or false value (Dorman, 2010; Stephens, 2014). They’re used to evaluate whether the bar closed higher than the EMA or if the volume’s EMA is rising.

When we code in TradingView Pine we often use the logical operators with the iff() function and the conditional ternary operator (?:). Other often-used operators in TradingView are the arithmetic operators and the assignment operator.

Summary

The and, or, and not logical operators evaluate one or two true/false values. and only returns true when both of its operands are true, while or returns true when at least one of its operands is true. The not operator evaluates to the opposite of the true/false value it’s placed for (not true returns false and not false returns true).


References

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

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