Download MetaTrader 5

Why these two indicators show different result

To add comments, please log in or register
bp2000
9
bp2000  

hello, can anyone know why these two indicators show different result?

Indi_A.mq4 is original code

Indi_B.mq4 is my changed code

// Indi_A.mq4

#property indicator_separate_window
#property indicator_minimum 2.0
#property indicator_maximum 4.0
#property indicator_buffers 4
#property indicator_color1 Lime
#property indicator_color2 OrangeRed
#property indicator_color3 Black
#property indicator_color4 Black

double buffer1[];
double buffer2[];
double buffer3[];
double buffer4[];

int init() {
   IndicatorShortName("Indi_A");
   SetIndexStyle(0, DRAW_ARROW, STYLE_SOLID, 0, clrLime);
   SetIndexArrow(0, 110);
   SetIndexBuffer(0, buffer1);
   SetIndexStyle(1, DRAW_ARROW, STYLE_SOLID, 0, clrOrangeRed);
   SetIndexArrow(1, 110);
   SetIndexBuffer(1, buffer2);
   SetIndexBuffer(2, buffer3);
   SetIndexBuffer(3, buffer4);
   IndicatorDigits(0);
   return (0);
}

int deinit() {
   return (0);
}

int start() {
   int nBar = 300;
   for (int i = 0; i < nBar; i++) {
      buffer2[i] = EMPTY_VALUE;
      buffer1[i] = EMPTY_VALUE;
      buffer3[i] = iMA(NULL, 0, 14, 0, MODE_EMA, PRICE_CLOSE, i) - iMA(NULL, 0, 50, 0, MODE_EMA, PRICE_CLOSE, i);
   }
   for (i = 0; i < nBar; i++) buffer4[i] = iMAOnArray(buffer3, 0, 5, 0, MODE_SMA, i);
   for (i = 0; i < nBar; i++) {
      if (buffer3[i] < buffer4[i]) buffer2[i] = 3;
      else buffer1[i] = 3;
   }
   return (0);
}


I want Indi_B show as Indi_A's result

Files:
Indi_A.mq4 2 kb
Indi_B.mq4 2 kb
whroeder1
16764
whroeder1  

In A buffer3 is a buffer, it is asSeries. Therefor iMA(5) knows you mean (5 + 6 + 7...)/n.

In B buffer3 is an array, not asSeries. Therefor iMA(5) wants ( 5 + 4 + 3...)/n which doesn't work.

Set the array to series, before populating it.
          ArraySetAsSeries - Array Functions - MQL4 Reference

bp2000
9
bp2000  

Thank you very much, it work now

To add comments, please log in or register