Custom Function returning multiple values - Please help!

To add comments, please log in or register
cowmodee
64
cowmodee  

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); 
            }
  }
//+------------------------------------------------------------------+
Mohamad Zulhairi Baba
16044
Mohamad Zulhairi Baba  
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...
cowmodee
64
cowmodee  
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      )
Mohamad Zulhairi Baba
16044
Mohamad Zulhairi Baba  
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.

cowmodee
64
cowmodee  
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?

Eugenio Bravetti
5037
Eugenio Bravetti  
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
}
Mrluck07
425
Mrluck07  

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

Eugenio Bravetti
5037
Eugenio Bravetti  
I write that is an example
To add comments, please log in or register