Understanding math.abs() Function in Pine Script

So you're working with Pine Script and keep seeing this math.abs()
function pop up everywhere? Let me break it down for you in plain English.
What does math.abs() actually do?
Think of math.abs()
as a function that just removes the negative sign from any number. That's it. If you give it -5, it spits out 5. If you give it 10, it stays 10. It's basically asking "how far is this number from zero?" without caring about direction.
The syntax is super simple:
result = math.abs(someNumber)
That's literally all there is to it.
Why would you even need this?

Here's the thing - when you're analyzing price movements, you often care about how much something moved, not necessarily which direction. Let me give you some real examples:
Price swings: Say you want to know if today's price change was significant. You don't care if it went up $3 or down $3 - both are big moves. Using math.abs(close - close[1])
gives you the size of the move regardless of direction.
Volatility stuff: When measuring how "jumpy" a stock is, you want to look at the size of all the moves. A stock that goes +2%, -3%, +1%, -2% is more volatile than one that goes +0.5%, -0.3%, +0.2%, -0.1%. The absolute values help you see that.
Building indicators: Sometimes your calculations give you negative numbers, but you need everything to be positive for the next step. math.abs()
is your friend here.
A real example that actually makes sense
Let's say you want to spot when a stock makes unusually big moves. Here's how you might do it:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Pineify
//@version=6
indicator("Big Move Spotter", overlay=true)
// Let the user decide what counts as a "big" move
bigMoveThreshold = input(2.0, title="Big Move Threshold (%)")
// Calculate today's price change as a percentage
priceChangePercent = (close - close[1]) / close[1] * 100
// Get the absolute value - we don't care if it's up or down
absoluteChange = math.abs(priceChangePercent)
// Mark it on the chart if it's a big move
plotshape(
series = absoluteChange > bigMoveThreshold,
location = location.belowbar,
color = color.orange,
style = shape.triangleup,
size = size.small,
title = "Big Move!"
)
// Also plot the actual percentage for reference
plot(absoluteChange, title="Daily Move %", color=color.blue)
This script will put a little orange triangle under any bar where the stock moved more than your threshold (let's say 2%) in either direction.
When I use this function
I find myself reaching for math.abs()
most often when:
- Comparing the size of moves between different stocks
- Building volatility indicators
- Creating alerts for significant price changes
- Smoothing out oscillators that bounce between positive and negative
The bottom line
math.abs()
is one of those simple functions that you'll use more than you think. It's not fancy, but it solves a common problem: sometimes you just need to know "how much" without caring about "which way."
Once you start using it, you'll probably find yourself throwing it into scripts all the time. It's like having a tool that just makes negative numbers positive - simple, but surprisingly useful.

If you're building more complex indicators and want a visual way to put them together, check out Pineify. It's pretty handy for creating indicators without having to write everything from scratch.