Comparison operators in pine script refer to the operators that are used to do a comparison between operands. It will return true or false only. Comparison operators tell us about the expression that either it is false or it is true. it is widely used in programming to make decisions like to check either price is above or below a certain support level.
Types of comparison operators
There are six types of comparison operators in pine language.
- ( > ) Greater than
- ( < ) Less than
- ( != ) not equal to
- ( == ) Equal to
- ( <= ) Less than or equal to
- ( >=) Greater than or equal to
Two operands are compared by using comparison operators. When numbers are compared in pin language, it returns a constant Boolean expression (true or false). While coding, we compare numbers, series, strings, colors, and lines to get the desired output.
For example, when two numbers 5 and 6 compared using a greater than the operator, like 6 > 5 it will return a true value. Constant Boolean expression true.
Look at the code below.
nmbr0 = 5<6 // true nmbr1 = 5>6 // false nmbr2 = 5==6 // false nmbr3 = 5!=6 // true nmbr4 = 5<=6 // true nmbr5 = 5>=6 // false
Strings can be compared only using the equal to a comparison operator. If you will compare two strings using a comparison operator other than equal to (or not equal to) you will get an error in the code. This is case-sensitive. Even a string with a space and a string without space are not equal and it will return true.
string1 = "pine" == "pine" // true string2 = "pine" == "Pine" // false string3 = "" == " " // false // string4 = "pine" >= "pine" // error string5 = "pine" != "Pine" // true
In the case of series, the comparison operator will also return series of true or false. For example, if you are comparing close series within an integer then it will continue returning false or true.
Within close series, there are candlestick closing prices in a series. During comparison with an integer, it will compare every candlestick closing price with an integer and return a true or false. So it will also form a series of true or false.
For example, there is a support or resistance level in currency at 1.0000. then you can compare the close series with this float value.
Close > 1.0000
If the current closing price of the last candlestick is 0.9000 then it will return false. Remember it is in series format. Whenever currency closing price will become greater than 1.0000 then it will return a true value and you can trigger a buy position/sell position according to the scenario. This is an example to show you how it will work.
series1 = close > 5 // [true,false] depends on the value series2 = high < 5 // [true,false] depends on the value series3 = close == 5 // [true,false] depends on the value
Boolean & Colors comparison
Booleans and colors can also be compared using only equal to or not equal to operator. otherwise, it will give an error. It also can not be compared with strings. Colors can also be compared with hexadecimal code.
bool_1 = true == true // true bool_2 = true == false // false //bool_3 = true >= true // error color_1 = color.red == color.red //true
Remember, Boolean data type (true/false) can not be plotted on the chart. These values just return the result that either an expression is true or false.