Home » Mathemtical Functions » Understanding math.abs() Function in Pine Script

Understanding math.abs() Function in Pine Script

Photo of author
Published on

In this article, we’ll dive deep into how to use math.abs() in Pine Script version 5, including its syntax, applications, and a practical example to solidify your understanding.

Syntax of math.abs()

The syntax for math.abs() is straightforward:

absValue = math.abs(x)

Here, x is the number (integer or float) of which you want to find the absolute value. The function returns the absolute value of x, effectively removing any negative sign.

Practical Example

To illustrate the use of math.abs(), let’s create a simple script that highlights price differences exceeding a specified threshold. This example will modify variable names for uniqueness and demonstrate how to apply the math.abs() function.

indicator("Price Difference Highlighter", overlay=true)

thresholdValue = input(5.0, title="Threshold Value")

// Calculating the absolute difference between the current close and the previous close
priceDifference = math.abs(close - close[1])

// Plotting a shape if the price difference exceeds the threshold value
plotshape(series=priceDifference > thresholdValue, location=location.belowbar, color=color.red, style=shape.triangleup, size=size.small, title="Significant Price Difference")


Walkthrough of Each Line

  • //@version=5: Specifies that this script is written for Pine Script version 5, ensuring compatibility with the latest features and syntax.
  • indicator(...): Declares a new indicator with the name “Price Difference Highlighter”. The overlay=true parameter indicates that this indicator will be drawn directly on the price chart, rather than in a separate pane below it.
  • input(5.0, title="Threshold Value"): Creates an input field for the user to specify a threshold value. The default value is set to 5.0. This value will be used to determine what constitutes a significant price difference.
  • priceDifference = math.abs(close - close[1]): Calculates the absolute value of the difference between the current closing price (close) and the closing price of the previous bar (close[1]). This calculation finds the magnitude of the price change without regard to direction (up or down).
  • plotshape(...): This function is used to plot shapes on the chart based on a specified condition. The condition here is priceDifference > thresholdValue, meaning a shape will be plotted whenever the price difference exceeds the user-defined threshold.
  • series=priceDifference > thresholdValue: The series parameter is the condition under which the shape will be plotted. If true, the shape is plotted for that bar.
  • location=location.belowbar: Specifies that the shape should be plotted below the bar.
  • color=color.red: Sets the color of the shape to red.
  • style=shape.triangleup: Chooses the upward-pointing triangle as the shape to plot.
  • size=size.small: Defines the size of the plotted shape as small.
  • title="Significant Price Difference": Gives a title to this particular plot, which can be seen in the indicator’s settings and legend.

Key Features and Takeaways

  • Function Usability: math.abs() is useful for scenarios where you need to normalize negative values, especially when calculating price differences, deviations, or other financial metrics that require positive values for comparison.
  • Syntax and Application: The function requires a single argument and returns the absolute value, making it straightforward to integrate into various calculations and conditions within your scripts.
  • Versatility: This function can be applied in a wide range of trading strategies and indicators, from volatility measurements to custom oscillators, by providing a simple way to handle negative numbers.

Leave a Comment