Get maxima and minima (condition)

 

I am not able to get the correct values ​​of the maximum and minimum considering the condition: if(Candle[i].open<Candle[i].close && Candle[i].open>Candle[i+1].close). Can anyone help me with the script, thanks in advance.

//+------------------------------------------------------------------+
//|                                                       MaxMin.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Math/Stat/Math.mqh> 

MqlRates Candle[];
double MaxMin[];

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
          CopyRates(_Symbol,PERIOD_M1,0,10,Candle);
          ArraySetAsSeries(Candle,true);       
                  
            ArrayResize(MaxMin,ArraySize(Candle)); 
            for(int i=0;i<ArraySize(Candle);i++) 
              {
                 if(Candle[i].open<Candle[i].close && Candle[i].open>Candle[i+1].close)
                   {
                      MaxMin[i]=Candle[i].close;
                      //Print(Candle[i].close);
                   }   
             }          

            double min = MathMin(MaxMin);
            Print("Valor Miima: ",min);
             
            double max = MathMax(MaxMin);
            Print("Valor máxima: ", max);
}
 

You should read the doc, about the functions! Place the cursor on the function and press F1!

Here is the list of all function: https://www.mql5.com/de/docs/function_indices

MathMax "Returns the maximal value of the two numeric values" - but your MaxMin[] is an array! The compiler should have told you!

But in that list you can find Array ArrayMaximum() - read the example it will save you a lot of time!!
Dokumentation zu MQL5: MQL5 Funktionenliste
Dokumentation zu MQL5: MQL5 Funktionenliste
  • www.mql5.com
MQL5 Funktionenliste - Nachschlagewerk MQL5 - Nachschlagewerk über die Sprache des algothitmischen/automatischen Handels für MetaTrader 5
 
Carl Schreiber #:

You should read the doc, about the functions! Place the cursor on the function and press F1!

Here is the list of all function: https://www.mql5.com/de/docs/function_indices

MathMax "Returns the maximal value of the two numeric values" - but your MaxMin[] is an array! The compiler should have told you!

But in that list you can find Array ArrayMaximum() - read the example it will save you a lot of time!!

I made some changes and unfortunately can't get the correct values. I'm new to mql5 programming and I'm having a lot of difficulty with this script and I appreciate if anyone can help me. My need is: 1) Filter the bars "if(Candle[i].open<Candle[i].close && Candle[i].open>Candle[i+1].close)" 2) Find the high and low of these filtered bars. Thank you very much!

//+------------------------------------------------------------------+
//|                                                       MaxMin.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Math/Stat/Math.mqh> 

MqlRates Candle[];
double Maxima[],Minima[];

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
          CopyRates(_Symbol,PERIOD_M1,0,10,Candle);
          ArraySetAsSeries(Candle,true);                    
                           
             ArrayResize(Maxima,ArraySize(Candle));  
             ArrayResize(Minima,ArraySize(Candle));
             for(int i=0;i<ArraySize(Candle);i++) 
              {
                 if(Candle[i].open<Candle[i].close && Candle[i].open>Candle[i+1].close)
                   
                      Maxima[i]=Candle[i].high;
                      Minima[1]=Candle[1].low;
                      
             }                 
                  
           double max = Maxima[ArrayMaximum(Maxima)]; 
           double min = Minima[ArrayMinimum(Minima)]; 
           Print(max);
           Print(min);
}
 
TdNutricula #:

I made some changes and unfortunately can't get the correct values. I'm new to mql5 programming and I'm having a lot of difficulty with this script and I appreciate if anyone can help me. My need is: 1) Filter the bars "if(Candle[i].open<Candle[i].close && Candle[i].open>Candle[i+1].close)" 2) Find the high and low of these filtered bars. Thank you very much!

First thing I found is your [i+1] loop gives 'array out of range' errors so I adjusted the loop to avoid this.

After that I added ArrayPrints for your Minima and Maxima arrrays - it looks like you are getting the min/max values but there are many 0 values there too which could be confusing the matter.

Have a look at the data collected in Candle and see if that's what you want - maybe you need to print out Candle to get a clear appreciation of what's going on

#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Math/Stat/Math.mqh> 

MqlRates Candle[];
double Maxima[],Minima[];

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
          CopyRates(_Symbol,PERIOD_M1,0,10,Candle);
          ArraySetAsSeries(Candle,true);                    
                           
             ArrayResize(Maxima,ArraySize(Candle));  
             ArrayResize(Minima,ArraySize(Candle));
             for(int i=0;i<ArraySize(Candle)-1;i++) 
              {
                 if(Candle[i].open<Candle[i].close && Candle[i].open>Candle[i+1].close)
                   
                      Maxima[i]=Candle[i].high;
                      Minima[1]=Candle[1].low;
                      
             }                 
                  
           ArrayPrint(Maxima);
           double max = Maxima[ArrayMaximum(Maxima)]; 

           ArrayPrint(Minima);
           double min = Minima[ArrayMinimum(Minima)]; 
           Print(max);
           Print(min);
}

             


 
  1. R4tna C #: First thing
                     if(Candle[i].open<Candle[i].close && Candle[i].open>Candle[i+1].close)
                           
                          Maxima[i]=Candle[i].high;
                          Minima[1]=Candle[1].low;
                             

    Your filter only applies to the max. The min is set always to the last value of Candle. Where are your brackets?

  2. TdNutricula #: Find the high and low of these filtered bars.

    Unless you are going to do something else with your arrays, just drop them and simplify.

    double min=DBL_MAX;
    double max=DBL_MIN;
    
                 for(int i=0;i<ArraySize(Candle)-1;i++) 
                  {
                     if(Candle[i].open<Candle[i].close && Candle[i].open>Candle[i+1].close)
                       {
                          if(max < Candle[i].high) max = Candle[i].high;
                          if(min > Candle[1].low)  min = Candle[1].low;
                       }
                 }                 
                      
               Print(max);
               Print(min);
 

The code passed by William Roeder is unspeakable. It's exactly what I need! Just to record, I made two adjustments to the code being; 1) Find the high and low from the previous candlestick history.: "for ( int i= "0".... for for (int i= "1"..... 2) I corrected my logic so that "min" is equal to "max" (I changed "1" to "i"). I learned a lot and I sincerely appreciate the time that the two friends dedicated to helping me. There are things in life that are priceless! Attached final code!

#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

MqlRates Candle[];

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{           
        CopyRates(_Symbol,PERIOD_M1,0,10,Candle);
        ArraySetAsSeries(Candle,true);  
            
            double min=DBL_MAX;
            double max=DBL_MIN;

             for(int i=1;i<ArraySize(Candle)-1;i++) 
              {
                 if(Candle[i].open<Candle[i].close && Candle[i].open>Candle[i+1].close)
                   {
                      if(max < Candle[i].high) max = Candle[i].high;
                      if(min > Candle[i].low)  min = Candle[i].low;
                   }
             }                 
                  
           Print(max);
           Print(min);                      
}      
Reason: