3

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.

Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58
laman
  • 552
  • 6
  • 18

3 Answers3

2

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

user3666197
  • 1
  • 6
  • 50
  • 92
Daniel Kniaz
  • 4,603
  • 2
  • 14
  • 20
0

A more dynamic solution..

struct grouptype
{

string elements[];
};

grouptype Groups[2];
Aftershock
  • 5,205
  • 4
  • 51
  • 64
-1

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

user3666197
  • 1
  • 6
  • 50
  • 92
Daniel Kniaz
  • 4,603
  • 2
  • 14
  • 20
  • IMHO, 2-cells in the 2nd dimension are not enough for working with values needed for laman's defined processing, 3+ values are needed to get stored / processed. Also the reason for using the indicated dynamic arrays seems not to be clear, once 100 bars, 50 bars and 10 bars requirements were given in the O/P. StackOverflow also encourages users to *rather* post a copy of a remote-site content, than to leave just a link to the content, without any elaboration on that remote content. Such content may become unavailable or inaccessible in some nearer of farther future, so you might want to edit it – user3666197 Sep 24 '16 at 15:10