Collect values from a function into an Array

 

Hi all,

I'm trying to collect the return values from a function a put them into array. 

I've the doubt if it make sense to collect the values in the function itself or outside?

On the other hand once the values are collected they can be used in array function (as arraymax) right?

this is the function code:

int Crossed1()
  {

   static int CurrentDirection1=0;
   static int LastDirection1=0;
   static bool FirstTime1=true;
   
//----
   if((BB20DO<BB30DO)&& (CandleCLOSE<BB20DO) && (CandleCLOSEp<BB20DOp)  && (RSIi< 30))
      CurrentDirection1=1;  // line1 above line2
   if(BB20DO>BB30DO)
      CurrentDirection1=2;  // line1 below line2
//----
   if(FirstTime1==true) // Need to check if this is the first time the function is run
     {
      FirstTime1=false; // Change variable to false
      LastDirection1=CurrentDirection1; // Set new direction
      return (0);
     }

   if(CurrentDirection1!=LastDirection1 && FirstTime1==false) // If not the first time and there is a direction change
     {
      LastDirection1=CurrentDirection1; // Set new direction
      return(CurrentDirection1); // 1 for up, 2 for down
     }
   else
     {
      return(0);  // No direction change
           
     }
         
  }

thank you in advance for the insights.

best

André

 
Pinto André:

Hi all,

I'm trying to collect the return values from a function a put them into array. 

I've the doubt if it make sense to collect the values in the function itself or outside?

On the other hand once the values are collected they can be used in array function (as arraymax) right?

this is the function code:

thank you in advance for the insights.

best

André

Declaration of the Array depends on what you want to use it with; Declaring it inside or outside a function will change its Scope.

Yes, these Array Related Functions can access Array Values; as long as they are visible where the Function Call is made.


Arrays - Variables - MQL4 Tutorial
Arrays - Variables - MQL4 Tutorial
  • book.mql4.com
Arrays - Variables - MQL4 Tutorial
 
Jeremie Courchesne #:

Declaration of the Array depends on what you want to use it with; Declaring it inside or outside a function will change its Scope.

Yes, these Array Related Functions can access Array Values; as long as they are visible where the Function Call is made.


Thank you Jeremie.

I have been trying to call the function from into another an arraymax function but i've been struggling to add the past values.

I think as the code is (yellow background), i'm only collecting the current value in the array, correct?

If i try to include the (i), it return an error - Wrong parameters count 

 Crossed1(i);


int ArrayCalcMax()
   {   
      int bars = 5;
      int malookback=bars;
     
                  
      double madaily[5];
      double dllv;
      
      for(int i = 0; i < malookback; i++)
      madaily[i] = Crossed1();
      Print(" array[",i,"] = ",madaily[i]);

      int maxPos = ArrayMaximum(madaily,malookback,0); // Check the "maxPos" to make sure it is in range as it could be "-1" if it fails
      Print(" maxPos = ", maxPos );

      if( maxPos >= 0 )
         {
         dllv = madaily[maxPos];  // Please note that "dllv" is a local variable that will be discarded as soon as you return
         Print(" dllv = ", dllv );
         }
   else
      Print("Something is wrong!");

      
   return(dllv);
}

Any thoughts? 

Best

André

 

Hi again,

The error on the array function is solved, it was missing the { } after the for settence.

Nonetheless the value doesn't store in the next calculation:

17:10:44.622 2019.08.23 12:20:00 BBReverse V1 EURUSD,H4: array[0] = 1 

17:10:44.622 2019.08.23 12:20:00 BBReverse V1 EURUSD,H4: array[1] = 0 

17:10:44.622 2019.08.23 12:20:00 BBReverse V1 EURUSD,H4: array[2] = 0

17:10:44.622 2019.08.23 12:20:00 BBReverse V1 EURUSD,H4: array[3] = 0 

17:10:44.622 2019.08.23 12:20:00 BBReverse V1 EURUSD,H4: array[4] = 0 

17:10:44.622 2019.08.23 12:20:00 BBReverse V1 EURUSD,H4: maxPos = 0 

17:10:44.622 2019.08.23 12:20:00 BBReverse V1 EURUSD,H4: dllv = 1

the result for the array [1] should be 1 but appears the value zero.

17:10:44.622 2019.08.23 12:40:00 BBReverse V1 EURUSD,H4: array[0] = 0 

17:10:44.622 2019.08.23 12:40:00 BBReverse V1 EURUSD,H4: array[1] = 0 

17:10:44.622 2019.08.23 12:40:00 BBReverse V1 EURUSD,H4: array[2] = 0

17:10:44.622 2019.08.23 12:40:00 BBReverse V1 EURUSD,H4: array[3] = 0

17:10:44.622 2019.08.23 12:40:00 BBReverse V1 EURUSD,H4: array[4] = 0

17:10:44.622 2019.08.23 12:40:00 BBReverse V1 EURUSD,H4: maxPos = 0

17:10:44.622 2019.08.23 12:40:00 BBReverse V1 EURUSD,H4: dllv = 0

Thanks André

 
Anyone?? really stuck here. :(
 
Your function has no arguments. How can you possibly think it can return more than one value?
 
Documentation on MQL5: Language Basics / Functions
Documentation on MQL5: Language Basics / Functions
  • www.mql5.com
Functions - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

Thank you both for the tips. 

I've been able to solve the issue. 

Best

André

Reason: