Array Out of Range. need help.

 

I wrote this to check if the close price of the last 150 bars were above the moving averege of the last 150 bars.

bool ma_buy_2()
{
int i = 1;
int n = 1;
double MA_Array[];
double Candle_Array[];

for(i=1; i<150; i++)
MA_Array[i] = iMA(Symbol(), Period(), 200, 0, 0, 0, i);

for(n=1; n<150; n++)
Candle_Array[n] = Close[n];

   if(MA_Array[i]<Candle_Array[n])
   {
   return (true);
   }
   else return false;

}



When i start the simulation the simulation stops immediately and in the journal stand "array out of range".

 
Because you haven't sized your arrays
 

I used the ArrayResize function but it didn't work. I have started programming in mql4 a month ago and i dont know what to do. I also tried to enter the number directly into the array ( double MA_Array[150]; ) but it didn't work.


bool ma_buy_2()
{
int i = 1;
int n = 1;
double MA_Array[];
ArrayResize(MA_Array,150,10);

double Candle_Array[];
ArrayResize(Candle_Array,150,10);

for(i=1; i<150; i++)
MA_Array[i] = iMA(Symbol(), Period(), 200, 0, 0, 0, i);

for(n=1; n<150; n++)
Candle_Array[n] = Close[n];

   if(MA_Array[i]<Candle_Array[n])
   {
   return (true);
   }
   else return false;
}



Bevor I used the for loop I wrote the function like that but it worked only for the 100 last bars. If i went above 100 bars i got the same error (array out of range).


bool ma_buy_1()
{
double ma1 = iMA(Symbol(), Period(), 200, 0, 0, 0, 1);
double ma2 = iMA(Symbol(), Period(), 200, 0, 0, 0, 5);
double ma3 = iMA(Symbol(), Period(), 200, 0, 0, 0, 10);
double ma4 = iMA(Symbol(), Period(), 200, 0, 0, 0, 15);
double ma5 = iMA(Symbol(), Period(), 200, 0, 0, 0, 20);
double ma6 = iMA(Symbol(), Period(), 200, 0, 0, 0, 25);
double ma7 = iMA(Symbol(), Period(), 200, 0, 0, 0, 30);
double ma8 = iMA(Symbol(), Period(), 200, 0, 0, 0, 35);
double ma9 = iMA(Symbol(), Period(), 200, 0, 0, 0, 40);
double ma10 = iMA(Symbol(), Period(), 200, 0, 0, 0, 45);
double ma11 = iMA(Symbol(), Period(), 200, 0, 0, 0, 50);


   if(ma1<Close[1]
   && ma2<Close[5]
   && ma3<Close[10]
   && ma4<Close[15]
   && ma5<Close[20]
   && ma6<Close[25]
   && ma7<Close[30]
   && ma8<Close[35]
   && ma9<Close[40]
   && ma10<Close[45]
   && ma11<Close[50]
   )

   {
   return (true);
   }
   else return false;
}

 

Although I don't see the point of the loop as you only use the last array elements

bool ma_buy_2()
{
int i = 1;
int n = 1;
double MA_Array[];
ArrayResize(MA_Array,150,10);

double Candle_Array[];
ArrayResize(Candle_Array,150,10);

for(i=1; i<150; i++)
MA_Array[i] = iMA(Symbol(), Period(), 200, 0, 0, 0, i);
//At the end of the loop i is incremented by 1 and that causes the array out of range
i--;

for(n=1; n<150; n++)
Candle_Array[n] = Close[n];
n--;

   if(MA_Array[i]<Candle_Array[n])
   {
   return (true);
   }
   else return false;
}
 
I  actually want check all array elements if they are below the prices of the closing bars but i dont know how to do it.
 
Just loop through all elemts of your array(s). Look in your reference (array..) there are examples..
Reason: