Home » Technical Analysis Functions » Understanding ta.dev() Function in Pine Script

Understanding ta.dev() Function in Pine Script

Photo of author
Published on

Pine Script offers a range of built-in functions that simplify the process of creating complex calculations. One such function is ta.dev(), which measures the deviation of a data series from its simple moving average (SMA) over a specified period. This article will delve into the ta.dev() function, explaining its syntax, arguments, and providing a practical example.

Syntax of ta.dev()

The ta.dev() function in Pine Script is used to calculate the average deviation of a series from its simple moving average (SMA). Its syntax is as follows:

ta.dev(source, length) → series float


  • source (series int/float): This is the series of values that ta.dev() will process. It can be any series of prices or indicators.
  • length (series int): This specifies the number of bars to consider for the calculation. It determines the length of the SMA and the range over which the deviation is computed.


Below is an example of how to use the ta.dev() function in Pine Script to plot the deviation of the close price from its 10-bar SMA.

indicator("ta.dev example")
plot(ta.dev(close, 10))

Custom Implementation:

For a deeper understanding of what ta.dev() does under the hood, let’s examine a custom implementation named pine_dev.

pine_dev(source, length) =>
    mean = ta.sma(source, length)
    sum = 0.0
    for i = 0 to length - 1
        val = source[i]
        sum := sum + math.abs(val - mean)
    dev = sum / length
plot(pine_dev(close, 10))


  • Calculating the Mean: First, we calculate the simple moving average (SMA) of the source series over the specified length using ta.sma(source, length). This serves as our mean around which we measure deviation.
  • Summing Deviations: We initialize a variable sum to zero. Then, using a for loop, we iterate over each bar in the specified length range, calculating the absolute difference between the source value at each bar and the mean. These differences are accumulated in sum.
  • Calculating Average Deviation: Finally, the sum of deviations is divided by the length to find the average deviation, which is stored in dev.

Key Features and Takeaways

  • The ta.dev() function is a built-in Pine Script function for measuring the average deviation of a series from its simple moving average over a specified period.
  • This function can be used to gauge the consistency of price movements or the variability of an indicator from its average.
  • A custom implementation, like pine_dev, demonstrates the underlying calculation, reinforcing the concept of averaging deviations for a given length.
  • Understanding both the built-in ta.dev() and the custom pine_dev function can enhance your Pine Script programming skills, especially in creating indicators that measure volatility or consistency in data series.

By exploring both the built-in ta.dev() function and a custom implementation, you gain insights into the versatility of Pine Script for financial analysis and indicator development.

Leave a Comment