series(var value, int length) : vars

Creates a time series of the given length with the given value, and returns the pointer to the series. A series is a var array that contains the history of the variable. It is normally used by indicators or script functions. Series can be static or dynamic. Dynamic series are automatically shifted at every time frame, so that every element corresponds to a bar or a time period; the [0] element to the value at the current bar or time frame, the [1] element to the value from one bar or time frame ago, and so on.


value Optional data value of the series. The series is initially filled with this value, otherwise 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 shifted by script with the shift function.


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


vars Closes = series(priceClose()); defines a series with the length of the LookBack period that contains the close prices of the current asset.


This variable of type var* can be set to a memory area that will then be used for the next series call. After a series call it is set to the returned pointer. This allows to use static or pre-filled buffers for a series.



// 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);

// using arrays of series
vars MySeriesArray[3]; // declars an array of series 
for(i=0; i<3; i++) MySeriesArray[i] = series();
(MySeriesArray[0])[0] = 123; // access the first element of the first series. Mind the parentheses!

See also:

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