Simple question on indicator loop

 

Hi

on all the indicators i m creating i m using a while loop (below). Lately i came across on this debate on this thread Contradictory information on IndicatorCounted() - MQL4 forum (thanks to WHRoeder for pointing that to me) so questions arise.

My code looks like this

extern int history=300;

...
..

start()
{
...

    candles=Bars-IndicatorCounted(); // Index of the first uncounted

    if (candles>history-1)                 // If too many bars ..
    {
       candles=history-1;                       // ..calculate specified amount
    }
    
     i=0; //  set i=0 OR 
    // i=1; // depends on the indicator needs  sometimes i need to update indicator on bar 0 and sometimes on bar 1
         
    while  (i<=candles)
    {
     ...............
      i++;
     }
}

Now, depending on the indicator needs, sometimes i need to update indicator on bar 0 and sometimes on bar 1.

So my main question is, in case i set

i=0;


as the code above, does this cause any redrawing or other problem?

What about backtesting? Will it work ok?

Please note that i m NOT subtracting 1 from the IndicatorCounted.

Thanks

 
It's not possible to answer your questions. It depends of what you put in "........".
 
athanfx:
candles=Bars-IndicatorCounted();
while  (i<=candles)

Now, depending on the indicator needs, sometimes i need to update indicator on bar 0 and sometimes on bar 1.

So my main question is, in case i set
i=0;

as the code above, does this cause any redrawing or other problem?

What about backtesting? Will it work ok?

Please note that i m NOT subtracting 1 from the IndicatorCounted.
  1. Don't you ALWAYS need to update bar zero? The bar is changing (OHLC)
  2. IndicatorCounted handles the case where ticks were missed and bars higher than zero also need to be updated.
  3. Normal recommendation is to count down. Always. It makes no difference in the Indicator unless you refer to previous values (bogus first run) or newer values (repainting.)
  4. Backtesting is irrelevant
  5. Valid candles are zero to Bars - 1 inclusive
    candles=Bars -1 -IndicatorCounted();
    while  (i<=candles)
    candles=Bars-IndicatorCounted();
    while  (i < candles)
 
WHRoeder:
  1. Don't you ALWAYS need to update bar zero? The bar is changing (OHLC)

    No, sometimes i just want to display the indicator up to bar 1 and just wait for bar 0 to close (and so bar 0 will become bar 1 and so on).


  2. IndicatorCounted handles the case where ticks were missed and bars higher than zero also need to be updated.
  3. Normal recommendation is to count down. Always. It makes no difference in the Indicator unless you refer to previous values (bogus first run) or newer values (repainting.)

    So what you mean is that normal recommendation is to use a for/while loop counting down. Is that right?
    But what do you mean by "unless you refer to previous values". Referring to previous values from within the loop eg Close[i+2] (based on my loop example)? If so a Close[i+2] in my example above would give a bogus first run, right?
    Also what do you mean by "newer values"? How can you refer to a newer value? Please give an example so to understand this.

  4. Backtesting is irrelevant
  5. Valid candles are zero to Bars - 1 inclusive



 
  1. If you ignore the last tick of bar zero, you won't have calculated any value for buffer[0]. Then the next tick buffer[1] is empty. Do ALL bars.
  2. Close[i+2] is fine. I'm referring to buffer[i] = function( aBuffer[i+2] ) but aBuffer[i+2] hasn't been calculated.
  3. newer values buffer[i] = function( abuffer[i-2] ) or function( Close[i-2] ) looking toward the future. Example is computing a least squares on bars 0-10 and updating the buffer[0]-buffer[10]. Looks good but only in hindsight.
 
WHRoeder:
  1. If you ignore the last tick of bar zero, you won't have calculated any value for buffer[0]. Then the next tick buffer[1] is empty. Do ALL bars.
  2. Close[i+2] is fine. I'm referring to buffer[i] = function( aBuffer[i+2] ) but aBuffer[i+2] hasn't been calculated.
  3. newer values buffer[i] = function( abuffer[i-2] ) or function( Close[i-2] ) looking toward the future. Example is computing a least squares on bars 0-10 and updating the buffer[0]-buffer[10]. Looks good but only in hindsight.


1. ok so i calculate ALL bars! Got it! Thanks!
2. Got it as well!

Would also an iCustom call could cause a similar problem as well in a counting up while loop? like

double temp=iCustom(..........); 
buffer[i]=temp;
 
athanfx:


1. ok so i calculate ALL bars! Got it! Thanks!
2. Got it as well!

Would also an iCustom call could cause a similar problem as well in a counting up while loop? like

No it would not make a difference, assuming the custom indicator is coded properly its values for the entire chart should be available to an iCustom() call.

Like WHR said it is recommended to always use counting down loops because there is nothing to gain by counting up, but counting down allows your indicator to use its own values as calculated on older bars if neccessary.

buffer[i] = iCustom(,,,,i) + buffer[i+1];