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

Understanding the math.avg() Function in Pine Script

Photo of author
Published on

In this article, we will explore the math.avg() function, covering its syntax, usage, and practical applications in Pine Script programming.

Syntax and Overloads

The math.avg() function can be used in two primary ways, returning either a simple float or a series float, depending on the context and the types of the arguments passed to it. The syntax of the function is as follows:


math.avg(numberA, numberB, ...) → simple float
math.avg(numberA, numberB, ...) → series float


  • numberA, numberB, …: These are a sequence of numbers (simple int/float) that you want to calculate the average of. The function is flexible, allowing you to pass any number of arguments for the calculation.


  • Average: The function returns the average of the input numbers. The return type can be a simple float or a series float, depending on whether the inputs are static numbers or dynamic series.

Practical Example

To illustrate how the math.avg() function works in practice, let’s look at a simple example. Suppose we want to calculate the average of three dynamic values that represent different moving averages of a stock’s price.

Example Code

indicator("My Average Calculator", overlay=true)

// Defining three moving averages
fastMA = ta.sma(close, 10)
mediumMA = ta.sma(close, 20)
slowMA = ta.sma(close, 50)

// Calculating the average of the three moving averages
averageMA = math.avg(fastMA, mediumMA, slowMA)

plot(averageMA, title="Average MA", color=color.blue)

Code Explanation

  1. Indicator Declaration: We start by declaring a new indicator named “My Average Calculator” that will be plotted over the price chart (overlay=true).
  2. Moving Averages Calculation: We calculate three simple moving averages (SMAs) of the stock’s closing price over different periods: 10, 20, and 50 days. These are stored in fastMA, mediumMA, and slowMA respectively.
  3. Average Calculation: We use the math.avg() function to calculate the average of the three moving averages. This demonstrates how math.avg() can take a series of values as input and compute their average.
  4. Plotting: Finally, we plot the result (averageMA) on the chart with the title “Average MA” and set the color to blue.

Key Features and Takeaways

  • Function Usability: math.avg() can handle both simple and series float values, making it highly versatile for various financial calculations.
  • Syntax Flexibility: It accepts any number of arguments, allowing for the dynamic calculation of averages based on variable inputs.
  • Application: Primarily used in technical analysis, such as averaging multiple indicators to create composite signals or smoothing data points.

Understanding and utilizing the math.avg() function in Pine Script enhances your capability to perform complex mathematical calculations with ease, thereby enriching your trading strategies and technical analysis tools.

Leave a Comment