Download MetaTrader 5
To add comments, please log in or register
Having troubles understanding the code? Perhaps, you can find an article on that topic!
berix
88
berix 2014.07.22 15:14 

I wonder why accessing volume or spread parameter of the OnCalculate function makes the indicator freezing when compiled with "#property strict" property.

Have a try. When running, first tick shows alerts up to instruction: Alert("tick_volume[0]="+IntegerToString(tick_volume[0]));

Subsequent ticks show the indicator not working any more.

Conversely, compiling with "#property strict" being commented produces a running code at every tick.

What is going wrong?


#property strict
#property indicator_separate_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
//---
Alert("--------------");

Alert("rates_total="+IntegerToString(rates_total));
Alert("prev_calculated="+IntegerToString(prev_calculated));
Alert("time[0]="+TimeToString(time[0]));
Alert("open[0]="+DoubleToString(open[0],5));
Alert("high[0]="+DoubleToString(high[0],5));
Alert("low[0]="+DoubleToString(low[0],5));
Alert("close[0]="+DoubleToString(close[0],5));
Alert("tick_volume[0]="+IntegerToString(tick_volume[0]));
Alert("volume[0]="+IntegerToString(volume[0])); // Reference to volume[0] doesn't work with #property strict. The indicator freezes. Why?
Alert("spread[0]="+IntegerToString(spread[0])); // Reference to spread[0] doesn't work with #property strict. The indicator freezes. Why?

//--- return value of prev_calculated for next call
   return(prev_calculated);
  }
berix
88
berix 2014.07.22 21:54  

After further investigation, it turns out that

ArraySize(volume) = 0
ArraySize(spread) = 0

Since "volume" and "spread" vectors are empty, no doubt the indicator stops working with an "array out of range" error.

Conclusion : do not use 'volume' and 'spread' parameters from OnCalculate calls.

/
To add comments, please log in or register