Custom Function returning multiple values - Please help!

 

Hello Everyone,

I am unable to figure out how to code a custom function that returns multiple values. Here is some code I started with for your review. 

I have no clue what syntax, format & structure to use.

I'm sure it is crystal clear to everyone else and I'm hoping someone can help.

Thank you for your time and consideration!


My questions:

a) where would this function be located? onStart, onCalc, external, within onTick

b) where do I declare variables? inside brackets, locally, externally, where the sun doesn't shine?

c) after calculations and values are returned, how do I associate, format & use them? Can I keep the same variable names while passing/returning?

 

Thanks again for affording me thew opportunity to learn something!

void OnTick()
  {
     // string variables for custom candle values 
           double lowest_price_candle;
           double highest_price_candle;
           double average_price_candle;
     
     // call this custom function Hi_Lo_Avg_Candles to calculate hi, lo & avg candle values
         
     Hi_Lo_Avg_Candles=(lowest_price_candle highest_price_candle average_price_candle)// do i declare variables inside the parenthathese? or external? (function will be external)
     
     //?lowestCandlePrice;
     //?highest_price_candle=Hi_Lo_Avg_Candles(double lowestCandlePrice, double highestCandlePrice, double averageCandlePrice);
     //?Hi_Lo_Avg_Candles(double lowestCandlePrice, double highestCandlePrice, double averageCandlePrice);
     
     // Chart output for the values / I know how to do this part!
     Comment("lowestCandlePrice = ",lowestCandlePrice,"\n","highestCandlePrice = ",highestCandlePrice,"\n"," averageCandlePrice = ",averageCandlePrice);
      

      //do I use void?, start, onCalc? Lord pls help me!
      void Hi_Lo_Avg_Candles()
      // ? after the values are returned, how do I associate them with variables & how to be sure these values are passed to the local use
               (double lowestCandlePrice, double highestCandlePrice, double averageCandlePrice)
               
               
      //-----------------------------------------------------------code to get hi lo & avg candle values------------------------------------------------
            {     int number_of_candles=12;
                  int lowestCandle;
                  double low[];
                  double lowestCandlePrice;
                  double high[];
                  int highestCandle;
                  double highestCandlePrice;
                  double averageCandlePrice;
                  
                  ArraySetAsSeries(low,true);
                  ArraySetAsSeries(high,true);
                  ArraySetAsSeries(rate_data,true);
                  MqlRates rate_data[];
                  
                  CopyHigh(_Symbol,_Period,0,number_of_candles,high);
                  CopyLow(_Symbol,_Period,0,number_of_candles,low);
                  
                  highestCandle=ArrayMaximum(high,0,number_of_candles);
                  lowestCandle=ArrayMinimum(low,0,number_of_candles);
                  
                  int data=CopyRates(_Symbol,_Period,0,number_of_candles,rate_data);
               // int data=CopyRates(_Symbol,_Period,0,Bars(Symbol(),Period()),rate_data);   
                  
                  lowestCandlePrice=rate_data[lowestCandle].low;
                  highestCandlePrice=rate_data[highestCandle].high;
                  averageCandlePrice=MathRound(NormalizeDouble(((highestCandlePrice+lowestCandlePrice)/2),2)*4)/4;
                  
                  return(lowestCandlePrice;highestCandlePrice;averageCandlePrice); 
            }
  }
//+------------------------------------------------------------------+
 
cowmodee:

Hello Everyone,

I am unable to figure out how to code a custom function that returns multiple values. 

Passing parameters by reference

Passing Parameters - Functions - Language Basics - MQL4 Reference
Passing Parameters - Functions - Language Basics - MQL4 Reference
  • docs.mql4.com
There are two methods, by which the machine language can pass arguments to a subprogram (function). The first method is to send a parameter by value. This method copies the argument value into a formal function parameter. Therefore, any changes in this parameter within the function have no influence on the corresponding call argument...
 
Mohamad Zulhairi Baba:

Passing parameters by reference

I do not understand how this information applies to my question.

I have a function that when called returns three values. 

How do I assign/associate/define & use each of those values when they are returned?

What is the structure, syntax & method?

How do I get to the values that are returned?

 Hi_Lo_Avg_Candles=(  how?=>  lowest_price_candle,
		      how?=>  highest_price_candle 
		      how?=>  average_price_candle      )
 
cowmodee:

I do not understand how this information applies to my question.

I have a function that when called returns three values. 

  1. No you don't.
  2. void function return nothing.
  3. and you code doesn't even compiled.
  4. have you look at the link that i gave?
  5. you need to pass the parameters by reference (those 3 values), before you can use them. doesn't matter in OnticK() or OnCalculate() or anywhere else..
hope it helps.

 
Mohamad Zulhairi Baba:

  1. No you don't.
  2. void function return nothing.
  3. and you code doesn't even compiled.
  4. have you look at the link that i gave?
  5. you need to pass the parameters by reference (those 3 values), before you can use them. doesn't matter in OnticK() or OnCalculate() or anywhere else..
