Download MetaTrader 5
To add comments, please log in or register
Freelance service: the safe way to order a trading robot. Try it!
Jason Crossfield
155
Jason Crossfield 2015.12.18 14:14 

Hi.  In researching this I see this was once not possible pre. build 600.  My question is whether it is now possible post build 600?  structures and classes ?   My objective is to pass the array values populated in one function into another function with a global declared array? 

 

For example:

string Opened[][2];

void OnTimer()
 {

int hstTotal=OrdersHistoryTotal(); 

  for(int i=0;i<hstTotal;i++) 
    { 
 
     if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) 
       { 
        Print("Access to history failed with error (",GetLastError(),")"); 
        break; 
       } 

      ArrayResize(Opened,i+1);
      // populate array
      Opened[i][0]=DoubleToStr(OrderStopLoss());
      Opened[i][1]=DoubleToStr(OrderTakeProfit());

    } // for loop

   //.... and then later on
  
   myfunction();

} // end OnTimer

myfunction () {

  // and within this function have Opened[][] values available ??

}
 

 

thanks in advance. 

Shaun Overton
105
Shaun Overton 2015.12.18 20:42  
Why don't you just pass the array arguments into the function?
Jason Crossfield
155
Jason Crossfield 2015.12.18 20:50  

Thanks tex,

 

The array is of a large size, potentially  having a first dimension of over 100 (even 1000).  With it being a dynamic array I was unsure it was possible to pass it as you suggest?

Jason Crossfield
155
Jason Crossfield 2015.12.19 07:42  

Ok now I see thanks tex,

I am attempting to pass the array values to the function like this:

 myfunction(Opened);

 And reference them in the array like this:

myfunction (string& O[][2]) {

  // and within this function have Opened[][] values available ??

}

 However the values of the array when called in myfunction() are 'empty'.

 

Here is now the complete code example:

 

string Opened[][2];

void OnTimer()
 {

int hstTotal=OrdersHistoryTotal(); 

  for(int i=0;i<hstTotal;i++) 
    { 
 
     if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) 
       { 
        Print("Access to history failed with error (",GetLastError(),")"); 
        break; 
       } 

      ArrayResize(Opened,i+1);
      // populate array
      Opened[i][0]=DoubleToStr(OrderStopLoss());
      Opened[i][1]=DoubleToStr(OrderTakeProfit());

    } // for loop
    string $t = "array test", Opened[0][0];
    Print($t); // is successfully printing the array value
  
   myfunction(Opened);

} // end OnTimer

myfunction (string& O[][2]) {

  // and within this function have Opened[][] values available ??
  // example:
  string $test = "test array ",O[0][0];
  Print($test); // returns - "test array " with no output value for the array

}
 
Jason Crossfield
155
Jason Crossfield 2015.12.19 07:45  
And in regards to the array size it hold up to 2147483647 elements.. wow, I doubt I will ever need that many !
honest_knave
969
honest_knave 2015.12.19 10:05  

Perhaps a simple script example of how arrays are passed by reference will help?

#property strict

void OnStart()
  {
   int MyArray[2][2]={0,1,2,3};
   MyFunction(MyArray);        
  } 

void MyFunction(int &array[][]) 
  {
   Print(array[0][0]);
   Print(array[0][1]);
   Print(array[1][0]);
   Print(array[1][1]);
  }
Jason Crossfield
155
Jason Crossfield 2015.12.19 10:27  

knave your a legend!  

 

Thanks my friend for the example.  My problem was adding parameter 2 value in the function array declaration. Now my code is working fine.

 

Again many thanks :-) 

honest_knave
969
honest_knave 2015.12.19 11:04  
jasonxfield:

knave your a legend!  

 

Thanks my friend for the example.  My problem was adding parameter 2 value in the function array declaration. Now my code is working fine.

 

Again many thanks :-) 

Glad you got it sorted!
/
To add comments, please log in or register