What's wrong with my code? - page 2

 

Unexpectly, I find out the problem by amending the line from

 ma[i] = iMAOnArray(length,0,range,0,MODE_SMA,i);

to

ma[i] = iMAOnArray(length,0,1,0,MODE_SMA,i); 

and the result is now correct!

 

#property indicator_separate_window

#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Purple

extern int range = 1;

//--- indicator buffers
double ma[], length[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   IndicatorDigits(Digits);
//---- indicator buffers mapping  
   SetIndexBuffer(0,ma);
   SetIndexBuffer(1,length);
//--- drawing settings
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexEmptyValue(0,0.0);
   SetIndexDrawBegin(0,1);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexEmptyValue(1,0.0);
   SetIndexDrawBegin(1,1);
//--- name for DataWindow and indicator subwindow label
   IndicatorShortName("ma on bar length");
   SetIndexLabel(0,"ma");
   SetIndexLabel(1,"length");
//---
//---
   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[])
  {
//---
   int limit=rates_total-prev_calculated;
//---
   if(rates_total<2)
      return(0);
//---
   int i;

   for(i=(limit-1); i>=1; i--)
   {
      length[i] = MathAbs(Close[i] - Open[i]);
   }
   
   for(i=(limit-1); i>=1; i--)
   {
      ma[i] = iMAOnArray(length,0,1,0,MODE_SMA,i);
   }

//--- return value of prev_calculated for next call
   return(rates_total);
  }

 

However, why can't I use extern int in this indicator???

 
ma[i] = iMAOnArray(length,0,1,0,MODE_SMA,i);
Where did you set length to be a series array (zero being the newest?)
 
WHRoeder:
Where did you set length to be a series array (zero being the newest?)

I wonder if anyone has ever found that an indicator buffer is not automatically set as series?

I know that the documentation states that buffers are non-series by default, but that is not the case. 

 
GumRai:

I wonder if anyone has ever found that an indicator buffer is not automatically set as series?

I know that the documentation states that buffers are non-series by default, but that is not the case. 

https://www.mql5.com/en/forum/23919/page2#comment_869096
 
tob2011:
....

However, why can't I use extern int in this indicator???

This morning I wasted one hour with a - may be - similar issue. But in my case, I was the problem.

I've coded a very simple indicator while it stayed in use in the chart of MT4. While doing that, I changed the default-Value of a indicator-variable but forgot to change it in the chart. So the indicator was still working with a very different and earlier value than I have expected through my code. It was to easy to look at the value debbuging. Because I've read your topic before, I thought mine is the same story.

So, long story short: Is it possible that you are testing with an old and wrong value in your 'range' ?

Reason: