Create array of array in mq4

1.2k views Asked by At

How to create a array of array in mq4?

I have a function with this pararameters:

double & v1[], double & v2[], double & v3[], double & v4[]

I want to create a array where each position has a reference to another array like:

double v[];

v[0] = v1;
v[1] = v2;
v[2] = v3;
v[3] = v4;

and then iterate like:

v[0][2] == v1[2]; // true

It's possible to do something like that? How can I do it?

1

There are 1 answers

1
whitebloodcell On

You have pretty much already answered yourself already. A 2D array can be thought of/imagined as an array with each cell of the first array containing a 1D array of the specified size. Similarly, a 3D array could be imagined as a 1D array containing a 2D array in each cell. So instead of passing in v1,v2,v3,v4 you could just have the input parameter as double &v[4][6] and loop through them.

TestFunction(double &v[4][6]) 
{
    for(int i=0;i<4;i++)
    {
       for(int j=0;j<6;j++)
       {
          v[i][j] = 0; 
       }
    }
}

If the arrays v1,v2,v3,v4 in your example are different sizes then you could created an array of CArrayDouble objects and pass that. E.g.

CArrayDouble TestArray2[4]

void TestFunction2(CArrayDouble &v[4])
{
   for(int i=0;i<4;i++)
   {
      for(int j=0;j<v[i].Total();j++)
      {
         v[i][j];
      }   
   }
}

In answer to your comment, if you are unable to change the function signature. You could copy the arrays into an instance of CArrayDouble.

CArrayDouble ArrayOfArray[4];
ArrayOfArray[0].AssignArray(v1);
ArrayOfArray[1].AssignArray(v2);
ArrayOfArray[2].AssignArray(v3);
ArrayOfArray[3].AssignArray(v4);

If the arrays v1,v2 etc are buffers and thus change in size on every new bar, I would declare the CArrayDouble as static and after the initial copying (which is what AssignArray does), add each new element from the arrays as and when the function is called (using the member function 'Add').