Sidechair

math.abs()

Browse the reference

Returns the absolute value of a number: its distance from zero, never negative.

Try it in the editor

math.abs turns a negative number into its positive counterpart and leaves other numbers alone. An int stays an int, and a float stays a float.

It is the usual way to measure the size of a move regardless of direction, for example math.abs(close - open) for the candle body.

Syntax

Pine Script
math.abs(number: simple int) -> simple int
math.abs(number: input int) -> input int
math.abs(number: const int) -> const int
math.abs(number: series int) -> series int
math.abs(number: simple int/float) -> simple float
math.abs(number: input int/float) -> input float
math.abs(number: const int/float) -> const float
math.abs(number: series int/float) -> series float

Arguments

NameTypeDefaultDescription
numbersimple int or input int or const int or series int or simple int/float or input int/float or const int/float or series int/floatrequiredThe number to take the absolute value of.

Returns

simple int or input int or const int or series int or simple float or input float or const float or series float: the number without its sign; na if the input is na.

Remarks

  • math.abs(0) is 0.

Example

Pine ScriptCandle body sizeOpen in editor
//@version=6
indicator("Body size")
body = math.abs(close - open)
plot(body, "Body", color = color.purple, style = plot.style_columns)
plot(ta.sma(body, 20), "Average body", color = color.orange)

See also

Runs on Sidechair