I need to do a lot of regression. My regression functions would benefit hugely from being able to return multiple values somehow.
Returning an array (or structure) would be acceptable but I can't find a way to do that.
Is there a recommended way to return two (or more) values from a function?
If its strings you are returning, you could use StringConcatenate() to collapse them into one string and then StringSubstr() to expand them.
Or you could have your function write multiple variables which you declare outside of all functions.
CB
This is related to receiving values by reference in function calls.
The text below was taken from https://docs.mql4.com/basis/variables/formal, which is good for what you need. In the example x,y are output variables.
"
"
It is also possible to pass parameters by reference. In this case, modification of such parameters will be shown in the corresponding variables in the called function passed by reference. Array elements cannot be passed by reference. Parameters can be passed by reference only within one module, such possibility being not provided for libraries. In order to inform that a parameter is passed by reference, it is necessary to put the & modifier after the data type.
Example:
void func(int& x, double& y, double& z[]) { double calculated_tp; ... for(int i=0; i<OrdersTotal(); i++) { if(i==ArraySize(z)) break; if(OrderSelect(i)==false) break; z[i]=OrderOpenPrice(); } x=i; y=calculated_tp; }
"
"
Whippy,
From a previous post I noticed you were having trouble compiling a User-Defined Function. Try something like the following.
I may be wrong but I do not think you can get a function to return two or more values. I suggest that you create a series of User-Defined Functions
for the various time series that you want to pull data from, and the various mathematical calcs that you want to run. Those functions will be structured
very similarly and then call those functions in consequential order inside the start() function and printout the results as shown in the code sample below.
raft
extern int lastelement; extern int firstelement;
int start()
{
int printout;
if(printout==0){
Print("Mean = ",ts_mean(lastelement, firstelement));
printout=1;
}
return(0);
}
//+------------------------------------------------------------------+
//===========================================
// User-Defined Function
//===========================================
double ts_mean(int last, int first){
double sum=0;
//============================================
// Calc arithmetic mean of the specified range of the specified time series
//============================================
for(int i=first; i<last; i++){
sum=sum+Close[i]; //------------<<<< specify the time series here
}
double mean=sum/(last-first);
return(mean);
}
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I need to do a lot of regression. My regression functions would benefit hugely from being able to return multiple values somehow.
Returning an array (or structure) would be acceptable but I can't find a way to do that.
Is there a recommended way to return two (or more) values from a function?