hope it helps.

how am I supposed to compile it when I do not know how to set it up?

ok so I do not use void. thank you!

yes I looked at the link. did you see my reply? I do not understand it. the link references passing the values "from" the function. 

How do I use these values when returned from the function?


I am looking for the syntax, structure  & method


if there were just "one" variable I know this would work: lowestCandlePrice=Hi_Lo_Avg_Candles();


this does not work

double lowestCandlePrice=Hi_Lo_Avg_Candles(double lowestCandlePrice);

this does not work

Hi_Lo_Avg_Candles(); double lowestCandlePrice; double highestCandlePrice; double averageCandlePrice;

this does not work

Hi_Lo_Avg_Candles(double lowestCandlePrice, double highestCandlePrice, double averageCandlePrice);

this does not work

Hi_Lo_Avg_Candles(lowestCandlePrice, highestCandlePrice, averageCandlePrice);

this does not work

Hi_Lo_Avg_Candles(lowestCandlePrice; highestCandlePrice; averageCandlePrice;);


Can you please complete the sentence?

How do you define each value that is returned?

 
cowmodee:

how am I supposed to compile it when I do not know how to set it up?

ok so I do not use void. thank you!

yes I looked at the link. did you see my reply? I do not understand it. the link references passing the values "from" the function. 

How do I use these values when returned from the function?


I am looking for the syntax, structure  & method


if there were just "one" variable I know this would work: lowestCandlePrice=Hi_Lo_Avg_Candles();


this does not work

double lowestCandlePrice=Hi_Lo_Avg_Candles(double lowestCandlePrice);

this does not work

Hi_Lo_Avg_Candles(); double lowestCandlePrice; double highestCandlePrice; double averageCandlePrice;

this does not work

Hi_Lo_Avg_Candles(double lowestCandlePrice, double highestCandlePrice, double averageCandlePrice);

this does not work

Hi_Lo_Avg_Candles(lowestCandlePrice, highestCandlePrice, averageCandlePrice);

this does not work

Hi_Lo_Avg_Candles(lowestCandlePrice; highestCandlePrice; averageCandlePrice;);


Can you please complete the sentence?

How do you define each value that is returned?

Hi cowmodee:

for return multiple value from function, see follow example

double MyMovinAverageSimple(int period, double &multipleValueReturn)
{
        double sum = 0.0;
        for( int i = 0; i < period; i++ )
        {
                sum += Close[i];
        }
        multipleValueReturn = sum; //in this mode you assign the sum price to reference parameter
        return sum / period; //this is the main return of function
}

how to use it

int OnTick()
{
        double MyMultipleReturn;
        double MyAvg = MyMovingAverageSimple(20, MyMultipleReturn);
        //at this point of code you will found two value the moving average value and the sum of all price
        //THIS IS ONLY AN EXAMPLE FOR RETURN ONE OR MORE MULTIPLE VALUES FROM A FUNCTION
}
 

i think you need to declare the variables on global variables, because if you place the variables inside the functions, it will reset the previous stored value each new cycle

 
I write that is an example
 

Hi, newbie here.  i'm trying to convert a repetitive 'if-else' statement into a function the "if" statement works as stand-alone but when inside a function, i keep getting 'not all control paths return a value' error message. How do i tell the function to "Do Nothing" if they do not return any value? is this the right way?

Appreciate pointers. Here's the code : 

double DeMPriceChangeRatio = //<--this variable is obtained from a separate function and suppose to pass to the function below

static string DeMDir;

CommentandAlertfn (double DeMPriceChangeRatio,string DeMDir);   //<-- is this call function correct?


//this Function to output to a Comment and alert. This function is located as global.

      void CommentandAlertfn (double DeMPriceChangeRatio, string DeMDir ) {   //<--this "if" statement works as stand-alone but as a function, i keep getting 'not all control paths return a value' error message.

.

      if (DeMPriceChangeRatio >=0 && DeMDir!="POS")

              {DeMDir ="POS";

              Alert (_Symbol,  " ", _Period, "  NEG to ", DeMDir);

              }

                        

          if (DeMPriceChangeRatio <0 && DeMDir != "NEG")

               {DeMDir = "NEG"; 

              Alert (_Symbol, " ", _Period, "  POS to ", DeMDir);

               }

      }//end of RecordPosorNegfn function


Comment ( "\nDeM = ",  DeMDir) ;

Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
Predefined Macro Substitutions - Named Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Eugenio Bravetti:

Hi  cowmodee:

for return multiple value from function, see follow example

how to use it

Hi, i'm new and wish to seek understanding on your above reply to cowmodee.

When you use 

double MyMultipleReturn;

dont we need to pass 2 arguments to the Function since it requires 2 arguments to work right? why is it MyMultipleReturn has no argument ?

 
JimSingadventure:

Please edit your post and use the code button (Alt+S) when pasting code.

EDIT your original post, please do not just post the code correctly in a new post.

Reason: