Home » Mathemtical Functions » math.sum Function in Pine Script

math.sum Function in Pine Script

Photo of author
Published on

In the world of Pine Script, one function that often proves to be crucial for performing complex calculations is the math.sum function. This in-built function is a vital tool for traders and investors who leverage Pine Script for analyzing financial data. Before we delve deeper into how to use this function, let’s clarify what it does.

What is the math.sum function?

Simply put, the math.sum function returns the sliding sum of the last ‘y’ values of ‘x’. Here’s the syntax for this function:

math.sum(source, length) → series float

It has two arguments:

  • source (series int/float): This refers to the series of values to process.
  • length (series int): This is the number of bars (length).

The function returns the sum of the ‘source’ for ‘length’ bars back.

Implementing math.sum in Pine Script

Basic Syntax

Implementing math.sum in your Pine Script code involves passing the two arguments to the function. In a simplified form, it might look something like this:

sumOfLastN = math.sum(close, 14)

In the example above, the ‘source’ is the ‘close’ price, and the ‘length’ is 14. So, this line of code would return the sum of the closing prices for the last 14 bars.

Unique Use Case: Calculating the Simple Moving Average (SMA)

The math.sum function can be extremely versatile, depending on how you use it. Let’s look at a unique case where we can use this function to calculate the Simple Moving Average (SMA), a common indicator used in technical analysis.

Here’s the Pine Script code to calculate a 14-period SMA:

indicator('My Script', overlay=true)
length = 14
sum = math.sum(close, length)
sma = sum / length
plot(sma, color=color.new(color.red, 0))

Line-by-Line Explanation

Here’s the explanation of each line of your provided Pine Script code, written with Pine Script’s version 5:

Line 1: //@version=5

This directive tells Pine Script that you are using version 5. It should always be the first line of your script. This newer version may have additional features and functionalities compared to previous versions.

Line 2: indicator('My Script', overlay=true)

This function sets the title of the script (‘My Script’) and declares that the output should be displayed on the price chart. The overlay=true attribute means that your plotted output will overlay on the price chart.

Line 3: length = 14

Here, a variable named length is defined, and it’s set to 14. This variable is used in the next line to define the number of periods for the math.sum function, essentially setting the scope of our summation.

Line 4: sum = math.sum(close, length)

This line calculates the sum of the closing prices for the last 14 bars (as specified by length). This sum is then stored in the sum variable.

Line 5: sma = sum / length

This line calculates the Simple Moving Average (SMA) by dividing the sum of the closing prices by the length (14 in this case). The result is stored in the sma variable.

Line 6: plot(sma, color=color.new(color.red, 0))

Finally, this line plots the SMA on the chart. The color.new(color.red, 0) function sets the color of the plot line to red. The zero in this function means the color has full transparency (0% opacity).

As you can see, even though the script’s version is updated, the underlying logic of the SMA calculation remains the same. In the end, the crucial understanding here is how each line contributes to the overall goal of calculating and plotting the SMA.

Key Takeaway

The math.sum function in Pine Script is a powerful tool for performing calculations over a specific number of bars or a defined length. Its flexibility and ease-of-use make it an essential function for script developers. As showcased in the SMA example, the function can even serve as a stepping stone to more complex technical indicators.


The utility of the math.sum function in Pine Script is vast, spanning from simple summations to the calculation of more intricate metrics like the SMA. Understanding its usage and capabilities is paramount for those who wish to leverage Pine Script to its fullest. Always remember, effective usage of such functions can significantly enhance your trading algorithms, thus potentially improving your trading outcomes. Happy coding!

Leave a Comment