Still "IndicatorCounted()", about recounted.

 

I find that indicator will recount at least one bar when by "IndicatorCounted()", for example, the code as below from "Alligator"

when I attach this indicator into chart, I see as below picture in Experts of Terminal

That means for() will be at least run two times, so always the bar before current bar will be recounted, yes? if it is, why?

thanks!

int start()
  {
   int limit;
   int counted_bars=IndicatorCounted();
//---- check for possible errors
   if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   Print("Bars-counted_bars",limit); // added by myself
//---- main loop
   for(int i=0; i<limit; i++)
     {
      //---- ma_shift set to 0 because SetIndexShift called abowe
      ExtBlueBuffer[i]=iMA(NULL,0,JawsPeriod,0,MODE_SMMA,PRICE_MEDIAN,i);
      ExtRedBuffer[i]=iMA(NULL,0,TeethPeriod,0,MODE_SMMA,PRICE_MEDIAN,i);
      ExtLimeBuffer[i]=iMA(NULL,0,LipsPeriod,0,MODE_SMMA,PRICE_MEDIAN,i);
     }
//---- done
   return(0);
  }
 
vx0532:

I find that indicator will recount at least one bar when by "IndicatorCounted()", for example, the code as below from "Alligator"

when I attach this indicator into chart, I see as below picture in Experts of Terminal

That means for() will be at least run two times, so always the bar before current bar will be recounted, yes? if it is, why?

Bar 0 is not counted, read the documentation, and also this . . .

if(counted_bars>0) counted_bars--;

. . . so that gives you 2 bars to recalculate on each tick.

 

vx0532:

if it is, why?

Sometimes the last 3 bars, 0,1,2, have to be recalculated.

The broker could have changed these bars - on y soit qui mal y pens!

gooly

 
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   Print("Bars-counted_bars",limit); // added by myself
//---- main loop
   for(int i=0; i<limit; i++)
  1. Drop the decrement - Contradictory information on IndicatorCounted() - MQL4 forum
  2. Aways count Down
       for(int i=Bars-1-counted_bars; i>=0; i--)
  3. Normally it will run just bar zero. At the start of a new bar it will run the last tick of bar 1 and new bar 0.
 
WHRoeder:
    Normally it will run just bar zero. At the start of a new bar it will run the last tick of bar 1 and new bar 0.

    My experience is that normally bar[1] and bar[0] are run sometimes as well bar[2] (if a new bar[0] was started).

    But this might be broker depended.

    gooly

     
    WHRoeder:
    1. Drop the decrement - Contradictory information on IndicatorCounted() - MQL4 forum
    2. Aways count Down
    3. Normally it will run just bar zero. At the start of a new bar it will run the last tick of bar 1 and new bar 0.


    So the code like below in official MQL4 should make some change according to your above points.

    int counted_bars=IndicatorCounted();
    //---- check for possible errors
       if(counted_bars<0) return(-1);
    //---- last counted bar will be recounted
       if(counted_bars>0) counted_bars--; 
       limit=Bars-counted_bars; 
    
     
    vx0532:


    So the code like below in official MQL4 should make some change according to your above points.

    The official MQL4 way of coding the loop is:

    Counted_bars=IndicatorCounted(); // Number of counted bars   
    i=Bars-Counted_bars-1;           // Index of the first uncounted   
    while(i>=0)                      // Loop for uncounted bars     
    { 
      Buf_0[i]=High[i];              // Value of 0 buffer on i bar      
      Buf_1[i]=Low[i];               // Value of 1st buffer on i bar      
      i--;                           // Calculating index of the next bar     
    }

    You will see there is no last bar will be recounted here.

    I believe the myth that it is neccessary to do "last bar will be recounted" originated in MQ moving averages code. In that code the loop really does require last bar to be recounted, but it is not obvious why it needs to do that, you have to examine the SMA code carefully line by line and figure up the calculation to see why it needs to recount the last bar.

     
    SDC:

    The official MQL4 way of coding the loop is:

    You will see there is no last bar will be recounted here.

    I believe the myth that it is neccessary to do "last bar will be recounted" originated in MQ moving averages code. In that code the loop really does require last bar to be recounted, but it is not obvious why it needs to do that, you have to examine the SMA code carefully line by line and figure up the calculation to see why it needs to recount the last bar.

    thank you. but I think there is no need to recount the last bar.
     
    vx0532: but I think there is no need to recount the last bar.
    That is the function of IndicatorCounted. Don't redo the last bar unless it is necessary. Last missed tick of the last bar and current tick of the new bar is the only case where bar 1 is redone.
     

    If you correlate a buffer index[i] with IndicatorCounted() by some code such as:

    int i, counted_bars = IndicatorCounted();
    i = Bars - counted_bars - 1;

    i == 1 every time the first tick of a new bar appears. That is true because when the new bar first appears it is so far "uncounted".

    So by using the standard indicator loop that tick will cause two calculations to made in the loop. The last bar will be recalculated by HLOC (i==1) and the new bar calculated by the value of that new tick (i==0).

    Usually it doesnt matter if the last bar is recalculated because most indicators would give the same result on the last tick of the bar as it does on final HLOC of that bar, BUT if you calculate by a method that is tick dependant and you don't want the result to be overwritten by final HLOC you would have to code the loop to not let i > 0 after it has calculated the chart history.

    It is not difficult to do that if you bear in mind:

    IndicatorCounted() == 0 when the chart history is being calculated (initial load or when chart history is added)

    IndicatorCounted() > 0 when working on bar zero live ticks.

     
    SDC:

    If you correlate a buffer index[i] with IndicatorCounted() by some code such as:

    i == 1 every time the first tick of a new bar appears. That is true because when the new bar first appears it is so far "uncounted".

    So by using the standard indicator loop that tick will cause two calculations to made in the loop. The last bar will be recalculated by HLOC (i==1) and the new bar calculated by the value of that new tick (i==0).

    Usually it doesnt matter if the last bar is recalculated because most indicators would give the same result on the last tick of the bar as it does on final HLOC of that bar, BUT if you calculate by a method that is tick dependant and you don't want the result to be overwritten by final HLOC you would have to code the loop to not let i > 0 after it has calculated the chart history.

    It is not difficult to do that if you bear in mind:

    IndicatorCounted() == 0 when the chart history is being calculated (initial load or when chart history is added)

    IndicatorCounted() > 0 when working on bar zero live ticks.


    So" i = Bars - counted_bars - 1;" is ok when IndicatorCounted() is not -1;
    Reason: