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

Understanding the array.max() Function in Pine Script

Photo of author
Published on

In this article, we dive deep into the workings of the array.max() function in Pine Script. This function is essential for extracting maximum values from an array, which can be particularly useful in data analysis, filtering, and decision-making processes in trading algorithms.


The array.max() function comes with multiple overloads to accommodate different needs:

  1. array.max(id) → series float
  2. array.max(id) → series int
  3. array.max(id, nth) → series float
  4. array.max(id, nth) → series int


  • id (array): This is the array from which the maximum value is to be extracted. It can be an array of integers (int) or floating-point numbers (float).


Let’s explore a practical example to understand how array.max() functions:

indicator("array.max Example")
b = array.from(5, -2, 0, 9, 1)
secondHighest = array.max(b, 2) // Returns 1

In this snippet, we create an array b with a mix of positive and negative integers. We then utilize array.max(b, 2) to find the second highest value in the array, which in this case is 1, and plot this value on the chart.

Walkthrough of Code

  • b = array.from(5, -2, 0, 9, 1): Initializes an array b with specified elements.
  • secondHighest = array.max(b, 2): This line finds the second highest value in the array b. The 2 here indicates that we’re interested in the second highest value, not just the absolute maximum.
  • plot(secondHighest): Plots the value of secondHighest on the TradingView chart, visually representing the second highest value in the array.


The array.max() function is versatile, capable of returning:

  • The greatest value in the array when called without the second argument.
  • The nth greatest value in the array when the second argument is provided, enabling more nuanced data analysis and manipulation.

Key Features

  • Function Usability: The array.max() function can be used with both integer and floating-point arrays, making it a versatile tool for various data analysis tasks.
  • Syntax Flexibility: With its overloads, the function can accommodate simple maximum value extraction or more complex scenarios requiring the nth maximum value.
  • Application in Trading Strategies: This function is invaluable for analyzing price data, indicator values, or any numerical array to extract maximum values for decision-making processes.


  • The array.max() function is a powerful tool in Pine Script for extracting maximum values from an array.
  • Its versatility and simplicity make it suitable for a wide range of applications, from data analysis to complex trading strategy development.
  • Understanding how to use array.max() effectively can enhance your scripting and analytical capabilities on the TradingView platform.

Incorporate this function into your scripts to unlock more sophisticated analysis and strategy development possibilities.

Leave a Comment