DolphinDB: How to set a threshold for cumulative calculation?

19 Views Asked by At

I want to create a column “X“ where the values are cumulative sums of an original column “Y“. However, when a value in “X“ reaches a certain threshold (e.g., an absolute value less than 1,000), the cumulative calculation restarts from the next value in “Y“.

For example, given the original column "Y" with values [1100, 1100, -1300, -10000, 1000], how to get the desired result with values [1100, 2200, 900, -10000, -9000]?

1

There are 1 best solutions below

0
dbaa9948 On

You can first use the cumsum function to cumulatively calculate the sum of the elements in "Y" and use the iif function to set the threshold. Here is an example script:

Y = [1100, 1100, -1300, -10000, 1000]
X = cumsum Y;
X - ffill(prev iif(1000>abs X,X,00)).nullFill(0);