How to return array or specific values from it | MQL5

 

Hello to everyone ! I want to pass multiple values (2 or more) from this array:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

string CheckSMMA_5M()
{
double SMMA_Array_5M[];
int SMMA_M5_Def = iMA (_Symbol,PERIOD_M5,50,0,MODE_SMMA,PRICE_CLOSE);
ArraySetAsSeries(SMMA_Array_5M,true); CopyBuffer(SMMA_M5_Def,0,0,3,SMMA_Array_5M);
myvalue0 = SMMA_Array_5M[0];
myvalue1 = SMMA_Array_5M[1]; // <- I can't pass this 
return myvalue0;
}

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

and the way that I am calling this to main is:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

void OnTick()
{
string temp = CheckSMMA_5M();
string temp1 = CheckSMMA_5M(); // <-Here I wanna bring the the previous value of smma (--SMMA_Array_5M[1]--)
Comment(temp + temp1);
}

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Any suggestions ?

 
MamRa:

Any suggestions ?

(1) Pass any number of variables into the function by reference.

See the second section of:

https://www.mql5.com/en/docs/basis/function/parameterpass

(2) Global variables. (Don't do this, though. It is bad form.)
 
Anthony Garot:

(1) Pass any number of variables into the function by reference.

See the second section of:

https://www.mql5.com/en/docs/basis/function/parameterpass

(2) Global variables. (Don't do this, though. It is bad form.)

Thank You Anthony !

I tried this one :

//+------------------------------------------------------------------+ 
//| Passing parameters by reference                                  | 
//+------------------------------------------------------------------+ 
double SecondMethod(int &i,int &j) 
  { 
   double res; 
//--- 
   i*=2; 
   j/=2; 
   res=i+j; 
//--- 
   return(res); 
  } 
//+------------------------------------------------------------------+ 
//| Script program start function                                    | 
//+------------------------------------------------------------------+ 
void OnStart() 
  { 
//--- 
   int a=14,b=8; 
   Print("a and b before call:",a," ",b); 
   double d=SecondMethod(a,b); 
   Print("a and b after call:",a," ",b); 
  } 
//+------------------------------------------------------------------+ 
//--- result of script execution 
//  a and b before call: 14 8 
//  a and b after call: 28 4

And doesn't work :/

 
Try These : 


int smma_handle;  
double temp[2];  
int OnInit() 
  { 
   smma_handle=iMA (_Symbol,PERIOD_M5,50,0,MODE_SMMA,PRICE_CLOSE);
   return(INIT_SUCCEEDED); 
  }

void OnTick()
{
CopyBuffer(smma_handle,0,0,2,temp);
Comment(temp[0] + temp[1]);
}
 
Lorentzos Roussos:
Try These : 


Thank You (Ευχαριστώ!) for the answer !
The my first code is one external file and I need to calculate 364 MA (Different periods from different Time Frames.
I am search for something faster and lighter . I could calculate 2 times my first code to different strings to get the two latest values from my array.
The reason that I am doing this,is that after the getting of the two latest values from the 364 MA's, I need to send them not directly to my framework, but to a "Check" file, which will do all the comparisons, and after to the Main. 
In PHP is so much easier to return array ! I cannot believe at it's so difficult to here.

I found also this from c++ forum

int * getRandom( ) {

   static int  r[10];

   // set the seed
   srand( (unsigned)time( NULL ) );
   
   for (int i = 0; i < 10; ++i) {
      r[i] = rand();
      cout << r[i] << endl;
   }

   return r;
}

// main function to call above defined function.
int main () {

   // a pointer to an int.
   int *p;

   p = getRandom();
   
   for ( int i = 0; i < 10; i++ ) {
      cout << "*(p + " << i << ") : ";
      cout << *(p + i) << endl;
   }

   return 0;
}

but it doesn't work :/

 
MamRa:

Thank You Anthony !

I tried this one :

And doesn't work :/

What do you mean by "And doesn't work."

This is exactly what I would expect of the code you wrote.

//--- result of script execution 
//  a and b before call: 14 8 
//  a and b after call: 28 4
 
MamRa:

Thank You (Ευχαριστώ!) for the answer !
The my first code is one external file and I need to calculate 364 MA (Different periods from different Time Frames.
I am search for something faster and lighter . I could calculate 2 times my first code to different strings to get the two latest values from my array.
The reason that I am doing this,is that after the getting of the two latest values from the 364 MA's, I need to send them not directly to my framework, but to a "Check" file, which will do all the comparisons, and after to the Main. 
In PHP is so much easier to return array ! I cannot believe at it's so difficult to here.

I found also this from c++ forum

but it doesn't work :/

So , you actually need 182 sets ? (2 for each asset , timeframe ?)

 

MamRa:

In PHP is so much easier to return array ! I cannot believe at it's so difficult to here.

but it doesn't work :/

  1. It's not difficult, just pass an array in by reference and modify the array.

    1. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless.
    2. Your comment shows it did work. You modified two variables in the caller.
 
William Roeder:
  1. It's not difficult, just pass an array in by reference and modify the array.

    1. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless.
    2. Your comment shows it did work. You modified two variables in the caller.


Can you help me on my example ?

 
Lorentzos Roussos:

So , you actually need 182 sets ? (2 for each asset , timeframe ?)

No, I want 360 unique values (for different MA Method, different MA Period and different Time Frame) for each.
So if we calculate the the previous of them, I want 720 values. 

Reason: