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:
id (int[]/float[])
: The array object for which the standard deviation needs to be calculated.biased (series bool)
: Determines which estimate should be used. This argument is optional, and the default value istrue
.
Remarks
The biased
argument determines the type of estimate used for the calculation. If biased
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.
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:
//@version=5
: This line sets the version of Pine Script used for the script. In this case, it is version 5.indicator("Standard Deviation of Closing Prices")
: This line defines the title of the indicator displayed on the chart.priceArray = array.new_float(0)
: This line initializes an empty float array namedpriceArray
.for i = 0 to 9
: This line starts a loop, iterating through the last 10 closing prices (from 0 to 9).array.push(priceArray, close[i])
: This line pushes the closing prices of the last 10 bars into thepriceArray
.stdDev = array.stdev(priceArray, false)
: This line calculates the standard deviation of the closing prices in thepriceArray
using an unbiased estimate (false
) and stores the result in thestdDev
variable.plot(stdDev, title="Standard Deviation", color=color.blue, linewidth=2)
: This line plots the standard deviation on the chart with the title “Standard Deviation,” a blue color, and a linewidth of 2.label newLable = na
: This line initializes a new label variable namednewLable
and sets its initial value tona
(not available).if barstate.islast
: This line checks if the current bar is the last one on the chart.newLable := label.new(...)
: 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 thenewLable
variable. The text displays the standard deviation value calculated earlier.label.delete(newLable[1])
: 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.
Key Takeaways
- The
array.stdev
function is used to calculate the standard deviation of an array’s elements in Pine Script. - The syntax for the function is
array.stdev(id, biased)
. - The function takes two arguments –
id
andbiased
. Theid
argument is the array object, and thebiased
argument is a boolean determining which estimate should be used for the calculation. The default value forbiased
istrue
. - If
biased
is true, the function calculates the standard deviation using a biased estimate of the entire population. Ifbiased
is false, it calculates using an unbiased estimate of a sample. - The
array.stdev
function can be used in various scenarios, such as calculating the standard deviation of closing prices, as demonstrated in the example provided.
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.