StdDev

Calculates the Standard Deviation indicator.

Accepts the following parameters:

Parameter NameTypeDefault ValueNotes
applyToSeries<Number>
close
-
periodNumber20-

Returns: Series<Number>

StdDev is a built-in function - you can use it directly. Below is an example implementation:

1 function StdDev(applyTo = close, period = 20):
2 series variance:
3 once:
4 average = SMA(applyTo, period)
5 sum = 0
6 for i from 0 to period - 1
7 sum = sum + (applyTo[i] - average) ** 2
8 sum / period
9 then:
10 variance[1] + (applyTo - applyTo[period]) * (applyTo - SMA(applyTo, period) + applyTo[period] - SMA(applyTo, period)[1]) / period
11
12 Math.Abs(variance) // 2