Download MetaTrader 5
To add comments, please log in or register
Enable MQL5 Storage to store your source codes. It is free!
Harry
267
Harry 2014.07.07 19:58 

Hi Everyone,

I try to work with array and write this simple code.

I am not sure why it show the "array out of range" right away at the cutup[I]=High[I].

Could you please help me to correct it?

Thank you so much,

SCFX

   int limit=rates_total-prev_calculated-1;
   if(limit<=0) limit=1;
   
double cutup[];

//---
for(int i=1; i<=limit ;i++)
{
   buy[i]=0;
   //cut up
   if(   (Close[i]>iMA(NULL,0,ma1,0,1,0,i) && Close[i+1]<iMA(NULL,0,ma1,0,1,0,i+1))  )
   cutup[i]=High[i]; //store value to that variable  
   
   buy[i]=cutup[i];
   
}
Keith Watford
Moderator
8016
Keith Watford 2014.07.07 20:10  
  
   //IF 50 BARS ON CHART EARLIEST BAR WILL BE INDEX 49
   int limit=rates_total-prev_calculated-1;
   // AT FIRST RUN 50-0-1 , LIMIT WILL EQUAL 49
   if(limit<=0) limit=1;
   
double cutup[];

//---
for(int i=1; i<=limit ;i++)
{
   buy[i]=0;
   //cut up
   if(   (Close[i]>iMA(NULL,0,ma1,0,1,0,i) && Close[i+1]<iMA(NULL,0,ma1,0,1,0,i+1))  )
   //i+1 IS TRYING TO ACCESS BAR INDEX 50 WHICH DOESN'T EXIST
   cutup[i]==High[i]; //store value to that variable  
   
   buy[i]=cutup[i];
   
}
You have had this type of error pointed out in your code in your previous posts.
Ali Akcaagac
169
Ali Akcaagac 2014.07.07 20:11  

Tried to give your array a size ?

double cutup[1024];

might help. Maybe you need to create an even bigger array 4096/8192/...

cutup[i]==High[i]; //store value to that variable

This doesn't story anything. == is a comparison.

cutup[i]=High[i]; //store value to that variable

This will store your value.

Harry
267
Harry 2014.07.07 20:22  

Hi GumRai,

I am not sure if it is the cause . If I simply skip the cutup step and put buy=High directly, there is no error at all.

I see the logic behind that 50 and 49 and it make me confused why no "array out of range" error show up.

   int limit=rates_total-prev_calculated-1;
   if(limit<=0) limit=1;
   
double cutup[];

//---
for(int i=1; i<=limit ;i++)
{
   buy[i]=0;
   //cut up
   if(   (Close[i]>iMA(NULL,0,ma1,0,1,0,i) && Close[i+1]<iMA(NULL,0,ma1,0,1,0,i+1))  )
   //cutup[i]==High[i]; //store value to that variable  
   
   buy[i]=High[i];



I also change to below limit but still cannot solve the issue

   int limit=rates_total-prev_calculated-2;
   if(limit<=0) limit=2;


that trick double cutup[1024]; is not working either.


SCFX

Keith Watford
Moderator
8016
Keith Watford 2014.07.07 20:34  
scfx:

I am not sure why it show the "array out of range" right away at the cutup[I]=High[I].


scfx:

I see the logic behind that 50 and 49 and it make me confused why no "array out of range" error show up.


 Are you getting "array out if range " or not?

You seem to be contradicting yourself. 

Harry
267
Harry 2014.07.07 21:12  
GumRai:

 Are you getting "array out if range " or not?

You seem to be contradicting yourself. 



What I mean is that this code below has No "array out of range' error, even though I only skip the cutup step

 //cutup[i]==High[i]; //store value to that variable  

and put

   buy[i]=High[i];

That's why I don't think the 50 to 49 is the reason for the error in the original code.

   int limit=rates_total-prev_calculated-1;
   if(limit<=0) limit=1;
   
double cutup[];

//---
for(int i=1; i<=limit ;i++)
{
   buy[i]=0;
   //cut up
   if(   (Close[i]>iMA(NULL,0,ma1,0,1,0,i) && Close[i+1]<iMA(NULL,0,ma1,0,1,0,i+1))  )
   //cutup[i]==High[i]; //store value to that variable  
   
   buy[i]=High[i];
Keith Watford
Moderator
8016
Keith Watford 2014.07.08 00:26  
scfx:



What I mean is that this code below has No "array out of range' error, even though I only skip the cutup step

 //cutup[i]==High[i]; //store value to that variable  

and put

That's why I don't think the 50 to 49 is the reason for the error in the original code.


Oh, I see.

You need to ReSize the array cutup 

Harry
267
Harry 2014.07.08 02:56  
GumRai:


Oh, I see.

You need to ReSize the array cutup 



So I do the following resize. Is that the right way to do it?

The indicator at least works now.

Thank you,

SCFX

   double ch[];
   ArrayResize(ch, rates_total);  
Harry
267
Harry 2014.07.08 03:08  

I want to give value when cut happened to both: bar[I] and bar[I-1].

This code below give value on bar[I] but it sometime give value to bar[I-1], sometime not.

Could you help me see why it is like that?

Thank you,

SCFX

   int limit=rates_total-prev_calculated-1;
   if(limit<=0) limit=1;
   
   double ch[];
   ArrayResize(ch, rates_total);   


//---
for(int i=1; i<=limit ;i++)
{
   buy[i]=0;

   if(   (Close[i]>iMA(NULL,0,ma1,0,1,0,i) && Close[i+1]<iMA(NULL,0,ma1,0,1,0,i+1))   )
         
  ch[i]=High[i];
  
  if(ch[i]>0)  
  {   buy[i]=ch[i];
      buy[i-1]=ch[i];}
Keith Watford
Moderator
8016
Keith Watford 2014.07.08 03:32  
scfx:



So I do the following resize. Is that the right way to do it?

The indicator at least works now.

Thank you,

SCFX

   double ch[];
   ArrayResize(ch, rates_total);  

 

 As the array is declared locally, you will be be allocating many more bytes to the array than necessary.

If you want to store the values in the array, declare it globally

else

you will only need it to be large enough to work with the last few bars (following the initial tick)

ArrayResize(ch, limit+1);

 should be enough

Harry
267
Harry 2014.07.08 21:22  

Thanks, GumRai.

SCFX

/
To add comments, please log in or register