What's wrong with my code?

 

Dear all

 

I have no idea why ma[] cannot show correct figures (moving average of bar length).

 

 

#property indicator_separate_window

#property indicator_buffers 2

extern int range = 10;

//--- 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_HISTOGRAM);
   SetIndexEmptyValue(1,0.0);
   SetIndexDrawBegin(1,1);
//--- name for DataWindow and indicator subwindow label
   IndicatorShortName("ma");
   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,range,0,MODE_SMA,i);
   }

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

 

 

tob2011 

 

Maybe its because you didnt specified the colors..try this right above the buffers..

 

#property indicator_color1 Red
#property indicator_color2 White
 
gviali:

Maybe its because you didnt specified the colors..try this right above the buffers..

 

 

I've added color to the buffers.  However, when I double check the figures, they're still wrong.

I tried to change the range to 1, but the figures of two buffers were not the same.... 

 
Check the root cause first. Just do simple logic, use Comment() and see if the result correct and then go from there.
 
deysmacro:
Check the root cause first. Just do simple logic, use Comment() and see if the result correct and then go from there.

I can't understand your meaning.

The indicator is so simple that I just want to find out ma of certain number of bar length.  The result is straight forward and can check it from terminal directly.

Where can I add Print() or Comment() to do a "simple" logic check??? 

 
Check the values for i?
 
gooly:
Check the values for i?

values for length[i] are ok.

the problem is the values of ma[i], and they are calculated by iMAOnArray function.

I found in help page that iMAOnArray calculates from left to right, so I used i-- instead of i++; 

I also have tried to change the value of range (variable) but no meaningful outcome can be found 

any suggestion? 

 
tob2011:

values for length[i] are ok.

May be not?

Try

Print("limit: ",(string)limit,"  loop i: ",(string)(limit-1)," .. 1");

 
gooly:

May be not?

Try


Value of length[i] is simply the absolute value of close[i] - open[i].  I can double check them bar by bar on chart and they are correct.

In addition, if I change variable "range" to 1, length[i] should be equal to ma[i] but it's not now.  That's the problem.

 
Well, just try it and post what is printed, you'll be surprised!
 

I'm not totally sure about what you want but try this

#property indicator_separate_window

#property indicator_buffers 1

extern int range = 10;

//--- indicator buffers
double ma[], length[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
  IndicatorBuffers(2);
//--- 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_HISTOGRAM);
   //SetIndexEmptyValue(1,0.0);
   //SetIndexDrawBegin(1,1);
//--- name for DataWindow and indicator subwindow label
   IndicatorShortName("ma");
   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,range,0,MODE_SMA,i);
   }

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

 I have highlighted the changes

Reason: