ArrayMinumun () returning last value

 

Hi all,

I'm trying to find the lowest value in an array that is determined between the last closed order and Timecurrent().

I'm always getting the last value of the buffer instead the minimum value of the array. I've tried to change every parameter but so far no luck.

Please could you help me out?

int ArrayCalcMin()
   {   
      datetime Timeorder=LastOrderTime();
      datetime Timeahora=TimeCurrent();
      datetime Times=Timeahora-Timeorder;
      
                
      int m = TimeMinute(Times);
      int h = TimeHour(Times);
      int d = TimeDay(Times);
      int bars = (((d-1)*24*60)+(h*60)+m)/240;
      int malookback=bars;
     
        
            
      double madaily[30000];
      double dllv;
      
      for(int i = 1; i < malookback; i++)
                  
      {
      
      madaily[i] = iCustom(NULL,0,"Scolor8",0,i);
         
      
      dllv = iCustom(NULL,0,"Scolor8",0,
             ArrayMinimum(madaily,WHOLE_ARRAY,0));
     }
     Print("Minimum value : ",dllv);
      
      if (dllv < 50){return(1);malookback=0; }
      else {return(0);malookback=0;}
      
   }

Thank you in advance. KR Andrés

 
Pinto André:

I'm trying to find the lowest value in an array that is determined between the last closed order and Timecurrent().

I'm always getting the last value of the buffer instead the minimum value of the array. I've tried to change every parameter but so far no luck.

Please could you help me out?

  1. It seems to be MQL4 code even though you post in the general section instead of the MT4 section! Please confirm if it is MQL4 or MQL5 because it can be very different, both in regards to iCustom and to ArrayMinimum. I will assume MQL4 for now!
  2. Don't just use the retrun value of ArrayMinimum directly. First retrieve it and check it because it may be returning an invalid value.
  3. You should check for the Minimum outside the loop. There is no use looking for a minimum during every step while you building the array. It is a waste of CPU resources.
  4. You start filling the "madaily" at position "1", but are searching for a minimum starting at position "0" which will initially be set at "0.0", and thus probably always the minimum value of all the rest.
  5. You execute "return" before setting the "malookback" to 0, which will never be executed.
  6. There are probably a few more problems, but I will leave it here for now until you resolve these issues first.
 
Fernando Carreiro:
  1. It seems to be MQL4 code even though you post in the general section instead of the MT4 section! Please confirm if it is MQL4 or MQL5 because it can be very different, both in regards to iCustom and to ArrayMinimum. I will assume MQL4 for now!
  2. Don't just use the retrun value of ArrayMinimum directly. First retrieve it and check it because it may be returning an invalid value.
  3. You should check for the Minimum outside the loop. There is no use looking for a minimum during every step while you building the array. It is a waste of CPU resources.
  4. You start filling the "madaily" at position "1", but are searching for a minimum starting at position "0" which will initially be set at "0.0", and thus probably always the minimum value of all the rest.
  5. You execute "return" before setting the "malookback" to 0, which will never be executed.
  6. There are probably a few more problems, but I will leave it here for now until you resolve these issues first.

Dear Fernando,

Thank you for you comments.

1. Yes, it's is MQL4, sorry about that.

2. I'm checking the value, printing it in this line, Print("Minimum value : ",dllv); and i was trying to see if the logic behind, and it it seems to be working, because it's returning 0 or 1 depending on the value in dllv.

it's there a way to check the values in the array? 

3. Yes initially i put it outside, but since it was returning the wrong value i tryied to put it inside. 

4. Done. 

5. I reversed the position, it's OK this way?


 int ArrayCalcMin()
   {   
      datetime Timeorder=LastOrderTime();
      datetime Timeahora=TimeCurrent();
      datetime Times=Timeahora-Timeorder;
      
                
      int m = TimeMinute(Times);
      int h = TimeHour(Times);
      int d = TimeDay(Times);
      int bars = (((d-1)*24*60)+(h*60)+m)/240;
      int malookback=bars;
     
        
            
      double madaily[30000];
      double dllv;
      
      for(int i = 0; i < malookback; i++)
                  
      {
      
      madaily[i] = iCustom(NULL,0,"Scolor8",0,i);
          
      }
     
     dllv = iCustom(NULL,0,"Scolor8",0,
             ArrayMinimum(madaily,WHOLE_ARRAY,0));
             
     Print("Minimum value : ",dllv);
      
      if (dllv < 50){malookback=0;return(1);}
      else {malookback=0;return(0);}

Thank youu again.

KR

 
Pinto André :

***

1. Yes, it's is MQL4, sorry about that.

***

There is a dedicated section for all questions about the old terminal: MQL4 and MetaTrader 4. 

I will postpone your topic.

 
Pinto André:

1. Yes, it's is MQL4, sorry about that.

2. I'm checking the value, printing it in this line, Print("Minimum value : ",dllv); and i was trying to see if the logic behind, and it it seems to be working, because it's returning 0 or 1 depending on the value in dllv.

it's there a way to check the values in the array? 

3. Yes initially i put it outside, but since it was returning the wrong value i tryied to put it inside. 

4. Done. 

5. I reversed the position, it's OK this way?

2. No, you are not checking the ArrayMinimum value. You are checking the iCustom value for the ArrayMinimum.

int minPos = ArrayMinimum(madaily,WHOLE_ARRAY,0); // Check the "minPos" to make sure it is in range as it could be "-1" if it fails
if( minPos >= 0 )
   dllv = iCustom(NULL,0,"Scolor8",0, minPos);
else
   Print("Something is wrong!");

5. You are just complicating matters. Simplify!

malookback=0;
if (dllv < 50)
   return(1);
else
   return(0);

Or just use "short-hand" with a Ternary Operator!

   malookback=0;
   return( dllv < 50 ? 1 : 0 );
EDIT: Or simplify even more! ArrayCalcMin will return a bool true or false instead of an int number 1 or 0!
bool ArrayCalcMin()
{
   // ...

   malookback=0;
   return( dllv < 50 );
}
Documentation on MQL5: Language Basics / Operators / Ternary Operator ?:
Documentation on MQL5: Language Basics / Operators / Ternary Operator ?:
  • www.mql5.com
Ternary Operator ?: - Operators - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Fernando Carreiro:

2. No, you are not checking the ArrayMinimum value. You are checking the iCustom value for the ArrayMinimum.

5. You are just complicating matters. Simplify!

Or just use "short-hand" with a Ternary Operator!

EDIT: Or simplify even more! ArrayCalcMin will return a bool true or false instead of an int number 1 or 0!

Hi Fernando, 

Thank you for the tips, very helpful indeed.

I've checked the value that is calculated in minPos and it strange since it's given me the last bar and it should be the fourth. What is the purpose of calculating the minPos? As i understand it should give the position in the array where is the lowest value, right?

There is something that i don't understand, why it's not given me the result that i want:

So the array madaily should capture all the values of the indicator buffer comprehended between the last bar where there was closed the last order and now (defined in malookback -> i reviewed and the value is OK, is given me the exact 9 bars).

The minPos should give the position in the array where it's registered the lowest value and then the dllv should give me value of the buffer in the position given by minPos.

Perhaps, since the position is wrong, the problem should be in how i'm calculating the array? could it be in for(int i = 0; i < malookback; i++)?

Thank you again.

Obrigado is case you are Portuguese!

KR.

Files:
Capturar.PNG  6 kb
 

Pinto AndréThank you for the tips, very helpful indeed.

I've checked the value that is calculated in minPos and it strange since it's given me the last bar and it should be the fourth. What is the purpose of calculating the minPos? As i understand it should give the position in the array where is the lowest value, right?

There is something that i don't understand, why it's not given me the result that i want:

So the array madaily should capture all the values of the indicator buffer comprehended between the last bar where there was closed the last order and now (defined in malookback -> i reviewed and the value is OK, is given me the exact 9 bars).

The minPos should give the position in the array where it's registered the lowest value and then the dllv should give me value of the buffer in the position given by minPos.

Perhaps, since the position is wrong, the problem should be in how i'm calculating the array? could it be in for(int i = 0; i < malookback; i++)?

Thank you again.

Obrigado is case you are Portuguese!

De nada! Yes, I'm Portuguese!

Print out the "madaily" array values, to see if it is as you expect.

The reason you need to check the "minPos" first, is in the case that the ArrayMinimum function fails, and yes it is an index into the array.

Also, print out the value of "minPos", so that we can see what it is actually returning.

Also, since you have already filled the "madaily" array, I would suggest getting the value directly from that array instead of calling the iCustom again.


   for(int i = 0; i < malookback; i++)
      madaily[i] = iCustom(NULL,0,"Scolor8",0,i);

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

   if( minPos >= 0 )
   {
      dllv = madaily[minPos];  // 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!");

   // malookback=0; You don't need this because it is a local variable that will be discarded as soon as you return 
   
   return( dllv < 50 );
}
 
Also, it might simplify things if your show the log text that gets printed, for the values of "minPos" and "dllv", and print out the contents of the "madaily" array as well.
 
Fernando Carreiro:
Also, it might simplify things if your show the log text that gets printed, for the values of "minPos" and "dllv", and print out the contents of the "madaily" array as well.

Good night Fernando,

Thank you for your help.

Replacing the code that you send me and print the values that you told me, i get the following results in the moment before realize the next buy.

0 21:50:18.898 2018.01.04 16:00:00  FalconTemplate2-5 EURUSD,H4: Scolor value : 63.5629

0 21:50:18.898 2018.01.04 16:00:00  FalconTemplate2-5 EURUSD,H4: Last closed order: 2  1514952000  0

0 21:50:18.898 2018.01.04 16:00:00  FalconTemplate2-5 EURUSD,H4:  array = 0

0 21:50:18.898 2018.01.04 16:00:00  FalconTemplate2-5 EURUSD,H4:  minPos = 9

0 21:50:18.898 2018.01.04 16:00:00  FalconTemplate2-5 EURUSD,H4:  dllv = 0

0 21:50:18.898 2018.01.04 16:00:00  FalconTemplate2-5 EURUSD,H4: value: ValorMIN 1

0 21:50:18.898 2018.01.04 16:00:00  FalconTemplate2-5 EURUSD,H4: EA Journaling: Trying to place a market order...

2 21:50:18.898 2018.01.04 16:00:00  FalconTemplate2-5 EURUSD,H4: open #3 buy 1.00 EURUSD at 1.20826 ok

Please note that the value for dllv is zero and before i was obtaining the the same value as the last value. equally printing the array i get the same result. I understand that the error might be here.

I've included the buffer value that i want to calculate (green) to show you that the icustom is working properly.

Thanks.

Andrés

 
Pinto André: Obrigado de novo pela ajuda. Em português fica mais fácil.

Unfortunately, you are are the English forum so you can't respond in Portuguese. That would be unfair to everyone else following the thread.

For the discussion to be in Portuguese, you would have to have started in the Portuguese forum of MQL5's website: https://www.mql5.com/pt/forum

So, if you wish to continue the discussion here, it will have to be in English.

Fórum MQL5
Fórum MQL5
  • www.mql5.com
MQL5: Fórum sobre sistemas de negociação automatizados e testes de estratégia
 

Pinto André:

0 21:50:18.898 2018.01.04 16:00:00  FalconTemplate2-5 EURUSD,H4:  minPos = 9

0 21:50:18.898 2018.01.04 16:00:00  FalconTemplate2-5 EURUSD,H4:  dllv = 0

Since "minPos" is returning a value of 9, then it definitely is not returning the index of the last bar (or first array element) and therefore seems to be working correctly.

The "dllv" value however, is returning a value 0, so you will have print out the "madaily" contents, at least the first 30 elements or so, to see what the value at 9 and the surround values.

The best is to provide the full source of both Indicator and a small test Script, so that we can replicate it on our end, since we are currently at a point that I am unable to offer further advice without knowing more of the details on your side.