In this tutorial, we will discuss the ** array.stdev** function in Pine Script, which is used to calculate the standard deviation of an array’s elements. We will go through the syntax of the function, its arguments, and a unique use case example. In the end, we will highlight the key takeaways before concluding the tutorial.

**What is the Array.stdev Function?**

**Definition**

The ** array.stdev** function is used to compute the standard deviation of an array’s elements in Pine Script. It can be used with both float and integer arrays. The standard deviation is a measure of the dispersion or spread of the data in the array. A high standard deviation indicates that the data points are spread out over a large range, while a low standard deviation indicates that they are close together.

**Syntax**

The syntax for the ** array.stdev** function is as follows:

array.stdev(id, biased) → series float array.stdev(id, biased) → series int

**Arguments**

The function takes two arguments:

: The array object for which the standard deviation needs to be calculated.`id (int[]/float[])`

: Determines which estimate should be used. This argument is optional, and the default value is`biased (series bool)`

.`true`

**Remarks**

The ** biased** argument determines the type of estimate used for the calculation. If

**is true, the function calculates the standard deviation using a biased estimate of the entire population. If**

`biased`

**is false, it calculates using an unbiased estimate of a sample.**

`biased`

**Use Case Example**

Now let’s explore a unique use case example that demonstrates how to use the ** array.stdev** function in Pine Script. In this example, we will calculate the standard deviation of the closing prices of the last 10 bars and plot it on the chart.

**Code**

//@version=5 indicator("Standard Deviation of Closing Prices") // Initialize the float array priceArray = array.new_float(0) // Loop through the last 10 closing prices and add them to the array for i = 0 to 9 array.push(priceArray, close[i]) // Calculate the standard deviation of the closing prices stdDev = array.stdev(priceArray, false) // Plot the standard deviation plot(stdDev, title="Standard Deviation", color=color.blue, linewidth=2) label newLable = na if barstate.islast newLable := label.new(x=bar_index, y=stdDev, style=label.style_label_left, color=#E6E6FA, textcolor=color.black, size=size.large, text="Standard Deviation = \n " + str.tostring(stdDev )) label.delete(newLable[1])

**Explanation**

Here is an explanation of each line of the provided code:

: This line sets the version of Pine Script used for the script. In this case, it is version 5.`//@version=5`

: This line defines the title of the indicator displayed on the chart.`indicator("Standard Deviation of Closing Prices")`

: This line initializes an empty float array named`priceArray = array.new_float(0)`

.`priceArray`

: This line starts a loop, iterating through the last 10 closing prices (from 0 to 9).`for i = 0 to 9`

: This line pushes the closing prices of the last 10 bars into the`array.push(priceArray, close[i])`

.`priceArray`

: This line calculates the standard deviation of the closing prices in the`stdDev = array.stdev(priceArray, false)`

using an unbiased estimate (`priceArray`

) and stores the result in the`false`

variable.`stdDev`

: This line plots the standard deviation on the chart with the title “Standard Deviation,” a blue color, and a linewidth of 2.`plot(stdDev, title="Standard Deviation", color=color.blue, linewidth=2)`

: This line initializes a new label variable named`label newLable = na`

and sets its initial value to`newLable`

(not available).`na`

: This line checks if the current bar is the last one on the chart.`if barstate.islast`

: If the current bar is the last one, this line creates a new label with the specified properties (x and y coordinates, style, color, text color, size, and text) and assigns it to the`newLable := label.new(...)`

variable. The text displays the standard deviation value calculated earlier.`newLable`

: This line deletes the previous label (if any) from the chart to ensure that only the label for the current bar’s standard deviation is displayed.`label.delete(newLable[1])`

**Key Takeaways**

- The
function is used to calculate the standard deviation of an array’s elements in Pine Script.`array.stdev`

- The syntax for the function is
.`array.stdev(id, biased)`

- The function takes two arguments –
and`id`

. The`biased`

argument is the array object, and the`id`

argument is a boolean determining which estimate should be used for the calculation. The default value for`biased`

is`biased`

.`true`

- If
is true, the function calculates the standard deviation using a biased estimate of the entire population. If`biased`

is false, it calculates using an unbiased estimate of a sample.`biased`

- The
function can be used in various scenarios, such as calculating the standard deviation of closing prices, as demonstrated in the example provided.`array.stdev`

**Conclusion**

In this tutorial, we have covered the ** array.stdev** function in Pine Script, which is used to compute the standard deviation of an array’s elements. We went through its syntax, arguments, and provided a unique use case example to illustrate its practical applications.

Understanding and using the ** array.stdev** function is essential for traders and analysts who want to calculate the dispersion or spread of their data points. By leveraging this function, you can easily incorporate standard deviation calculations in your trading strategies and technical analysis, allowing you to make more informed decisions based on the volatility of the market.