rolling

Syntax

rolling(func, funcArgs, window, [step=1])

Arguments

func is an aggregate or vectorized function.

funcArgs are the arguments passed to func, which can be vectors, matrices, or tables. It is a tuple if there are more than 1 parameter of func, and all arguments must have the same size (the number of elements of a vector or rows of a matrix).

window is the window size.

step (optional) is the count or interval that windows slide. The default value is 1. If func is a vectorized function, step must be equal to window.

Details

The rolling function applies func to a moving window of funcArgs. It starts calculating when the window size is reached for the first time, then calculates with frequency specified by step.

Similar to the moving function, windows in rolling function are always along rows.

The differences of rolling and moving functions lie in:

  • The func parameter in rolling function supports aggregate or vectorized functions, whereas func in moving function only supports aggregate functions.

  • When func is specified as an aggregate function,

    • step can be specified in rolling function.

    • rolling does not return NULL values of the first (window -1) elements.

Examples

Example 1. When func is a vectorized function:

m = matrix(3 4 6 8 5 2 0 -2, 2 9 NULL 1 3 -4 2 1, NULL 8 9 8 0 1 9 -3)
rolling(cummax, m, 4, 4)
col1 col2 col3
3 2
4 9 8
6 9 9
8 9 9
5 3 0
5 3 1
5 3 9
5 3 9
rolling(cumsum, m, 3, 3)
col1 col2 col3
3 2
7 11 8
13 11 17
8 1 8
13 4 8
15 0 9

Example 2. When func is a aggregate function:

rolling(sum, m, 4)
col1 col2 col3
21 12 25
23 13 25
21 0 18
15 2 18
5 2 7

Calculate the rolling beta of AAPL against the market (SPY) with the moving window of size 10 and frequency of 5.

date=2016.08.01..2016.08.31
date=date[1<=weekday(date)<=5]
aaplRet=0.0177 -0.0148 0.0125 0.0008 0.0152 0.0083 0.0041 -0.0074 -0.0006 0.0023 0.0120 -0.0009 -0.0015 -0.0013 0.0026 -0.0078 0.0031 -0.0075 -0.0043 -0.0059 -0.0011 -0.0077 0.0009
spyRet=-0.0008 -0.0064 0.0029 0.0011 0.0082 -0.0006 0.0006 -0.0025 0.0046 -0.0009 0.0029 -0.0052 0.0019 0.0022 -0.0015 0.0000 0.0020 -0.0051 -0.0007 -0.0019 0.0049 -0.0016 -0.0028
t=table(date, aaplRet, spyRet);
t;
date aaplRet spyRet
2016.08.01 0.0177 -0.0008
2016.08.02 -0.0148 -0.0064
2016.08.03 0.0125 0.0029
2016.08.04 0.0008 0.0011
2016.08.05 0.0152 0.0082
2016.08.08 0.0083 -0.0006
2016.08.09 0.0041 0.0006
2016.08.10 -0.0074 -0.0025
2016.08.11 -0.0006 0.0046
2016.08.12 0.0023 -0.0009
2016.08.15 0.012 0.0029
2016.08.16 -0.0009 -0.0052
2016.08.17 -0.0015 0.0019
2016.08.18 -0.0013 0.0022
2016.08.19 0.0026 -0.0015
2016.08.22 -0.0078 0
2016.08.23 0.0031 0.002
2016.08.24 -0.0075 -0.0051
2016.08.25 -0.0043 -0.0007
2016.08.26 -0.0059 -0.0019
2016.08.29 -0.0011 0.0049
2016.08.30 -0.0077 -0.0016
2016.08.31 0.0009 -0.0028
betas = rolling(beta, [aaplRet, spyRet], 10,5);
dates = rolling(last, date, 10,5);
table(dates, betas);
dates betas
2016.08.12 1.601173
2016.08.19 0.512656
2016.08.26 1.064465

If funcArgs is a matrix with row labels, the row labels are retained in the output.

minBar = 2024.03.08T10:00:00 + 0..9
aaplClose = [170.88,170.88,170.90,171.05,171.18,171.30,171.51,171.49,171.31,171.14]
ibmClose = [150.15,150.18,150.20,150.05,150.18,150.25,150.32,150.30,150.31,150.20]
m = matrix(aaplClose, ibmClose).rename!(minBar, `aapl`ibm)
rolling(func=avg, funcArgs=m, window=5, step=2)
label aapl ibm
2024.03.08T10:00:00 170.978 150.152
2024.03.08T10:00:02 171.188 150.2
2024.03.08T10:00:04 171.358 150.272