Average ( int length);

Default values:

length: 12


Returns the average value of a set of data for the last length bars. If the length of the data set is not specified, the default value is used. See the following example to learn how the average is calculated.

Input parameters

Parameter Default value Description
data - Defines data for which the average is found.
length 12 Defines period on which the average value is found.

Example 1

script AverageTS {
input data = close;
input length = 12;
plot AverageTS = Sum(data, length) / length;

input price = close;
input length = 12;
plot SMA1 = Average(price, length);
plot SMA2 = AverageTS(price, length);

The example plots an average value using the thinkScript® implementation called AverageTS and the built-in function. Since both the implementations produce the same result the plots coincide with each other forming a single curve.

Example 2

plot SMA = Average(close, 20);

The example displays the moving average for the last 20 closing prices.