0% found this document useful (0 votes)
220 views2 pages

Pine Script Comparison Operators Guide

There are six comparison operators in Pine Script: <, <=, !=, ==, >, and >=. The study demonstrates how these operators can be used to compare numbers, strings, series, booleans, colors, and objects like lines and labels in Pine Script. Each comparison returns either a constant boolean value or a series of boolean values depending on what is being compared.

Uploaded by

happydxt25
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
220 views2 pages

Pine Script Comparison Operators Guide

There are six comparison operators in Pine Script: <, <=, !=, ==, >, and >=. The study demonstrates how these operators can be used to compare numbers, strings, series, booleans, colors, and objects like lines and labels in Pine Script. Each comparison returns either a constant boolean value or a series of boolean values depending on what is being compared.

Uploaded by

happydxt25
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

//@version=4

study("Variable Operators - Comparison Operators")

// There are six comparison operators in Pine Script:


// < Less Than
// <= Less Than or Equal To
// != Not Equal
// == Equal
// > Greater Than
// >= Greater Than or Equal To

// ----- COMPARISONS ------

// numbers
lt_int = 2 < 2 // const bool = false
lt_na = 2 < na // const bool = false
lte_int = 2 <= 2 // const bool = true
e_int = 2 == 2 // const bool = true
e_int_float = 2 == 2.0 // const bool = true
ne_int = 2 != 3 // const bool = true
gt_int = 2 > -1 // const bool = true
gt_na = 2 > na // const bool = false
gte_int = 2 >= 2 // const bool = true

// strings
// lt_string = "" < "" // ERROR
// lte_string = "" <= "" // ERROR
e_string = "a" == "a" // const bool = true
e_string_na = "" == na // const bool = true
e_string_na2 = " " == na // const bool = false
ne_string = "" != " " // const bool = true
// gt_string = "a" > "b" // ERROR
// gte_string = "a" >= "b" // ERROR

// series
lt_series = close < 2 // series bool = [true, false]
lt_series_na = close < na // series bool = [false, false]
lte_series = low <= high // series bool = [true, false]
e_series = close == 2 // series bool = [true, false]
ne_series = close != close // series bool = [false, false]
gt_series = high > 1.0 // series bool = [true, false]
gte_series = high >= low // series bool = [true, false]

// booleans
// lt_bool = true < false // ERROR
// lte_bool = true <= true // ERROR
e_bool = true == true // const bool = true
e_bool2 = true == false // const bool = false
e_bool3 = false == false // const bool = true
e_bool_na = false == na // const bool = false
e_bool_na2 = true == na // const bool = false
e_bool_one = true == 1 // const bool = false
// e_bool_mt = false == 'string' // ERROR
ne_bool = true != false // const bool = true
// gt_bool = true > false // ERROR
// gte_bool = true >= true // ERROR
// color
e_color = [Link] == [Link] // const bool = true
e_hex = #4CAF50 != #4CAF50 // const bool = false
e_hex_color = [Link] == #4CAF50 // const bool = true
e_comp = e_color == e_hex // const bool = false
e_hex_2 = #4caf50 == #4CAF50 // const bool = false
// plot(e_hex ? 1 : 0, color=#4CAF50, title='#4CAF50')
// plot(e_comp ? 1 : 0, color=#4CAF50, title='#4CAF50')
// plot(e_hex_2 ? 1: 0, color=#4caf50, title='#4caf50')

// label & line


var e_line_1 = [Link](0, 0, 0, 0) // These two lines are not the same
var e_line_2 = [Link](0, 0, 0, 0) // at least when being compared with ==
e_line_bool = e_line_1 == e_line_2 // series[bool] or [false] = series[line]
== series[line]
// Same for label.

plot(e_line_bool ? 1 : 0)

Common questions

Powered by AI

Pine Script allows comparison of color values using the == and != operators. An example from the document shows that a color defined by name (e.g., color.green == color.green) evaluates to true, suggesting identical named colors are seen as equal . However, different hex cases (e.g., #4CAF50 == #4caf50) are considered unequal due to case sensitivity in hex representation . This showcases that while named colors are a straightforward comparison, hex string comparisons require precise casing.

Pine Script considers lines or labels equal only when they point to the same instance in memory, not when they are structurally identical but differently instantiated (e.g., even if var e_line_1 has the same coordinates as var e_line_2, they are not equal). This affects graphical representations by preventing assumptions about shared instances from affecting their rendering or behavior, allowing distinct manipulation and tracking of similar yet independent elements.

In Pine Script, comparison operators are used to evaluate relationships between values, yielding Boolean outputs. For numbers, operators like == or != check equality or inequality (e.g., 2 == 2 is true). Strings can be compared for equality but not for order, leading to errors if such operations are attempted (e.g., "a" == "a" is true, but "a" > "b" causes an error). Series comparison involves comparing current values with constants or other series (e.g., close < 2 results in a series of Booleans). The behavior of comparison depends heavily on data types involved.

Pine Script produces errors when attempting to compare booleans using greater or less than operators (e.g., true < false causes an error). This restriction enforces logical comparisons strictly to equality and inequality checks, aligning with the nature of boolean data that inherently lacks ordinal value. It restricts developers from performing nonsensical logical operations, ensuring logical integrity within the code by preventing operations that misinterpret the binary nature of boolean values.

In Pine Script, 'na' (not available) introduces special considerations in comparison operations. When compared to any number or series, operations like < or > with 'na' always yield false as 'na' represents an undefined or missing value (e.g., 2 < na is false). The implication is that scripts using these comparisons must account for the presence of 'na' as it may be associated with missing data points, requiring conditional checks or filtering to maintain robust data handling and prevent logical errors.

Pine Script addresses type mismatch errors by strictly enforcing comparisons only between compatible types. Attempting operations between incompatible types, such as using comparison operators on booleans or mixed types with strings (e.g., e_bool_mt = false == 'string') results in errors . This prevents erroneous logic and enforces type congruity in scripting, ensuring that the developers explicitly manage type conversions or handle each type's data structure appropriately before comparison.

Pine Script handles equality by type rather than value representation specifics, such as hex casing. When evaluating colors, the script finds #4CAF50 != #4caf50 false due to case sensitivity, despite the visual color equivalence in hexadecimal . This impacts the application by necessitating consistency in type representation (hex casing), affecting areas where uniform visual elements may need computational logic distinction, such as in UI theming or conditionally rendering visuals based on these comparisons.

The handling of series-specific comparison operators in Pine Script significantly influences its application in financial data analysis by allowing dynamic assessment across time series. This involves comparing current data points with either constants or moving series values to generate Boolean results that reveal trends or signal triggers (e.g., close < 2 returns dynamic state over time). Such capabilities enable complex analytical strategies in charting and trading scripts, allowing users to build conditional logic that reacts adaptively to market trajectory, enhancing actionable insights from historical data trends.

Pine Script defines equality between objects like labels or lines through structural comparison rather than reference equality. Two geometrically identical lines are not considered equal if instantiated separately (e.g., var e_line_1 != var e_line_2, even if their coordinates match). This highlights that object identity checks focus on the instantiation and instance identity rather than purely structural factors, suggesting significant implications for tracking and comparing such elements in scripts.

Comparing series in Pine Script presents the challenge of handling sequences instead of static values. Series, such as close or high, represent time-dependent data points, requiring comparisons that resolve to true or false for each element (e.g., close < 2 results in a time series of true/false values). A solution includes evaluating these over time to get dynamic Boolean arrays, adopting series-specific comparisons instead of singular value checks, ensuring that logical consistency is maintained over potentially varying data.

You might also like