CUM(1) while using Bar Replay doesn't increment

Hi. I am using a bar count in my AFL.

The following code doesn’t increment when using Bar Replay.

index = LastValue(cum( 1 )) - 1; 		// this is the bar index we are working on

_TRACE (“Index <” + index + “>”);

Any thing I should know about?


LastValue, as the name implies, uses only the last value :slightly_smiling_face:

Try this

x = Cum(1);
Your_Index = x - 1;

@DaFish and @zbin,

use EndValue for BarReplay.

index = cum( 1 ) - 1; // this is the bar index we are working on

_TRACEF("Index <%g>", EndValue(index));

Then it will increment when using Bar Replay.


Hi @fxshrat, Actually both solutions would work similarly. As checked,

x = Cum(1);
Your_Index = x - 1; // this is the bar index we are working on

_TRACEF("Index <%g>", Your_Index);

The only issue with using the EndValue function is that if a user wishes to ‘debug’ via the exploration method then the EndValue would return only the end value of the selected range (or last value if no range is selected). The ‘plain vanilla index value’ works in both the cases

Did you read the manual?

Proper way to get bar count is to use… surprise, surprise… tada …


which is built in variable. So your code can be just

_TRACE("Index <" + BarCount + ">");

Another thing that you should read is this:
and this:
note also what is written in the Cum() function doc:

Assumptions are not facts. Instead of assuming things read the manual. Generally your formula would not get/need all bars, but only required/visible bars, unless you specifically ask for all bars. To ask for all bars, as explained in the KB you need to call SetBarsRequired( sbrAll ), otherwise unless your chart is zoomed out max, you will get a ‘moving/scrolling window’ as new bars come in, old will be removed from sight and from array and your BarCount won’t change. Instead of looking on number of bars and making assumptions on number of bars, look at the last bar DateTime. You will see it changes.


Hi Tomasz, thanks for this and thanks to the others that responded. It was working live they way it was, just not in Bar Replay.

I actually have read the manual, but missed: SetBarsRequired( sbrAll );

Once I put that in, it worked. I also changed it to BarCount - thanks for that too.

SetBarsRequired( sbrAll );

BCindex = BarCount - 1; 						

_TRACE ("BCindex <" + BCindex + ">");

The thing is that you should NOT really use SetBarsRequired.
In 99% of cases makes no sense to ask for all bars. It just slows down the execution.
DO NOT DO THIS unless you have better reason than just assumptions.

The whole expectation of increasing number of bars is flawed.

You skipped important part of my response. Just as in a scrolling window INVISIBLE bars are NOT really needed. You don't see them and you don't really need them. BarCount does not matter. What counts is that NEW BARS ARE ADDED and you get NEW DATA without changing BarCount (like in FIFO queue).


Invisible bars should not bother you. You really don't need to care what happened in 1903. Re-read my previous response especially last paragraph possibly many times until it sinks.

1 Like