SumSince vs HighestSince/LowestSince

While debugging some AFL today I noticed that SumSince() finds the sum excluding the bar on which the Expression was true. However, HighestSince() and LowestSince() find the max and min including the bar on which the Expression was true. Is there a reason for this difference in approach?


IMO, most of the time you want to sum an array after an event but not including the event.

E.g. in below code you would have Buy signal at Close and now you would want to count occurrences of some array since that Buy at Close triggered e.g. then count another Cross()... so that the count starts after the Buy signal (which it does)

Buy = Cross(C, MA(C,20));
BuyPrice = Close;

array = Cross(C,MA(C,10));

Plot( SumSince(Buy,array), "SumSince", colorRed );
Plot( Buy, "Buy", colorYellow, styleHistogram );

With your way you would have to substract ValueWhen(Buy,array) every time to exclude occurrences at Buy signal.

Also with current implementation SumSince(condition,1) is equivalent (result array) to BarsSince(condition), so it's zero based.

If you want to have it inline with your way you can still add ValueWhen(condition, array) to SumSince.


Thanks @fxshrat. I agree that with all the *Since functions it's easy enough to get a result which either includes or excludes the bar on which the event occurred. It just seems curious that SumSince() would work differently than LowestSince(). I know that @Tomasz always seems to have a good reason behind his implementation choices, so I was hoping he'd enlighten me. :slight_smile:

Your 1st post wasn't directed to Tomasz.
But I think he will give bullet proof developer reason soon.

Since then I may give my further opinion...
Now... as for HighestSince/LowestSince... here contrary to SumSince you would rather want to include bar at event most of the time. E.g. getting highest/lowest since start of day so there you rather want to include high at some start of a time period. If the bar at event would be excluded by default then user would have to wrestle to get it included.

And also here you have flexibility e.g. if for example you want to exclude High/Low at Buy signal and if BuyPrice = Close; you can still use HighestSince(Ref(Buy,-1), array) in order to prevent forward looking (future leak).


As written in the manual, SumSince was designed as much faster replacement of:

x = Cum( array ) - ValueWhen( condition, Cum( array ) );

Such statement was quite common in many formulas and SumSince was designed as drop-in replacement.

1 Like

I think to create a version of SumSince which includes the event day itself, we can do something like this:

condition = DayOfWeek() == 1;
cumSum = Cum(array);
sumSinceWithEvent = cumSum - ValueWhen(condition, Ref(cumSum, -1));
function SumSinceInclusive( condition, array )
  return SumSince( condition, array ) + ValueWhen( condition, array );