Could anyone review this code? How to assign values to an array...

 

Hi there,

I am trying to program an custom indicator but I cannot manage to assign values to an array. In my example, I am just trying to calculate the average value between the High and the Low of each bar and assign to an array for subsequent calculations. I used the Alert() function to find out the value of the Array components once assigned (just for bars from 1 to 5). The Alert() function always gave the value 0 as an outcome. However, if I use the same code with normal variables instead of an Array everything works, but it's not very useful when I want to do it for a large number of Bars. Could anyone help? Why the values are not assigned to the Array?

Thank you

PS, do not worry about the code efficiency and the fact that there are no arrays assigned to the buffers.

//--------------------------------------------------------------------
// averagevalue.mq4 
// The code should be used for educational purpose only.
//--------------------------------------------------------------------
#property indicator_chart_window    // Indicator is drawn in the main window
#property indicator_buffers 2       // Number of buffers
#property indicator_color1 Blue     // Color of the 1st line
#property indicator_color2 Red      // Color of the 2nd line
 
double Buf_0[],Buf_1[];             // Declaring indicator arrays
//--------------------------------------------------------------------
int init()                          // Special function init()
  {
   return;                          // Exit the special funct.init()
  }
//--------------------------------------------------------------------
int start()                         // Special function start()
  {
   int i,                           // Bar index
       Counted_bars;                // Number of counted bars
   double Aver_Bar[],
          Ave_H,
          Ave_L;
//--------------------------------------------------------------------
   Counted_bars=IndicatorCounted(); // Number of counted bars
   i=Bars-Counted_bars-1;           // Index of the first uncounted
   while(i>=0)                      // Loop for uncounted bars
     {
         Ave_H=High[i]/2;
         Ave_L=Low[i]/2;
         Aver_Bar[i]=Ave_H + Ave_L;        // Calculates Average value for each Bar 
         switch(i)
         {
         case 1 :
                  Alert("Aver_Bar[1]= ", Aver_Bar[i]);
                  Alert("Ave_H= ", Ave_H, " Ave_L= ", Ave_L);
                  Alert("High[1]= ", High[i], " Low[1]= ", Low[i]);
                  break;
         case 2 :
                  Alert("Aver_Bar[2]= ", Aver_Bar[i]);
                  Alert("Ave_H= ", Ave_H, " Ave_L= ", Ave_L);
                  Alert("High[2]= ", High[i], " Low[2]= ", Low[i]);
                  break;
         case 3 :
                  Alert("Aver_Bar[3]= ", Aver_Bar[i]);
                  Alert("Ave_H= ", Ave_H, " Ave_L= ", Ave_L);
                  Alert("High[3]= ", High[i], " Low[3]= ", Low[i]);
                  break;
         case 4 :                  
                  Alert("Aver_Bar[4]= ", Aver_Bar[i]);
                  Alert("Ave_H= ", Ave_H, " Ave_L= ", Ave_L);
                  Alert("High[4]= ", High[i], " Low[4]= ", Low[i]);
                  break;
         case 5 :         
                  Alert("Aver_Bar[5]= ", Aver_Bar[i]);
                  Alert("Ave_H= ", Ave_H, " Ave_L= ", Ave_L);
                  Alert("High[5]= ", High[i], " Low[5]= ", Low[i]);
                  break;
         }
         
      i--;                          // Calculating index of the next bar
     }

//--------------------------------------------------------------------
   return;                          // Exit the special funct. start()
  }
//--------------------------------------------------------------------

 
Are you using buffers or arrays ? if using arrays you need to declare the array with the number of elements or do an ArrayResize . . . if you are using buffers then you need to use them properly . . .
 
galcades:
PS, do not worry about the code efficiency and the fact that there are no arrays assigned to the buffers.
That's your problem. Your arrays have no size (Aver_Bar[]), you can not use it/them until they do, either by explicit compile time size, dynamic resize, or implicitly via setIndicatorBuffer
 
RaptorUK:
Are you using buffers or arrays ? if using arrays you need to declare the array with the number of elements or do an ArrayResize . . . if you are using buffers then you need to use them properly . . .


Thanks a lot, I am starting with mql4 and didn't know I had to declare the array with the number of elements. Now it works smoothly.