Array out of range

 

Hello guys, im getting the following error: "array out of range in 'IndicadorV1.mq4' (63,5)".


I want to make a code like this:


double=med[];

int OnInit()
  {
   return(INIT_SUCCEEDED);
  }

void OnTick()
{

if(ll!=Bars)

{

ArrayFree(med);
ArrayResize(med,1000,100);

for(int i=0; maxmin==False; i++)

{

 med[i]=iMA(NULL,15,50,15,MODE_SMA,PRICE_CLOSE,i);

.....

.....

}

}

}


I've try changing the ArrayResize and getting it before the med[i], and it works, but it doesnt do anything.

ArrayResize(med,1000,100);

 med[i]=iMA(NULL,15,50,15,MODE_SMA,PRICE_CLOSE,i);


But if i try to change the "1000" for the "i" parameter, then it gives the array out of range error...

ArrayResize(med,i,100);

 med[i]=iMA(NULL,15,50,15,MODE_SMA,PRICE_CLOSE,i);



I have tried to do a not dynamic array, and it doesnt gives the error, but it doesnt do aynthing at all either....


There's any way i could see how the array if getting filled?? im afraid the debugger stops right after the OnInit():

int OnInit()
  {
   return(INIT_SUCCEEDED);
  }


I'll be grateful for any help! Thanks guys.

 
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. double=med[];
  3. Post code that compiles.
  4. ArrayResize(med,1000,100);
    for(int i=0; maxmin==False; i++){
     med[i] == ...
    med has 1000 elements, your loop is infinite unless you change maxmin.
 
WHRoeder:

  1. Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Post code that compiles.
  3. med has 1000 elements, your loop is infinite unless you change maxmin.

Yes, i know. thats why i wrote "......" after the med[i]. Maxmix value becomes True in some point of the code.

But, the most important thing so i can try to fix it myself, i want to know how the array is getting filled? i mean, i want to know the values of med[1], med[2], med[...].


Thanks for your answer!

 
But if i try to change the "1000" for the "i" parameter, then it gives the array out of range error...

ArrayResize(med,i,100);

 med[i]=iMA(NULL,15,50,15,MODE_SMA,PRICE_CLOSE,i);

If you resize an array to i

the array contains

med[0]

to

med[i-1]

med[i] does not exist and so you will get the array out of range error. 

 
GumRai:

If you resize an array to i

the array contains

med[0]

to

med[i-1]

med[i] does not exist and so you will get the array out of range error. 

That's right, now it works! thanks you very much!!

And one last question.... how can I see the values of med[0], med[1],....., med[i]??? my debugger doesnt work.

 
   int as=ArraySize(med);
   for(int x=0;x<as;x++)
      Print("med[",(string)x,"]=",DoubleToStr(med[x],Digits));
That will print them to the expert log
 
GumRai:
That will print them to the expert log

Thank you very much!!

Now I see that all the values of the array are the same, med[0]=med[1]=.....=med[i]

Any idea why this could happend??? thanks!!

 
Compi: Any idea why this could happend???
 med[i]=iMA(NULL,15,50,15,MODE_SMA,PRICE_CLOSE,i);
There are no mind readers here; you need to post the code.We have no idea if i is changing.
 
WHRoeder:
Compi: Any idea why this could happend???
There are no mind readers here; you need to post the code.We have no idea if i is changing.

 

 extern string TRAIL_Ex              = "------- TrailStop";

extern int TrailingStop             = 20; // Trailing Stop loss
extern int perd                       = 50; // Stop loss

extern int y                        = 1; // Numero velas tomadas
extern double x                        = 1; // Valor entre medias
extern int k                           =1; //Diferencia para comprar/vender


bool Sube, Baja, indmax1, indmin1, indmax2, indmin2,contador;
double Lotes,Max1, min1, Max2, min2;
int magic=1145425,ll;
bool maxmin=False;


int OnInit()
  {
//---
   Lotes=Lotese;
//---
   return(INIT_SUCCEEDED);
  }

void OnTick()
{
//---
if(ll!=Bars)
{

double med[];
double MA;

MA=iMA(NULL,0,50,15,MODE_SMA,PRICE_CLOSE,0);

ArrayFree(med);

for (int n=0; n<y; n++)
{

ArrayResize(med,n+1);

 med[n]=iMA(NULL,15,50,15,MODE_SMA,PRICE_CLOSE,n);
}

for(int i=y; maxmin==False; i++) 
{

ArrayResize(med,i+1);

 med[i]=iMA(NULL,15,50,15,MODE_SMA,PRICE_CLOSE,i);
 
 if ((med[i-y]-med[i]<0) && (Sube==False))
 {
  Baja=True;
 }
 
 if ((med[i-y]-med[i]>0) && (Baja==False))
 {
  Sube=True;
 }


 if ((med[i-y]-med[i]>0) && (Baja==True) && (Sube==False))
 {
  Baja=False;
  Sube=True;
  if (indmin1==False)
  {
    indmin1=True;
   }
    else if (indmin1==True)
   {
    indmin2=True;
    }
   }
 
 if ((med[i-y]-med[i]<0) && (Sube==True) && (Baja==False))
 {
  Baja = True;
  Sube=False;
  if (indmax1==False)
  {
    indmax1=True;
   }
  else if (indmax1==True)
   {
    indmax2=True;
  }
 }
 if (indmax2==True || indmin2==True)
 {
  maxmin=True;
 }
}



if ((indmin2==True) && (Ordenes(magic)<maxor) && (med[0]>10*Point*k))
{
 int res=OrderSend(NULL,OP_BUY,Lotes,Ask,0,0,0,"Compi",magic,0,Green);
 indmax1=False;
 indmax2=False;
 maxmin=False;
 Sube=False;
 Baja=False;
 indmax1=false;
 indmin1=false;
}

if ((indmax2==True) && (Ordenes(magic)<maxor) && (med[0]<10*Point*k))
{
 int res=OrderSend(NULL,OP_SELL,Lotes,Bid,0,0,0,"Compi",magic,0,Red);
 indmax1=False;
 indmax2=False;
 maxmin=False;
 Sube=False;
 Baja=False;
 indmax1=false;
 indmin1=false;
}

 

   //Trail largo

 if((Bid-OrderOpenPrice())>10*Point*TrailingStop && (OrderType()==OP_BUY))
 {
  TrailingStopLoss(magic);
  contador=True;
 }
 
   //Trail corto
 if((OrderOpenPrice()-Ask)>10*Point*TrailingStop && (OrderType()==OP_SELL))
 {
  TrailingStopLossCorto(magic);
  contador=True;
 }
  

 

Here's the code.

 

 med[i]=iMA(NULL,15,50,15,MODE_SMA,PRICE_CLOSE,i);    

The first "i" in yellow changes, because it prints the med[1],med[2],....,med[30],..... med[i]

The thing is all the values are the same, as if the second "i" in red does not change...

Thanks guys! 

 


 
I have absolutely no idea what you are trying to achieve with your code. I find it impossible to follow the logic. Well, I may be able to given time. But I am not going to spoend 2 or 3 hours trying to unscramble your code.
 
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. for (int n=0; n<y; n++){
     ArrayResize(med,n+1);
     med[n]=iMA(NULL,15,50,15,MODE_SMA,PRICE_CLOSE,n);
    }
    
    Here you could resize the array once to y before the loop.
  3. for(int i=y; maxmin==False; i++){
     ArrayResize(med,i+1);
     med[i]=iMA(NULL,15,50,15,MODE_SMA,PRICE_CLOSE,i);
    Here you are in an infinite loop. Should the condition never occur, you will be putting zeros in the array. If you used #property strict you would have errored instead.
  4. The first "i" in yellow changes, because it prints the med[1],med[2],....,med[30],..... med[i]

    The thing is all the values are the same, as if the second "i" in red does not change...

    You say that but you have no code to do that, and don't show the result. Don't tell us what you think it says, show us. There are no mind readers here.
  5. int magic=1145425,ll;
    :
    void OnTick()
    {
    //---
    if(ll!=Bars)
    ll has no value (random) so the comparison is meaningless.
  6. I aggree with GumRai, the rest is unreadable.
Reason: