Help find error

 

Hi,

Could anyone please help me  find what is the problem with this indicator that I downloaded from the internet? The code is supposed to calculate the double exponential moving average, DEMA, using the iMAonArray function instead of calculating it from scratch which makes the code much more elegant and easy to understand. The indicator is supposed to draw three different averages: the Ema, EmaOfEma and the Dema. However, only the Ema indicator is drawn. Since the source code is from 2006, I presumed that it has to do with some newer version of MT4 that does not work well with the indicator code.

After many hours of debugging, I found that the other two buffers, EmaOfEma and Dema are not filled at all due to "out of range" error. Therefore, I changed the declaration of those two buffers to: "double EmaOfEma[10000]" and "double Dema[10000]". The debugging showed that these two buffers are now filled with the (seemingly) correct values. However, they are still not drawn in MT4. The only line printed is Ema as before.

 Appreciate any pointers and help. Thanks in advance

 

The code:

//+------------------------------------------------------------------+
//|                                                   DEMA_RLH       |
//|                                    Copyright © 2006, Robert Hill |
//|                                       http://www.metaquotes.net/ |
//|                                                                  |
//| Based on the formula developed by Patrick Mulloy                 |
//|                                                                  |
//| It can be used in place of EMA or to smooth other indicators.    |
//|                                                                  |
//| DEMA = 2 * EMA - EMA of EMA                                      |
//|                                                                  |
//|  Red is EMA, Green is EMA of EMA, Yellow is DEMA                 |
//|                                                                  |
//+------------------------------------------------------------------+
#property  copyright "Copyright © 2006, Robert Hill "
#property  link      "http://www.metaquotes.net/"
//---- indicator settings
#property  indicator_chart_window
#property  indicator_buffers 3
#property  indicator_color1  Red
#property  indicator_color2  Green
#property  indicator_color3  Yellow
#property  indicator_width1  2
#property  indicator_width2  2
#property  indicator_width3  2
//----
extern int EMA_Period=14;
//---- buffers
double Dema[];
double Ema[];
double EmaOfEma[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- drawing settings
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexDrawBegin(0,EMA_Period);
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+2);
//---- 3 indicator buffers mapping
   if(!SetIndexBuffer(0,Ema) &&
      !SetIndexBuffer(1,EmaOfEma) &&
      !SetIndexBuffer(2,Dema))
      Print("cannot set indicator buffers!");
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("DEMA("+EMA_Period+")");
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   int i, limit;
   int    counted_bars=IndicatorCounted();
   if(counted_bars>0) counted_bars--;
//----   
   limit=Bars-counted_bars;
//----
   for(i=limit; i>=0; i--)
      Ema[i]=iMA(NULL,0,EMA_Period,0,MODE_EMA,PRICE_CLOSE,i);
   for(i=limit; i >=0; i--)
      EmaOfEma[i]=iMAOnArray(Ema,Bars,EMA_Period,0,MODE_EMA,i);
   //========== COLOR CODING ===========================================               
   for(i=limit; i >=0; i--)
      Dema[i]=2 * Ema[i] - EmaOfEma[i];
   return(0);
  }
//+------------------------------------------------------------------+
 
   if(!SetIndexBuffer(0,Ema) &&
      !SetIndexBuffer(1,EmaOfEma) &&
      !SetIndexBuffer(2,Dema))
      Print("cannot set indicator buffers!");

should be

      if(!(SetIndexBuffer(0,Ema) &&
      SetIndexBuffer(1,EmaOfEma) &&
      SetIndexBuffer(2,Dema)))
      Print("cannot set indicator buffers!");
 
Uwe Goetzke:

should be

Thanks so very much Uwe.

This tiny, seemingly insignificant correction did the trick. Now,all three lines are showing.

I am curious, did you guess what the problem was? I.e., is it a common problem? Or did you have to debug?

 

Best regards and thank you again

 

In fact  I had to debug because I could not recognize it by reading and got curious...

I have the feeling that handling of the && operator changed and it skips the other evaluations if it is false one the first element. 

i am not sure if this was always the case.

 

Uwe 

 
Uwe Goetzke:

In fact  I had to debug because I could not recognize it by reading and got curious...

I have the feeling that handling of the && operator changed and it skips the other evaluations if it is false one the first element. 

i am not sure if this was always the case.

 

Uwe 

I think your speculation is correct. In any case, I now know what to look for when debugging these kinds of errors.

Much obliged for your help.

/Omar