Home » Technical Analysis Functions » Arnaud Legoux Moving Average (ALMA) in Pine Script

Arnaud Legoux Moving Average (ALMA) in Pine Script

Photo of author
Published on

In the vast universe of technical indicators, Arnaud Legoux Moving Average, often known as ALMA, holds a unique place. Unlike conventional moving averages, ALMA uses Gaussian distribution as weights, offering an advantageous tradeoff between smoothness and responsiveness. This tutorial will offer an in-depth look at the ta.alma function in Pine Script, showcasing how to use it effectively in your scripts.

What is ta.alma?

ta.alma is a built-in function in Pine Script that calculates the Arnaud Legoux Moving Average. This function has two overloads:

ta.alma(series, length, offset, sigma) → series float
ta.alma(series, length, offset, sigma, floor) → series float

Where:

  • series is the data you want to calculate ALMA for. This could be any series of integer or float values, such as close prices of a stock.
  • length is the number of bars to include in the calculation.
  • offset is a value that controls the tradeoff between smoothness (values closer to 1) and responsiveness (values closer to 0).
  • sigma influences the smoothness of ALMA; the larger the value, the smoother the ALMA.
  • floor is an optional boolean parameter that determines whether the offset calculation is floored before ALMA is calculated. The default value is false.

A Basic ta.alma Example

Here’s a simple example of how to use ta.alma:

//@version=5
indicator("ta.alma", overlay=true)
plot(ta.alma(close, 9, 0.85, 6))

In this script, ta.alma is calculated using the closing price (close), over 9 bars (9), with an offset of 0.85 and a sigma of 6. The result is plotted on the chart overlay.

An Extended Example: Implementing ALMA without ta.alma

While ta.alma is an efficient way to calculate ALMA in Pine Script, it can be instructive to implement the calculation ourselves to better understand how it works. Here is a script that implements ALMA without using ta.alma:

//@version=5
indicator("ta.alma", overlay=true) 
plot(ta.alma(close, 9, 0.85, 6))

// same on pine, but much less efficient
pine_alma(series, windowsize, offset, sigma) =>
    m = offset * (windowsize - 1)
    //m = math.floor(offset * (windowsize - 1)) // Used as m when math.floor=true
    s = windowsize / sigma
    norm = 0.0
    sum = 0.0
    for i = 0 to windowsize - 1
        weight = math.exp(-1 * math.pow(i - m, 2) / (2 * math.pow(s, 2)))
        norm := norm + weight
        sum := sum + series[windowsize - i - 1] * weight
    sum / norm
plot(pine_alma(close, 9, 0.85, 6) , linewidth = 3)
Arnaud Legoux Moving Average

Here, pine_alma is a user-defined function that calculates ALMA. It takes four parameters: series (the series to calculate ALMA for), windowsize (equivalent to length in ta.alma), offset, and sigma. The function calculates m and s based on these parameters, then computes a weight for each bar in the window. These weights are then used to calculate a weighted sum of the series values, which is finally divided by the total weight (norm) to get ALMA.

Key Takeaways

The Arnaud Legoux Moving Average (ALMA) is a versatile and powerful tool that can help you make more informed decisions in trading. The use of ta.alma in Pine Script provides a streamlined way to implement this complex calculation in your scripts. While understanding the underlying algorithm might not be necessary for all traders, it can certainly help you gain a deeper understanding of this tool and how it impacts your trading strategies.

Conclusion

In this tutorial, we’ve introduced you to the ta.alma function in Pine Script, explained its parameters, and shown how to use it in a basic example. We’ve also provided an extended example where we implemented the ALMA calculation without using ta.alma. Whether you’re a seasoned Pine Script developer or just getting started, understanding how to use tools like ALMA effectively can make a big difference in your trading strategies.

Leave a Comment