Download MetaTrader 5

Alternate ways of coding arrays.

To add comments, please log in or register
imamushroom
238
imamushroom  

Hi,


Currently, I have a large list of arrays containing different information and I'm initialising them sequentially as so...

double array1[];
ArrayResize(array1,4);
ArrayInitialize(array1,0.0);

double array2[];
ArrayResize(array2,4);
ArrayInitialize(pArray,0.0);

double array3[];
ArrayResize(array3,4);
ArrayInitialize(array1,0.0);

This seems a laborious way of doing it.

What I want to be able to do is define an array with a list of the arrays to define and loop around this array initialising them individually such as below (non-working code)...

string list[] = {"array1","array2","array3"};

for(int i=0; i<ArraySize(list); i++) {
   ArrayResize(list[i],4);
   ArrayInitialize(list[i],0.0);
}

What's the best way to achieve this. Any example code please?

TIA.

timkrug
39
timkrug  
imamushroom:

Hi,


Currently, I have a large list of arrays containing different information and I'm initialising them sequentially as so...

This seems a laborious way of doing it.

What I want to be able to do is define an array with a list of the arrays to define and loop around this array initialising them individually such as below (non-working code)...

What's the best way to achieve this. Any example code please?

TIA.


 Multidimensional array?

double array[][1000];
ArrayResize(array,4);
ArrayInitialize(array,0.0);
Alain Verleyen
Moderator
35138
Alain Verleyen  
imamushroom:

Hi,


Currently, I have a large list of arrays containing different information and I'm initialising them sequentially as so...

This seems a laborious way of doing it.

What I want to be able to do is define an array with a list of the arrays to define and loop around this array initialising them individually such as below (non-working code)...

What's the best way to achieve this. Any example code please?

TIA.

Use a struct, or a full OOP approach like Standard Library.

struct SArray
  {
   double            data[];

   void Init(int size,double value)
     {
      ArrayResize(data,size);
      ArrayInitialize(data,value);
     }
  };

SArray myArrays[3];

void OnStart()
  {
   for(int i=0;i<3;i++)
     {
      myArrays[i].Init(4,0.0);
     }
  }
imamushroom
238
imamushroom  
timkrug:


 Multidimensional array?

Thanks. I find keeping track of which element contains what value can be confusing. But thanks for your suggestion.

imamushroom
238
imamushroom  
Alain Verleyen:

Use a struct, or a full OOP approach like Standard Library.

Thanks, I had thought of using a struct so thanks for the suggestion. I will look into this further. As for the OOP method, I'm not a fan of how arrays are done in the standard library but that just might be me.

Thanks for your suggestion.

Icham Aidibe
10815
Icham Aidibe  
imamushroom:

Thanks, I had thought of using a struct so thanks for the suggestion. I will look into this further. As for the OOP method, I'm not a fan of how arrays are done in the standard library but that just might be me.

Thanks for your suggestion.

#include <Arrays\ArrayObj.mqh>
#include <Arrays\ArrayDouble.mqh>
   
CArrayObj *MyListOfArray = new CArrayObj(); 
CArrayDouble *MyArrays;

// Set values
do { MyArrays = new CArrayDouble(); MyArrays.Add(0.0); MyListOfArray.Add(MyArrays); } while (MyListOfArray.Total()<4);

// Get values
for (int i=0;i<MyListOfArray.Total();i++) {
MyArrays = MyListOfArray.At(i);
printf("*** MyListOfArrays (%g) > Array number : %g - value : %g ",MyListOfArray.Total(),i,MyArrays.At(0));
}
imamushroom
238
imamushroom  
Icham Aidibe:

Thanks for the example, I'm still not a fan I'm afraid but I appreciate your input.

imamushroom
238
imamushroom  
Alain Verleyen:

Use a struct, or a full OOP approach like Standard Library.

I have a problem calling one of the struct methods and wonder if someone could help me. I'm sure it's due to my lack of knowledge of passing Multidimensional arrays into methods and struct's.


I currently have this code:

