Download MetaTrader 5

Alert Error "array out of range"

To add comments, please log in or register
Have you published a program in the Market? Place it in your blog as well - the widget will help!
texoro
126
texoro 2014.04.22 09:04 

[10:50:01] Teodoro Nato:

 for ( int i=0; i< 5; i++)
{ for ( int i=0; i< 5; i++)
{
//i create the array in the values of the last 5 candle of the average at 20 periods
MovingAvarage20[i]= iMA(NULL, 0, PeriodMovingAvarage20,0, MODE_EMA, PRICE_CLOSE,i);
ma20[i]= MovingAvarage20[i];

//i create the array in the values of the last 5 candle of the average at 40 periods
MovingAvarage40[i]= iMA(NULL, 0, PeriodMovingAvarage40,0, MODE_EMA, PRICE_CLOSE,i);
ma40[i]= MovingAvarage40[i];

//i create the array in the values of the last 5 candle of the average at 50 periods
MovingAvarage50[i]= iMA(NULL, 0, PeriodMovingAvarage50,0, MODE_EMA, PRICE_CLOSE,i);
ma50[i]= MovingAvarage50[i];
I created an index but when i mark the MovingmAverage" what happends is: The index works, but if i fill the mistake do not appear but if i "Attach"to the graph the errors is "array out of rage"
Alain Verleyen
Moderator
30737
Alain Verleyen 2014.04.22 11:26  

Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.

Alain Verleyen
Moderator
30737
Alain Verleyen 2014.04.22 11:28  
texcs:

[10:50:01] Teodoro Nato:

I created an index but when i mark the MovingmAverage" what happends is: The index works, but if i fill the mistake do not appear but if i "Attach"to the graph the errors is "array out of rage"
How are declared your arrays ?
texoro
126
texoro 2014.04.22 15:34  

in the following way



double MovingAvarage20[],MovingAvarage40[],MovingAvarage50[];
double ma20[], ma40[], ma50[];
whroeder1
14514
whroeder1 2014.04.22 15:52  
Where are you resizing them?
texoro
126
texoro 2014.04.22 16:08  
give me an example thanks
qjol
3243
qjol 2014.04.22 16:13  
a great example already exist in the Docs ArrayResize
texoro
126
texoro 2014.04.23 08:32  
ok, but why should not resized if I gave him greatness?
texoro
126
texoro 2014.04.23 09:11  

i did the modify as your suggestion, the mistake seem resolved but after the restart on "mt4" the program goes on crash and i have to restart again, Here is the code:

   int trendUp=0,trendDown=0;
   double MovingAvarage20[],MovingAvarage40[],MovingAvarage50[];
   double ma20[], ma40[], ma50[];
   int counted_bars = IndicatorCounted();
   int limit=Bars-counted_bars;
     //---- check for possible errors
   if(counted_bars < 0) return(-1);
   //---- last counted bar will be recounted
   if(counted_bars > 0) counted_bars--;
   
   ArrayResize(MovingAvarage20,5);ArrayResize(ma20,5); 
   ArrayResize(MovingAvarage40,5);ArrayResize(ma40,5); 
   ArrayResize(MovingAvarage50,5);ArrayResize(ma50,5);     
   //creo l'array dei valori delle ultime 5 candele delle medie
   for ( int i=0; i< 5; i++)
         {  
         //creo l'array dei valori delle ultime 5 candele della media  a 20 periodi
          MovingAvarage20[i]= iMA(NULL, 0, PeriodMovingAvarage20,0, MODE_EMA, PRICE_CLOSE,i);
          ma20[i]= MovingAvarage20[i];
          
          //creo l'array dei valori delle ultime 5 candele della media  a 40 periodi
          MovingAvarage40[i]= iMA(NULL, 0, PeriodMovingAvarage40,0, MODE_EMA, PRICE_CLOSE,i);
          ma40[i]= MovingAvarage40[i];
          
          //creo l'array dei valori delle ultime 5 candele della media  a 50 periodo
          MovingAvarage50[i]= iMA(NULL, 0, PeriodMovingAvarage50,0, MODE_EMA, PRICE_CLOSE,i);
          ma50[i]= MovingAvarage50[i];
         
         if ((ma20[i] > ma40[i] ) && (ma40[i] > ma50[i]))
            trendUp++;                   
         
         if ((ma20[i] < ma40[i]) && (ma40[i]< ma50[i]))
            trendDown++;   
   }         
       
Keith Watford
Moderator
9624
Keith Watford 2014.04.23 09:33  

What value are you giving PeriodMovingAvarage20 etc

This seems odd anyway

MovingAvarage20[i]= iMA(NULL, 0, PeriodMovingAvarage20,0, MODE_EMA, PRICE_CLOSE,i);

Why not simply

MovingAvarage20[i]= iMA(NULL, 0,20,0, MODE_EMA, PRICE_CLOSE,i);
texoro
126
texoro 2014.04.23 10:18  
input int PeriodMovingAvarage20=20;
input int PeriodMovingAvarage40=40;
input int PeriodMovingAvarage50=50;

To make it easier to edit
123
To add comments, please log in or register