Home » Mathemtical Functions » math.max function in Pine Script

math.max function in Pine Script

Photo of author
Published on

One particularly useful function in Pine Script is math.max. This function returns the greatest value out of multiple inputs.

Syntax of math.max

The math.max function has a very flexible syntax that can accommodate multiple arguments of different data types. Here are some basic forms of the function:

  • math.max(number0, number1, ...) → simple int
  • math.max(number0, number1, ...) → simple float
  • math.max(number0, number1, ...) → input int
  • math.max(number0, number1, ...) → input float
  • math.max(number0, number1, ...) → series int
  • math.max(number0, number1, ...) → series float

It’s important to note that the function takes at least two arguments and will return the highest value among these arguments.

How it Works

The math.max function works by scanning through the input arguments and finding the greatest value. For example, if you pass in three numbers like so: math.max(1, 3, 2), the function would return 3 as it is the largest number among the arguments.

Using math.max in Pine Script

Let’s look at a simple code example to understand how math.max works in Pine Script:

//@version=5
indicator("math.max Example", overlay=true)
plot(math.max(close, open), linewidth = 3)
math.max function

Code Explanation

  1. //@version=5 is the version directive that specifies the version of Pine Script to use for the script. This directive must be placed at the very start of the script and no other code or comments should precede it. Here, we are using version 5 of Pine Script.
  2. indicator("math.max Example", overlay=true) creates a new indicator script named “math.max Example”. The overlay=true part means that the output of this script will be drawn directly on the price chart, rather than in a separate pane below it.
  3. plot(math.max(close, open), linewidth = 3) is using the plot function to draw a graph on the chart. The values that are being graphed are the result of math.max(close, open), which calculates the maximum of the close and open prices for each bar on the chart. The linewidth = 3 parameter specifies the thickness of the line drawn on the chart to be 3 pixels.

Key Takeaway

The math.max function in Pine Script is a versatile tool for identifying the maximum value among a set of variables or data series. It can be used for various analytical purposes, such as identifying the highest price or volume in a certain time period.

Conclusion

Understanding how to use math.max function in Pine Script is essential for any trader or analyst who wishes to create their own custom scripts. By using this function, you can obtain the maximum value from multiple inputs, helping you to develop more intricate trading strategies and indicators.

Leave a Comment