struct Struct_PriceArray

  {

   double            data[][2];



   void Init(int pSize,double pValue)

     {

      ArrayResize(data,pSize);

      ArrayInitialize(data,pValue);

      ArraySetAsSeries(data,true);

     }



   void Populate(string pSymbol,int pTimeframe,datetime pStart,int pSize,int pTfIndex)

     {

      Init(pSize,0.0);

      CopyHigh(pSymbol,pTimeframe,pStart,pSize,data[pTfIndex][0]);

      CopyLow(pSymbol,pTimeframe,pStart,pSize,data[pTfIndex][1]);

      ArraySetAsSeries(data,true);

     }

  };



struct Struct_BbArray

  {

   double            data[][2][3];



   void Init(int pSize,double pValue)

     {

      ArrayResize(data,pSize);

      ArrayInitialize(data,pValue);

      ArraySetAsSeries(data,true);

     }



   void Populate(double &pHigh[],double &pLow[],int pSize,int pTimeframe,int pMaPeriod,double pDeviation,int pMaShift)

     {

      Init(pSize,0.0);

      for(int a=0; a<pSize; a++)

        {

         data[a][0][0] = NormalizeDouble(iBandsOnArray(pHigh[a][0],0,pMaPeriod,pDeviation,pMaShift,MODE_UPPER,a),_Digits);

         data[a][0][1] = NormalizeDouble(iBandsOnArray(pHigh[a][0],0,pMaPeriod,pDeviation,pMaShift,MODE_MAIN,a),_Digits);

         data[a][0][2] = NormalizeDouble(iBandsOnArray(pHigh[a][0],0,pMaPeriod,pDeviation,pMaShift,MODE_LOWER,a),_Digits);



         data[a][1][0] = NormalizeDouble(iBandsOnArray(pLow[a][1],0,pMaPeriod,pDeviation,pMaShift,MODE_UPPER,a),_Digits);

         data[a][1][1] = NormalizeDouble(iBandsOnArray(pLow[a][1],0,pMaPeriod,pDeviation,pMaShift,MODE_MAIN,a),_Digits);

         data[a][1][2] = NormalizeDouble(iBandsOnArray(pLow[a][1],0,pMaPeriod,pDeviation,pMaShift,MODE_LOWER,a),_Digits);

        }

     }

  };



Struct_PriceArray pa[];

Struct_BbArray bba[];



      for(int a=0;a<ArraySize(timeFrame);a=+2)

         pa[a].Populate(_Symbol,timeFrame[a],Time[i],6,a);



      for(int a=0;a<ArraySize(timeFrame);a++)

         bba[a].Populate(pa[a],pa[a],ArraySize(timeFrame),timeFrame[a],MaPeriod,Deviation,MaShift);

When I try compiling this I get the following error:


'pa' - parameter conversion not allowed 

The code is to populate the arrays with the relevant values.


I've tried changing every combination of method defines and parameters but haven't found a solution to the problem yet.


Could someone tell me where I'm going wrong please?


Thanks in advance.

Alain Verleyen
Moderator
35138
Alain Verleyen  
 CopyHigh(pSymbol,pTimeframe,pStart,pSize,data[pTfIndex][0]);

This is not allowed. Last parameter of CopyHigh() must be a one dimension array.

imamushroom
238
imamushroom  

Thanks. That may be another problem and I'll check up on that.

However, the compiler is complaining about this line

         bba[a].Populate(pa[a],pa[a],ArraySize(timeFrame),timeFrame[a],MaPeriod,Deviation,MaShift);

specifically, the first 

pa[a]

reference.

With the error:

'pa' - parameter conversion not allowed

I've tried all ways of passing it but nothing worked. What else am I doing wrong?

TIA

imamushroom
238
imamushroom  

Fixed it.

My silly error! Had a double defined in the method call but passing a Struct!

Needed this line:

   void Populate(Struct_PriceArray &pHigh[],Struct_PriceArray &pLow[],int pSize,int pTimeframe,int pMaPeriod,double pDeviation,int pMaShift)

Thanks for everyone's support!

12
To add comments, please log in or register