Search code examples
pine-scriptcumulative-sumpine-script-v5

ta.cum(Value) reset on Condition - pinescript


Is there a way to reset the count of ta.cum()?

-Current method ta.cum(Value) reset on X bar_index

-Problem On condition I am setting the "Value" to 0, but when it calls ta.cum(Value) again on the next bar its using the previous cum of all values before, I am looking for a way to reset the cum count


Solution

  • Whereevr you want to reset, you can keep a backup of ta.cum in another variable. And subtract that from actual cumulative value for all subsequent candles. Example below , where i am resetting at every 10th bar

    //@version=5
    indicator("Cumulative reset")
    
    var prvsum=0.0
    sum=ta.cum(close)
    if bar_index%10==0
        prvsum:=sum
    fixedsum=sum-prvsum
    plot(fixedsum)