Download MetaTrader 5

What's wrong with my code?

To add comments, please log in or register
Do you have questions to the application's developer - ask them in Comments section
tob2011
72
tob2011 2015.02.05 03:28 

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 

Gabriel Viali
205
Gabriel Viali 2015.02.05 04:05  

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

 

#property indicator_color1 Red
#property indicator_color2 White
tob2011
72
tob2011 2015.02.05 05:50  
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.... 

SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2015.02.05 05:55  
Check the root cause first. Just do simple logic, use Comment() and see if the result correct and then go from there.
tob2011
72
tob2011 2015.02.05 06:04  
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??? 

Carl Schreiber
6586
Carl Schreiber 2015.02.05 07:59  
Check the values for i?
tob2011
72
tob2011 2015.02.05 08:30  
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? 

Carl Schreiber
6586
Carl Schreiber 2015.02.05 09:35  
tob2011:

values for length[i] are ok.

May be not?

Try

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

tob2011
72
tob2011 2015.02.05 09:43  
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.

Carl Schreiber
6586
Carl Schreiber 2015.02.05 09:54  
Well, just try it and post what is printed, you'll be surprised!
Keith Watford
Moderator
9614
Keith Watford 2015.02.05 11:44  

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

12
To add comments, please log in or register