how to perform factor calculation using sliding windows with elements from different columns?

41 views Asked by At

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?

1

There are 1 answers

0
biggggtomato On BEST ANSWER

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 as msum, you can use the script window(sum, a, -9:-1) + c to sum up the values of "a" in the first 9 rows and then add the value of "c".