Sidechair

math.max()

Browse the reference

Returns the largest of the numbers you pass; na if any of them is na.

Try it in the editor

math.max compares two or more numbers on the current bar and returns the biggest. It accepts any count of arguments, in any mix of ints and floats, and the result type follows the arguments.

It compares values side by side on one bar. To find the largest value of a series across several bars, use ta.highest, and for the largest value ever seen use ta.max.

Syntax

Pine Script
math.max(number0, number1, ...: const int) -> const int
math.max(number0, number1, ...: const int/float) -> const float
math.max(number0, number1, ...: simple int) -> simple int
math.max(number0, number1, ...: simple int/float) -> simple float
math.max(number0, number1, ...: input int) -> input int
math.max(number0, number1, ...: input int/float) -> input float
math.max(number0, number1, ...: series int) -> series int
math.max(number0, number1, ...: series int/float) -> series float

Arguments

NameTypeDefaultDescription
number0, number1, ...const int or const int/float or simple int or simple int/float or input int or input int/float or series int or series int/floatrequiredTwo or more numbers to compare.

Returns

const int or const float or simple int or simple float or input int or input float or series int or series float: the largest argument.

Remarks

  • math.max(x, 0) clips negative values to zero.
  • If any argument is na, the result is na.

Example

Pine ScriptClip a series to zeroOpen in editor
//@version=6
indicator("Positive part")
gain = math.max(close - close[1], 0)
loss = math.max(close[1] - close, 0)
plot(gain, "Gain", color = color.green, style = plot.style_columns)
plot(-loss, "Loss", color = color.red, style = plot.style_columns)

See also

Runs on Sidechair