How to stop indicator from crashing?

 

This indicator crashes whenever the variable History is larger than the number of bars I have in data history.

int start()
  {
   //******** TEMPLATE INDICADOR **********/
   int i, counted_bars=IndicatorCounted();
   i = Bars - counted_bars - 1;
   if (i > History - 1)
      i = History - 1;
      
   while ( i >= 0 ) //loop do template
   {
      buf_ma[i] = AvgSpread(Period(), MAPeriod, i);
            
      if ( Close[i] >= Close[i+1] )
      {
         buf_up[i] = High[i] - Low[i];
         buf_down[i] = 0;
         i--;
         continue;
      }
      if ( Close[i] < Close[i+1] )
      {
         buf_down[i] = High[i] - Low[i];
         buf_up[i] = 0;
         i--;
         continue;
      }
   }
   
   /*****************************************/
   
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

double AvgSpread(int timeframe, int period, int shift)
{
   double spread = 0;
   int i = shift, counter = 0, sessao = 0, h4 = 0;
   
   spread = 0;
      
   for (i = shift; i <= shift + period; i++)
      spread = spread + iHigh(NULL, timeframe, i) - iLow(NULL, timeframe, i);
   
   return(spread/period);
}

 

I tried the condition

if (History > Bars)
   History = Bars

 

But it still crashes and the indicator repeats itself when I scroll the chart to the left. Another indicator template that will avoid these pesky crashes will also be appreciated, thank you!

 
As 'crashing' I mean it crashes Metatrader: I have to restart it.
 
Jasus5457:
As 'crashing' I mean it crashes Metatrader: I have to restart it.

Is History an input ?  I don't think they can be changed by your code . . . so use an intermediate variable . . 

int lHistory;  //local version of the History variable


lHistory = History;

if (History > Bars)
   lHistory = Bars;

  . . . then use lHistory in your code instead of History.

 

Thank you, it worked, History is an input yes. But it remains with a problem, if I scroll the chart beyond the point where it has to download data from the server, the indicator either repeats itself with former values or Metatrader crashes..

 

#property indicator_separate_window

#property indicator_buffers 3

#property indicator_color1 Black
#property indicator_color2 Sienna
#property indicator_color3 Maroon

#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2

double buf_up[], buf_down[], buf_ma[];
extern int History = 500;
extern int MAPeriod = 100;

int london[2] = {9, 14}, london_us[2] = {15, 16}, us[2] = {17, 19}, asian[2] = {20, 8};
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   SetIndexBuffer(0, buf_up);
   SetIndexBuffer(1, buf_down);
   SetIndexBuffer(2, buf_ma);
   SetIndexStyle(0, DRAW_HISTOGRAM, 0);
   SetIndexStyle(1, DRAW_HISTOGRAM, 0);
   SetIndexStyle(2, DRAW_LINE, 0);
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   //******** TEMPLATE INDICADOR **********/
   int i, counted_bars=IndicatorCounted();
   i = Bars - counted_bars - 1;
   int tHistory;
   
   if (History > Bars - MAPeriod)
      tHistory = Bars - MAPeriod;
   else
      tHistory = History;
      
   if (i > tHistory - 1)
      i = tHistory - 1;
         
   while ( i >= 0 ) //loop do template
   {
      buf_ma[i] = AvgSpread(Period(), MAPeriod, i);
            
      if ( Close[i] >= Close[i+1] )
      {
         buf_up[i] = High[i] - Low[i];
         buf_down[i] = 0;
         i--;
         continue;
      }
      if ( Close[i] < Close[i+1] )
      {
         buf_down[i] = High[i] - Low[i];
         buf_up[i] = 0;
         i--;
         continue;
      }
   }
   
   /*****************************************/
   
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

double AvgSpread(int timeframe, int period, int shift)
{
   double spread = 0;
   int i = shift, counter = 0, sessao = 0, h4 = 0;
   
   spread = 0;
      
   for (i = shift; i <= shift + period; i++)
      spread = spread + iHigh(NULL, timeframe, i) - iLow(NULL, timeframe, i);
   
   return(spread/period);
}
 
Jasus5457:

Thank you, it worked, History is an input yes. But it remains with a problem, if I scroll the chart beyond the point where it has to download data from the server, the indicator either repeats itself with former values or Metatrader crashes..

Does MT4 crash or just get busy (lock-up) ? I suspect it just get very busy with your loop in AvgSpread(),  print the values to screen with Comment() and to the log with Print() them make it lock-up . . . see if you can see anything helpful.
 
You shouldn't call it AvgSpread as the spread is the difference between Ask and Bid. That is the Average Range (same as in Average True Range.)
double AvgSpread(int timeframe, int period, int shift){
   :
   spread += iHigh(NULL, timeframe, i) - iLow(NULL, timeframe, i);
 
RaptorUK:
Does MT4 crash or just get busy (lock-up) ? I suspect it just get very busy with your loop in AvgSpread(),  print the values to screen with Comment() and to the log with Print() them make it lock-up . . . see if you can see anything helpful.


Actually the problem was on another function. What was happening is that for some reason it entered in an infinite loop when the shift was higher than bars in history, due to a 'while' loop. So you were right, it was on the function I was using not on the indicator implementation. Thanks!
Reason: