Suppose there is a table with 6 columns: “date”, “symbol”, “a”, “b”, “c”, and “d”. I intend to calculate a factor with a sliding window of length 10, grouped by “symbol“.
I intend that the first 9 rows of each sliding window take the values of “a“ and “b“, while the last row takes the values of “c“ and “d“. How to achieve this purpose?
For self-defined factors that cannot be calculated using a built-in function, we can only create a user-defined function to define the calculation rules within the window, and then use the higher-order function
moving
to calculate the sliding window. For common factors that can be calculated using a built-in function, such asmsum
, you can use the scriptwindow(sum, a, -9:-1) + c
to sum up the values of "a" in the first 9 rows and then add the value of "c".