laman
laman

Reputation: 562

How to code a Multidimensional Dynamic Array?

How to code a Multidimensional Dynamic Array in MQL4? I'm fairly new to coding MQL4. Currently coding my first EA and just learnt about Arrays. I was wondering, how to code a dynamic array?

What I'm trying to do is when my EA is initialized, for the past 100 bars, find out the Highest 50 bars and save and name them accordingly, then out of the 50 bars, find out the top 10 with the Highest Trading Volume and save them and name them again. I'm thinking using dynamic array to save the bars but I don't know how to do it.

Upvotes: 4

Views: 6013

Answers (3)

Aftershock
Aftershock

Reputation: 5351

A more dynamic solution..

struct grouptype
{

string elements[];
};

grouptype Groups[2];

Upvotes: 0

Daniel Kniaz
Daniel Kniaz

Reputation: 4681

Nothing special, just using regular tools:

double array[][2];
int    size = 100;

void FunctionArray(){
     ArrayResize( array, size );
     for( int i = 0; i < size; i++ ){
          array[i][0] =          iHigh(   _Symbol, 0, i );
          array[i][1] = (double) iVolume( _Symbol, 0, i );
     }

// Print( __LINE__, " ", array[0][0], " ", array[1][0], " ", array[2][0], " ", array[3][1], " ", array[size-1][0], " ", array[size-1][1] );

   ArraySort( array, WHOLE_ARRAY, 0, MODE_DESCEND );

// Print( __LINE__, " ", array[0][0], " ", array[0][1] );

   double     new50Array[50][2];
   ArrayCopy( new50Array, array, 0, 0, size );          // block-copying

// Print( __LINE__, " ", array[0][0], " ", array[0][1], " ", array[1][0], " ", array[1][1], " ", array[49][0], " ", array[49][1] );
   }

and same for volumes - you need to develop own tool as ArraySort() is running only for the first element;
alternatively - copy by element into a new50Array[][]
but iVolume() at first place
and iHigh() at second,
instead of 'copying', and then call ArraySort() again

Upvotes: 2

Daniel Kniaz
Daniel Kniaz

Reputation: 4681

Define multidimensional array: double array[][2];

Check array functions ArrayCopySeries(), ArraySort().

ArrayCopyRates() is not useful, if you need to sort using some, not fist dimension, I am afraid.

All documentation is here

Upvotes: -2

Related Questions