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

Understanding the math.sign() Function in Pine Script

Photo of author
Published on

This article provides an in-depth look at the math.sign() function, including its syntax, overloads, and practical applications in Pine Script.

Syntax and Overloads

The math.sign() function can be applied in various contexts within Pine Script, making it a flexible function for different data types. Here’s an overview of its syntax and overloads:

  • Syntax: math.sign(number) → const float
  • Overloads:
  • math.sign(number) → input float
  • math.sign(number) → simple float
  • math.sign(number) → series float


  • number (const int/float): This argument specifies the number for which the sign will be determined. It can be a constant integer or float, an input from the user, a simple floating-point number, or a series of floating-point numbers.


  • The function returns the sign of the argument as a constant float. Specifically:
  • 0 if the number is zero
  • 1.0 if the number is greater than zero
  • -1.0 if the number is less than zero

Practical Example

To illustrate the use of math.sign(), consider a simple scenario where we want to categorize the daily price change of a stock into positive, negative, or neutral movements:

indicator("Daily Price Change Sign", overlay=true)

// Example price change calculation (current close price - previous close price)
priceChange = close - close[1]

// Determine the sign of the price change
priceChangeSign = math.sign(priceChange)

// Plotting the sign on the chart for visualization
plot(priceChangeSign, color=color.new(color.blue, 0), linewidth=2)


  • Indicator Declaration:
    • indicator("Daily Price Change Sign", overlay=true): This line declares a new indicator named “Daily Price Change Sign”. The overlay=true parameter specifies that the indicator will be plotted directly on the price chart, overlaying the price data.
  • Price Change Calculation:
    • priceChange = close - close[1]: This line calculates the daily price change by subtracting the previous day’s closing price (close[1]) from the current day’s closing price (close). The result is a positive number if the price has increased, negative if it has decreased, and zero if there’s no change.
  • Determining the Sign of the Price Change:
    • priceChangeSign = math.sign(priceChange): Here, the math.sign() function is used to determine the sign of the priceChange value calculated in the previous step. The function returns:
      • 1 if the price change is positive (indicating an increase in price),
      • -1 if the price change is negative (indicating a decrease in price),
      • 0 if there is no change in price.
  • Plotting the Sign on the Chart for Visualization:
    • plot(priceChangeSign, color=color.new(color.blue, 0), linewidth=2): This line plots the sign of the daily price change (priceChangeSign) on the chart. The plot uses a blue color (color.blue) with no transparency (0 in color.new()). The linewidth=2 parameter makes the plot line thicker for better visibility.

Key Features and Takeaways

  • Function Versatility: math.sign() can process various data types, including constants, inputs, simple floats, and series floats.
  • Usefulness: It is particularly useful for conditional checks and direction analysis in trading strategies.
  • Syntax and Application: The function is straightforward, requiring only one argument, making it easy to integrate into Pine Script code.
  • Practical Applications: From trend analysis to conditional strategy triggers, math.sign() facilitates numerous financial analysis and trading operations.

In summary, the math.sign() function is an essential component of Pine Script, offering simplicity and versatility for financial analysis. Whether you’re assessing market trends or developing complex trading algorithms, understanding and utilizing this function can significantly enhance your scripting capabilities in Pine Script.

Leave a Comment