genericStateIterate

Syntax

genericStateIterate(X, initial, window, func)

Arguments

X can be column(s) from the input table, or the calculation results by applying a vector function to the column(s). You can set X to [] to leave it unspecified; or use a tuple to specify multiple columns for X.

initial is the column that fills the result of the initial window in the output table. It can be a column from the input table, or the calculation results by applying a vector function to it. The initial window is [1, window], measured by the number of records.

window is an integer greater than 1 that specifies the window size.

func is a stateless user-defined function with one scalar as the return value. Arguments passed to func are as follows:

  • The first argument is the data used for iteration;

  • Then followed by columns specified in X.

  • [Optional] Other fixed constants to be passed to func. In this case, you can fix the arguments with partial application.

Details

This function performs calculation with count-based sliding windows iteratively.

Suppose X is specified as [X1, X2, …, Xn], column “factor” in the output table holds the calculation results, column “initial” is the initial column, window is set to “w”, and the iterate function is “func”.

For the k-th record, the calculation rule is:

  • k <= w:factor[k] = initial[k]

  • k > w:factor[k] = func(factor[(k-w):k], X1[k], X2[k], … , Xn[k])

Note that when a pair is used to indicate index, the right boundary is not inclusive, i.e., the range of (k-w):k is [k-w, k).

Example

// define a function
$ def myfunc(x, w){
$         re = sum(x*w)
$     return re
$ }

$ dateTime = 2021.09.09T09:30:00.000 2021.09.09T09:31:00.000 2021.09.09T09:32:00.000 2021.09.09T09:33:00.000 2021.09.09T09:34:00.000
$ securityID = `600021`600021`600021`600021`600021
$ volume = 310 280 300 290 240
$ price = 1.5 1.6 1.7 1.6 1.5
$ t = table(1:0, `dateTime`securityID`volume`price, [TIMESTAMP, SYMBOL, INT, DOUBLE])
$ tableInsert(t, dateTime, securityID, volume, price)
$ output = table(100:0, `securityID`dateTime`factor1, [SYMBOL, TIMESTAMP, DOUBLE])

$ engine = createReactiveStateEngine(name="test", metrics=[<dateTime>, <genericStateIterate(volume,price,3,myfunc{,})>], dummyTable=t, outputTable=output, keyColumn=`SecurityID, keepOrder=true)
$ engine.append!(t)
$ dropAggregator(`test)
securityID dateTime factor1
600021 2021.09.09T09:30:00.000 1.5
600021 2021.09.09T09:31:00.000 1.6
600021 2021.09.09T09:32:00.000 1.7
600021 2021.09.09T09:33:00.000 1,392
600021 2021.09.09T09:34:00.000 334,872

The above example is calculated as follows:

  • As window is set to 3, the first 3 records of factor1 in the initial window take the corresponding values of column price.

  • When the 4th record arrives, the initial window [1.5, 1.6, 1.7] and the current value of volume 290 are used for iteration. The result of myfunc([1.5, 1.6, 1.7], 290) is 1392.

  • When the 5th record arrives, the previous [1.6, 1.7, 1392] and the current value of volume 240 are used for iteration. The result of myfunc([1.6, 1.7, 1392], 240) is 334872.

The calculation process is preceded in the same way if data ingestion continues.

Related function: genericTStateIterate