Array out of range

 
Why do I get this error here
   int MAhandle;
   
   i=0;
   j=0;
   for(i=0;i<T;i++){
      for(j=0;j<N;j++){
         ArrayResize(MA,0);
         ArrayResize(MA,T+N+1);
         MAhandle=0;
         MAhandle=iMA(symbol,Period(),j+1,0,MODE_EMA,PRICE_MEDIAN);
         while(BarsCalculated(MAhandle)==-1){Sleep(5);}
         if(CopyBuffer(MAhandle,0,0,T+N+1,MA)<=0){
            Alert(" EA Halted. Failed to copy ma handle - Error #",GetLastError());}
         know[i][j]=MA[1+j+i]-MA[2+j+i];// at this point with MA[2+j+i] !!?
         see[i][j]=MA[j+i]-MA[1+j+i];}}

the max index for MA is T+N which is equal to the max value of i+j+2.

I have even tried increasing the maximum size of MA to T+N+ a large number to no avail. What am I missing?

 

Firstly, properly format your code - it is hard to understand without indents

int MAhandle;

i=0;
j=0;
for(i=0;i<T;i++)
  {
   for(j=0;j<N;j++)
     {
      ArrayResize(MA,0);
      ArrayResize(MA,T+N+1);
      MAhandle=0;
      MAhandle=iMA(symbol,Period(),j+1,0,MODE_EMA,PRICE_MEDIAN);
      while(BarsCalculated(MAhandle)==-1){Sleep(5);}
      if(CopyBuffer(MAhandle,0,0,T+N+1,MA)<=0)
        {
         Alert(" EA Halted. Failed to copy ma handle - Error #",GetLastError());
        }
      know[i][j]=MA[1+j+i]-MA[2+j+i];// at this point with MA[2+j+i] !!?
      see[i][j]=MA[j+i]-MA[1+j+i];
     }
  }

 
In second, why don't you  check result of CopyBuffer() - are there enough values in array MA?
 
Rosh:
In second, why don't you  check result of CopyBuffer() - are there enough values in array MA?

I now get error 4401 yet this I wrote this appears before that code
   datetime
   serverdate,
   startdate,
   firstdate;
   
   while(!SeriesInfoInteger(symbol,PERIOD_M1,SERIES_SERVER_FIRSTDATE,serverdate) && !IsStopped())
{
      
Sleep(5);}
      
   if(serverdate > startdate)
{
      
startdate=serverdate;}
   
   if(SeriesInfoInteger(symbol,Period(),SERIES_FIRSTDATE,firstdate))
{
      if(firstdate>0 && firstdate<=startdate)
{
         while(!SeriesInfoInteger(symbol,Period(),SERIES_SYNCRONIZED) && !IsStopped())
{
            Sleep(5);}}}
and I've even tried to make amends like so
   int MAhandle, MAbuffer;
   
   i=0;
   j=0;

   for(i=0;i<T;i++){

      for(j=0;j<N;j++){

         ArrayResize(MA,0);
         ArrayResize(MA,T+N+1);

         MAhandle=0;
         MAhandle=iMA(symbol,Period(),j+1,0,MODE_EMA,PRICE_MEDIAN);

         while(BarsCalculated(MAhandle)==-1){Sleep(5);}

         MAbuffer=0;
         MAbuffer=CopyBuffer(MAhandle,0,0,T+N+1,MA);

         //while(MAbuffer<T+N+1){Sleep(5);}
         if(MAbuffer<=T+N+1){
            Alert(" EA Halted. Could only copy ", MAbuffer, " ma buffers - Error #",GetLastError());}

         know[i][j]=MA[1+j+i]-MA[2+j+i];
         see[i][j]=MA[j+i]-MA[1+j+i];}}
uncommenting buffer sleep just takes EA to limbo!!
 

Rosh

thanks. I seem to have resolved this. I should have had

if(MAbuffer<T+N+1){


and not


if(MAbuffer<=T+N+1){


Thanks for your input


 

You are welcome :)

Sometimes it's enough to tell somebody your question and problem resolves "itself'.


Reason: