# Contradictory information on IndicatorCounted() - page 2

33

Glad to hear that you finally agree. And thanks for your replies. Maybe I'll stop using IndicatorCounted() in the future (for some reason), but I wanted to understand it fully.

I still wonder why "everyone", including the documentation, decrements counted_bars, though ... If anyone reading this thread has an explanation, I'll all ears.

Thanks.

2584

thisone:

SDC: I'm not sure what code you are referring to. Could you please paste the code you have settled with and think is the most convenient?

Result:

13414, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1 ...

No decrementing needed, not matter how much your EA screws up.

The code I use that i found most convenient for general purpose indicator loops is something like this:

```extern int MA_Period=20;
extern int MaxHistory=1000;

int init()
{
for(int i=0;i<=5;i++)                                 //loop for 5 buffers
{SetIndexStyle(i,DRAW_LINE);                         //
if(Bars>MaxHistory+MA_Period)                       //This if else probably needs some
SetIndexDrawBegin(i,Bars-MaxHistory+MA_Period);    //modification I use it to prevent
else                                                //erroneous beginning of indicator lines
SetIndexDrawBegin(i,MA_Period*2);                  //but I feel there is probably better way of doing it
}
return(0);
}

int start()
{
int i,limit,counted_bars;
//----
if(Bars<=MA_Period) return(0);
counted_bars=IndicatorCounted();
if(counted_bars<0) return(-1);
limit=Bars-counted_bars-1;
if(limit>MaxHistory-1) limit=MaxHistory-1;
//----
for(i=limit;i>=0;i--)                               //I like to use for(i=limit;i>=0;i--)
{                                                  //because filling the buffers from old to
if(i<5) Alert("i= ",i);                           //new allows for buffer[i+1] to be used in
}                                                  //calculations within the same loop
return(0);
}
```

I would appreciate any comments/improvements regarding the above code

Test output:

There was low volume when I ran that test on the 1 minute chart.

a 1 tick bar at 23:51

a 3 tick bar at 23:52

a 1 tick bar at 23:53

a 2 tick bar at 23:54

but as you can see after the initial history bars are counted, i doesnt go above 1

33

Your code does basically the same as mine.

I printed out the amount of bars to count. That's why the result was 1,1,1,1,1, 2,1,1 etc ... Your "limit" variable is not the amount of bars to count, but the amount of bars to count minus one (... 0,0,0,1,0,0,0... ). I.e. it is the index of the first bar to count, which is generally 0 (current bar) or 1 (previous + current).

So we're doing the same thing, and none of us are trying to count more bars than IndicatorCounted() "wants" us to (by decrementing counted_bars).

Please tell me if you ever find out why so many indicators decrement counted_bars. It seems to me that all three persons posting in this thread agree that it's unnecessary.

2584

Yes I agree with you there is no point decrementing CountedBars() I didnt realise it was because the CountedBars() function decremented itself though until you pointed it out.

Another thing I could never figure out is why most indicators use for(int i=0; i<limit; i++) some use while(i>=0) but you rarely see one use for(i=limit;i>=0;i--)

15351

1. This is a point in decrementing CountedBars.
```int cb = CountedBars();
if (cb != 0) cb--;
for (int shift = Bars-1-cb; shift>=0; shift--){ ...```
If cb is zero you want to update all bars: Bars-1 .. zero. If cb is non-zero you want to update Bars-cb .. zero. An alternative would be
```int cb = CountedBars();
if (cb != 0) int shift = Bars-cb; else shift=Bars-1;
for (; shift>=0; shift--){ ...```

2. never figure out is why most indicators use for(int i=0; i<limit; i++) some use while(i>=0) but you rarely see one use for(i=limit;i>=0;i--)
Personal preference for the coder. Counting up may repaint.
33

```int cb = CountedBars();
if (cb != 0) cb--;
for (int shift = Bars-1-cb; shift>=0; shift--){ ...```

There is no point in decrementing cb (counted bars).

Let's see how your code will run:

```                                           // First tick:      Second tick     First tick of new bar
// Bars: 100        Bars: 100       Bars: 101
int cb = IndicatorCounted();               // cb: 0            cb: 99          cb: 99 <-- Remember, IndicatorCounted() decrements for you!
if (cb != 0) cb--;                         // cb: 0            cb: 98          cb: 98         (more specifically, it returns the value from the previous bar)
lim = Bars-1-cb;                           // lim: 99          lim: 1          lim: 2
for (int shift = lim; shift>=0; shift--){
--> Bar 99 - Bar 0      Bar1 - Bar0     Bar2, Bar1, Bar0
(every bar)
```

Look at that last column. You're counting three bars. No need to do that. IndicatorCounted() already wants you to count Bar1. You're making it count Bar2 as well, which was done a long time ago.

"If cb is zero you want to update all bars: Bars-1 .. zero."

Agreed.

"If cb is non-zero you want to update Bars-cb .. zero."

No, you want to update every bar that is excluded from IndicatorCounted(). Most of the time that means only bar zero. Sometimes it includes the previous bar as well, which IndicatorCounted() will exclude for you. Hence there's no need to decrement cb/counted_bars.

119

SDC:

The code I use that i found most convenient for general purpose indicator loops is something like this:

I would appreciate any comments/improvements regarding the above code

Test output:

There was low volume when I ran that test on the 1 minute chart.

a 1 tick bar at 23:51

a 3 tick bar at 23:52

a 1 tick bar at 23:53

a 2 tick bar at 23:54

but as you can see after the initial history bars are counted, i doesnt go above 1

you still have the problem that in the beginning all bars a re calculated twice:https://www.mql5.com/en/forum/133110
147

...returns the number of bars minus 1.
I uploaded an indicator that has the sole purpose of displaying what IndicatorCounted(0) returns: https://forum.mql4.com/47935.
Every time a new bar is formed, Bars is incremented one tic before IndicatorCounted is incremented.  So, IndicatorCounted() returns Bars-2 one time, every time, a new bar is created.
2584

I think I may have found where decrement Counted_Bars originated.

If you examine MQ Moving Averages code from 2004, the way the values for the averaging period are accumulated really does require counted_bars to be decremented. You have to examine the code carefully to find out why. I think future coders saw MQ decremented Counted_Bars in the moving average, found it did not cause any problems to do it in their own indicators, and because they did not see the reason for it they decided to put it in just in case it is neccessary. That would explain why it became the norm to do it as a matter of course without anyone really knowing why.

15351

With the advent of Build 6xx and OnCalculate() for indicators this thread and the function is deprecated. See https://www.mql5.com/en/forum/149695
12