Interesting error in indicator

 

I have programmed an indicator which includes some function and main calculation is as below the problem is that the indicator does not automatically show the histogram, I have to refresh or change time frames then new histograms appear.

could any one let me know how should I solve this problem?

or is there any function which refreshes the indicator?

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   RefreshRates();
   WindowHandle(Symbol(),PERIOD_CURRENT);

   int i,                           // Bar index
       Counted_bars;                    // Number of counted bars
//--------------------------------------------------------------------
   Counted_bars=IndicatorCounted; // Number of counted bars
   i=Bars-Counted_bars-7;           // Index of the first uncounted
   while(i>=0)                      // Loop for uncounted bars
     {
      CalculateForMoment(i);
      i--;                          // Calculating index of the next bar
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
 
Taher Halimi:

could any one let me know how should I solve this problem?

That kind of trouble is usually due to the "array out of range" error.

However, I can't find the corresponding part in the program you showed us.

The Meta Editor has a debugging feature, so you can try tracing with it.

 

What is IndicatorCounted ? Do you mean IndicatorCounted() ?

if so, this should give you a unusable index

i=Bars-Counted_bars-7;           // Index of the first uncounted

if Bars is 1000 and counted bars is 999 then the index is calculated to

1000-999-7 which is -6

 
Keith Watford:

What is IndicatorCounted ? Do you mean IndicatorCounted() ?

That's right. I was not aware of it.

 
  1. You should stop using the old event handlers and IndicatorCounted() and start using the new ones.
              Event Handling Functions - Functions - Language Basics - MQL4 Reference
              How to do your lookbacks correctly.

  2. i=Bars-Counted_bars-7; 
    
    Keith already pointed the problem.
              How to do your lookbacks correctly.

 
Keith Watford:

What is IndicatorCounted ? Do you mean IndicatorCounted() ?

if so, this should give you a unusable index

if Bars is 1000 and counted bars is 999 then the index is calculated to

1000-999-7 which is -6

thank you very much, this was the problem, I added a condition if result is below zero not to deduct -7, but when I do not deduct -7 my indicator does not work at all :D

but I do not know why??

 
William Roeder:
  1. You should stop using the old event handlers and IndicatorCounted() and start using the new ones.
              Event Handling Functions - Functions - Language Basics - MQL4 Reference
              How to do your lookbacks correctly.

  2. Keith already pointed the problem.
              How to do your lookbacks correctly.

thank you very much William.

Reason: