Download MetaTrader 5

Still "IndicatorCounted()", about recounted.

To add comments, please log in or register
Sell your CPU capacity and earn money!
Fa Cao
1141
Fa Cao 2014.01.03 05:15 

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);
  }
Simon Gniadkowski
Moderator
17999
Simon Gniadkowski 2014.01.03 09:00  
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.

Carl Schreiber
6588
Carl Schreiber 2014.01.03 12:12  

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

whroeder1
14480
whroeder1 2014.01.03 13:30  
   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.
Carl Schreiber
6588
Carl Schreiber 2014.01.03 13:48  
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

    Fa Cao
    1141
    Fa Cao 2014.01.03 13:56  
    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; 
    
    Ian Venner
    2584
    Ian Venner 2014.01.03 16:22  
    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.

    Fa Cao
    1141
    Fa Cao 2014.01.04 08:45  
    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.
    whroeder1
    14480
    whroeder1 2014.01.04 12:50  
    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.
    Ian Venner
    2584
    Ian Venner 2014.01.05 05:05  

    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.

    Fa Cao
    1141
    Fa Cao 2014.01.05 08:08  
    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;
    12
    To add comments, please log in or register