Home » Array Functions » Understanding the array.every() Function in Pine Script

Understanding the array.every() Function in Pine Script

Photo of author
Published on

Let’s delve into the array.every() function, exploring its syntax, usage, and practical applications array.every() Function in Pine Script

Syntax

The syntax of the array.every() function is straightforward:

array.every(arrayId) → series bool

Arguments:

  • arrayId (array): This argument represents the array object that the function will process. Although primarily designed for arrays of boolean values, the function is versatile and can also handle arrays of integers (int) and floating-point numbers (float), interpreting zero values as false and non-zero values as true.

How It Works

The array.every() function evaluates each element within the specified array. It returns a boolean value: true if all elements of the array meet a certain condition (i.e., all are true for boolean arrays, or non-zero for int and float arrays), and false otherwise. This functionality is particularly useful when you need to confirm that a dataset entirely satisfies a given criterion.

Practical Application

Consider a scenario where you’re developing a trading strategy that requires all conditions within a set period to be met before executing a trade. Using array.every() allows you to streamline this process by evaluating all conditions at once.

Example

Suppose we have an array of boolean values representing whether certain conditions are met over the last 5 days:

//@version=5
indicator("My Script", overlay=true)

// Example conditions over the last 5 days
conditionDay1 = close > open
conditionDay2 = close > open[1]
conditionDay3 = close > open[2]
conditionDay4 = close > open[3]
conditionDay5 = close > open[4]

// Creating an array of conditions
conditionsArray = array.new_bool(5, false)
array.set(conditionsArray, 0, conditionDay1)
array.set(conditionsArray, 1, conditionDay2)
array.set(conditionsArray, 2, conditionDay3)
array.set(conditionsArray, 3, conditionDay4)
array.set(conditionsArray, 4, conditionDay5)

// Checking if all conditions are met
allConditionsMet = array.every(conditionsArray)

plotshape(series=allConditionsMet, location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small)
Example

In this example, we’re checking whether the closing price was higher than the opening price for each of the last 5 days. If all conditions are met, a green triangle is plotted below the bar.

Walkthrough:

  1. Indicator Declaration: The indicator function is used to declare a new indicator named “My Script” with the overlay=true parameter, which means this indicator will be plotted directly on the price chart.
  2. Condition Definitions: The code defines five boolean conditions (conditionDay1 to conditionDay5). Each condition checks if the closing price (close) of the current session is greater than the opening price (open) of the current session and the four previous sessions, respectively. This is a basic way to check for bullish sessions.
  3. Array of Conditions: An array named conditionsArray is created to store the boolean values of the five conditions. The array is initially filled with false values for all five elements.
  4. Setting Array Values: The array.set function updates the conditionsArray with the actual values of the conditions (conditionDay1 to conditionDay5) for each corresponding index (0 to 4).
  5. Check All Conditions: The array.every(conditionsArray) function checks if all elements in conditionsArray are true, i.e., if all the specified conditions over the last 5 days are met. It returns true if all conditions are met, otherwise false.
  6. Plotting a Shape: The plotshape function plots a green triangle below the bar if all conditions are met (allConditionsMet is true). This visual cue helps users quickly identify the trading sessions where the closing price was higher than the opening price for five consecutive days.

Key Features and Takeaways

  • Function Useability: The array.every() function can handle boolean, integer, and float arrays, offering versatility in evaluating conditions.
  • Syntax: Simple and intuitive, allowing for easy implementation in scripts.
  • Application: Ideal for strategies requiring all conditions to be met within a dataset, enhancing decision-making processes.

The array.every() function is a potent tool in Pine Script, offering a concise method to evaluate multiple conditions simultaneously. By understanding and utilizing this function, developers can create more efficient and effective trading scripts on the TradingView platform.

Leave a Comment