series(var value, int length) : vars

Creates a series of the given length, fills it with the given value, and returns the pointer to the series. A series is an array that contains the history of a variable. It is normally used by indicators or script functions. Every element of the array normally corresponds to a bar or time frame in the price chart: the [0] element to the most recent bar, the [1] element to one bar / time frame ago and so on.


value Optional data value of the series. When omitted, the series is filled with 0.
length Optional number of elements of the series; must not change once set. When omitted or 0, the series gets the length of LookBack. A negative number allocates a static series that can be used like a normal var array and is not shifted at every time frame.


Pointer to the var array (the vars type is just a var* pointer).


vars Close = series(priceClose()); returns a series that contains the close prices of the current asset.



// create a series with the high-low price differences
vars PriceRange = series(priceHigh()-priceLow());
// compare the current range with the range from 3 bars ago
if(PriceRange[0] > PriceRange[3])
// calculate a 20-bar Simple Moving Average containing the price differences from 5 bars ago
var Average5 = SMA(PriceRange+5,20);

// wrong use of conditional series
if(priceClose() > Threshold) {
  vars X = series(priceClose()); // error message!
  vars Y = series(SMA(X,100)); // error message!

// correct use of conditional series
vars X = series(), Y = series();
if(priceClose() > Threshold) {
  X[0] = priceClose(); // ok!
  Y[0] = SMA(X,100);

See also:

price, sort, rev, diff, shift ► latest version online