Time-Based Moving Functions (tm-functions)

For most window calculations involving time-series data, it's often required that the windows slide based on the time units. Therefore, DolphinDB introduces the time-based moving functions (tm-functions) for such cases. The tm-functions act similarly with the m-functions when the parameter window is a DURATION value. However, the tm-functions can be used in SQL statements to perform calculations on the columns of a table without requiring an indexed series or matrix.

Introduction

  • Higher-order function tmoving:

    tmoving(func, T, funcArgs, window)

The tm-functions are optimized for their specialized use cases and therefore have a better performance than the higher-order function tmoving().

  • Syntax templates for the tm-functions:

    tmfunc(T, X, window)
    tmfunc(T, X, Y, window)

    Parameters:

    T is a non-strictly increasing vector of temporal or integral type. It cannot contain NULL values.

    X (Y) is a vector of the same size as T.

    window is a scalar of positive integer or DURATION type indicating the size of the sliding window.

Windowing Logic

For the tm-functions, window can have the positive integral or DURATION type. The window size is measured by time.

For each element Ti in T:

  • When T is an integral value, the range of the corresponding window is (Ti - window, Ti]

  • When T is a temporal value, the range of the corresponding window is (temporalAdd(Ti, -window), Ti]

The following example explains how the window slides:

T = [2022.01.01, 2022.01.02, 2022.01.03, 2022.01.06, 2022.01.07, 2022.01.08, 2022.01.10, 2022.01.11]
X = 1..8

print tmsum(T, X, window=3)
// output
[1, 3, 6, 4, 9, 15, 13, 15]