array out of range or uninit reason 8 + array size not right

 

Hello everyone,

I would appreciate your help since I am not so familiar with mql4, but trying my best to learn more every day. :)

Thank you in advance.

My goal:

ArraySize (atf) should be 3, since M1, M5 as well as M15 are true.


If I put the code into int OnInit () then the error "uninit reason 8" will be shown:

#property strict


input bool MN1 = false;          // time frame MN1
input bool W1 = false;           // time frame W1
input bool D1 = false;           // time frame D1
input bool H4 = false;           // time frame H4
input bool H1 = false;           // time frame H1
input bool M15 = true;           // time frame M15
input bool M5 = true;            // time frame M5
input bool M1 = true;            // time frame M1

int      array;
int      atfsize;


int OnInit()
  {
EventSetTimer(15);
 // define the possible times frames (tf) and the elements for inputtf
int      atf [];
   int tf [8] =  { 43200, 10080, 1440, 240, 60, 15, 5, 1 };
   bool inputtf [];
   inputtf [0] = MN1;
   inputtf [1] = W1;
   inputtf [2] = D1;
   inputtf [3] = H4;
   inputtf [4] = H1;
   inputtf [5] = M15;
   inputtf [6] = M5;
   inputtf [7] = M1;

   for(array = 0; array < ArraySize (tf) ; array++)
     {
      if (   (inputtf [array] == true)   )      // M1, M5 as well as M15 = true
        {
         atfsize       = ArraySize(atf);        // 1. time = true --> atfsize = 0                                 / 2. time = true --> atfsize = 1                      / 3. time = true --> atfsize = 2  
         ArrayResize(atf, atfsize + 1);         // 1. time = true --> array will be resized --> size will be 1    / 2. time = true --> size = 2                         / 3. time = true --> atfsize = 3 
         atf [atfsize] = tf [array];            // 1. time = true --> element 0 will be tf [array]                / 2. time = true --> element 1 will be tf [array]     / 3. time = true --> element 2 will be tf [array]               
        }
     } 
  Print (ArraySize(atf)); // size should be 3


   return(INIT_SUCCEEDED);
  }

// expert deinitialization function
void OnDeinit(const int reason)
  {
   
  }

// expert tick function
void OnTick()
  {

  }

// timer function
void OnTimer()
  {
   
        
   return;
   }


If I put the code into OnTimer(), then  the error "array out of range " will be shown:

#property strict


input bool MN1 = false;          // time frame MN1
input bool W1 = false;           // time frame W1
input bool D1 = false;           // time frame D1
input bool H4 = false;           // time frame H4
input bool H1 = false;           // time frame H1
input bool M15 = true;           // time frame M15
input bool M5 = true;            // time frame M5
input bool M1 = true;            // time frame M1

int      array;
int      atfsize;


int OnInit()
  {
EventSetTimer(15);
 // define the possible times frames (tf) and the elements for inputtf

   return(INIT_SUCCEEDED);
  }

// expert deinitialization function
void OnDeinit(const int reason)
  {
   
  }

// expert tick function
void OnTick()
  {

  }

// timer function
void OnTimer()
  {
   int      atf [];
   int tf [8] =  { 43200, 10080, 1440, 240, 60, 15, 5, 1 };
   bool inputtf [];
   inputtf [0] = MN1;
   inputtf [1] = W1;
   inputtf [2] = D1;
   inputtf [3] = H4;
   inputtf [4] = H1;
   inputtf [5] = M15;
   inputtf [6] = M5;
   inputtf [7] = M1;

   for(array = 0; array < ArraySize (tf) ; array++)
     {
      if (   (inputtf [array] == true)   )      // M1, M5 as well as M15 = true
        {
         atfsize       = ArraySize(atf);        // 1. time = true --> atfsize = 0                                 / 2. time = true --> atfsize = 1                      / 3. time = true --> atfsize = 2  
         ArrayResize(atf, atfsize + 1);         // 1. time = true --> array will be resized --> size will be 1    / 2. time = true --> size = 2                         / 3. time = true --> atfsize = 3 
         atf [atfsize] = tf [array];            // 1. time = true --> element 0 will be tf [array]                / 2. time = true --> element 1 will be tf [array]     / 3. time = true --> element 2 will be tf [array]               
        }
     } 
  Print (ArraySize(atf)); // size should be 3
        
   return;
   }


And even if I get around that somehow, then the code will show that the ArraySize (atf) is 0 instead of 3.

 
int      array;
int      atfsize = 0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   //--- indicator buffers mapping
   int      atf [];
   int      tf [8] =  { 43200, 10080, 1440, 240, 60, 15, 5, 1 };
   bool inputtf [8];
   inputtf [0] = MN1;
   inputtf [1] = W1;
   inputtf [2] = D1;
   inputtf [3] = H4;
   inputtf [4] = H1;
   inputtf [5] = M15;
   inputtf [6] = M5;
   inputtf [7] = M1;

   for(array = 0; array < ArraySize (tf) ; array++)
   {
      if (inputtf [array] == true) 
      {
         atfsize  += 1; 
         ArrayResize(atf, atfsize); 
         atf [atfsize - 1] = tf [array];   
      }
   }



 
Nagisa Unada #:



Dear Nagisa Unada,

Thank yo so much for your help.

The number 8 in the bool inputtf [8] makes already such a different. :)

All the best for you and Merry Christmas.

 

Hello everyone,

I would appreciate your help with the code below, which is now a little bit longer than at the beginning. :)

The code runs well. At the same time the for-function is running only once, even though the variable c get bigger than 1 after the for-function.

Most likely is there is an issue with "ArrayCopy(closeold, closenew, 0, 0, WHOLE_ARRAY);"

Thank you in advance.

#property strict

double   closex;
double   closey;

int      c=0;
int      k;
int      x;
int      y;
double   z;
string   output;
string   outclosenew;
string   outcloseold;

input bool MN1 = false;          // time frame MN1
input bool W1 = false;           // time frame W1
input bool D1 = false;           // time frame D1
input bool H4 = false;           // time frame H4
input bool H1 = false;           // time frame H1
input bool M15 = true;           // time frame M15
input bool M5 = true;            // time frame M5
input bool M1 = true;            // time frame M1

double closeold [];
double closenew [];

int      array;
int      atfsize=0;
int      closenewsize=0;
int      atf [];

int OnInit()
  {
EventSetTimer(40);
 // define the possible times frames (tf) and the elements for inputtf
   int tf [8] =  { 43200, 10080, 1440, 240, 60, 15, 5, 1 };
   bool inputtf [8];
   inputtf [0] = MN1;
   inputtf [1] = W1;
   inputtf [2] = D1;
   inputtf [3] = H4;
   inputtf [4] = H1;
   inputtf [5] = M15;
   inputtf [6] = M5;
   inputtf [7] = M1;
   
//define the chosen time frames to create a new time frame array 
   for(array = 0; array < ArraySize (tf) ; array++)
     {
      if(   ((inputtf [array]) == true)   )
        {
         atfsize       = atfsize + 1;
         ArrayResize(atf, atfsize);
         atf [atfsize - 1] = tf [array];
        }
      
      //else
      //  {
      //atf [atfsize] = atf [atfsize];
      //  }
     }
   
// sort the time frames from the lowest to the highest   
   ArraySort(atf, WHOLE_ARRAY, 0, MODE_ASCEND);
   
   return(INIT_SUCCEEDED);
  }

// expert deinitialization function
void OnDeinit(const int reason)
  {
   
  }

// expert tick function
void OnTick()
  {

  }

// timer function
void OnTimer()
  {

   for(array = 0; array < ArraySize (atf) ; array++)
   {

//get the close of the last and prelast candle for the time frames chosen
   x=1;
   y=x+1;
   closex               = iClose(Symbol(),atf[array],x);
   closey               = iClose(Symbol(),atf[array],y);
   closenewsize         = closenewsize + 1;
   ArrayResize(closenew, closenewsize);
   closenew [closenewsize - 1] = closex; 
   
      if (  (c <= 0)   )
           {
           k = 1;
           }
           else if   (   (c >= 1) && (closenew [array] != closeold [array])   )
                  {
                  k = 1;
                  }
                  else
                    {
                    k = 0; 
                    }
               
//if close (of prior check) is equal to the close of this check then no comment/print, ELSE comment/print 
      
      if ( (k >= 1)   )
         {
         if  (closenew [array] > closeold [array])
         {
         Comment (Symbol(), atf[array], "long");
         Print (Symbol(), atf[array],"long");
         }
         else
         {
         Comment (Symbol(), atf[array], "short");
         Print (Symbol(), atf[array], "short");
         }
         }

   }
   ArrayCopy(closeold, closenew, 0, 0, WHOLE_ARRAY);
   c = c + 1;
   return;
}