IndicatorCounted() - Which are the default or correct indicator calculation in MT4/MT5: Current data to Previous data ("Right to Left" or "Zero to Last bar")??

 

IndicatorCounted() - Which are the Default or Correct indicator calculation in MT4/MT5: Current data to Previous data ("Right to Left" or "Zero to Last bar")?? 

Someone said that if you started in current data ("0") for indicator calculation then your indicator become lagging indicator?? WTF!

I searched in forum but I see some code here posted by others members that's started from "Zero to Last bar value". I want to know if this the correct way to do calculation or natural way to make indicator. It's still confused me, some coders decrements the looping calculation then make the calculation from Last bar value to Zero (Left to Right)?

I used IndicatorCounted() because it's simple, easy to use, and make my code flow natural.

Here's some example code that I learn from Youtube tutorial, blog post, and members sample code in MQL forum.

int start()
{ 
   int limit;
   int counted_bars=IndicatorCounted();
   
  //---- check for possible errors
   if(counted_bars<0) return(-1);
   
  //---- the last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   
   limit=Bars-counted_bars;
   
   for(int i=0; i<limit; i++)    // Zero to last bar data
   {
        buffer[] = close[i];     // Current data or start from "0"
        buffer2[] = close[i+1];  // Previous data or 1 day ago
   }

  return 0;
}
 
Musngi:

IndicatorCounted() - Which are the Default or Correct indicator calculation in MT4/MT5: Current data to Previous data ("Right to Left" or "Zero to Last bar")?? 

Someone said that if you started in current data ("0") for indicator calculation then your indicator become lagging indicator?? WTF!

I searched in forum but I see some code here posted by others members that's started from "Zero to Last bar value". I want to know if this the correct way to do calculation or natural way to make indicator. It's still confused me, some coders decrements the looping calculation then make the calculation from Last bar value to Zero (Left to Right)?

I used IndicatorCounted() because it's simple, easy to use, and make my code flow natural.

Here's some example code that I learn from Youtube tutorial, blog post, and members sample code in MQL forum.

You should use OnCalculate() instead of start(). There are variables rates_total and prev_calculated in OnCalculate(). It's up to you if you want to calculate from the newest bar (zero) to the oldest bar (last bar) or in opposite way. This is not related to lagging.

From Zero to the Last bar:

int OnCalculate (const int rates_total,      // size of input time series
                 const int prev_calculated,  // bars handled in previous call
                 const datetime& time[],     // Time
                 const double& open[],       // Open
                 const double& high[],       // High
                 const double& low[],        // Low
                 const double& close[],      // Close
                 const long& tick_volume[],  // Tick Volume
                 const long& volume[],       // Real Volume
                 const int &spread[])        // Spread

  {
   //---- check for possible errors
   if(prev_calculated<0) return(-1);
   //---- the last counted bar will be recounted, the oldest bar will never be counted because of close[i+1]
   int limit=rates_total-prev_calculated+prev_calculated<1?-1:1;
   for(int i=0; i<limit; i++)    // Zero to last bar data
   {
        buffer[i] = close[i];     // Current data or start from "0"
        buffer2[i] = close[i+1];  // Previous data or 1 day ago
   }
   return(rates_total);
  }

From the Last bar to Zero:

int OnCalculate (const int rates_total,      // size of input time series
                 const int prev_calculated,  // bars handled in previous call
                 const datetime& time[],     // Time
                 const double& open[],       // Open
                 const double& high[],       // High
                 const double& low[],        // Low
                 const double& close[],      // Close
                 const long& tick_volume[],  // Tick Volume
                 const long& volume[],       // Real Volume
                 const int &spread[])        // Spread

  {
   //---- check for possible errors
   if(prev_calculated<0) return(-1);
   //---- the last counted bar will be recounted, the oldest bar will never be counted because of close[i+1]
   int limit=rates_total-prev_calculated-prev_calculated<1?2:0;
   for(int i=limit; i>-1; i--)    // From the last bar to Zero
   {
        buffer[i] = close[i];     // Current data or start from "0"
        buffer2[i] = close[i+1];  // Previous data or 1 day ago
   }
   return(rates_total);
  }
 
  1. As Petr said, you decide.

  2. You should stop using the old IndicatorCounted and start using the new Event Handling Functions - Functions - Language Basics - MQL4 Reference

  3. The predefined arrays are initially as series, the ones in OnCalculate, you must set.

  4. See How to do your lookbacks correctly. (@Petr Nosek That includes not returning rates_total so you do not have to have the initial prev_calculated test.)
 
whroeder1:

(@Petr Nosek That includes not returning rates_total so you do not have to have the initial prev_calculated test.)

I agree with you. I don't use this test in my code but I wanted to show @Musngi a similar code to his code.