Highest High & Lowest Low OK, but struggling to get 2nd highest/ lowest candle?

 

Good day All, I have the basic code written below to obtain the Highs, Lows, Closes etc in arrays below on a indicator. Ive done a lot of reading to try and find a way to also obtain the 2nd highest high & lowest low values.  I was  trying to use ArraySort (highs, WHOLE_ARRAY, 1, MODE_DESCEND); but nothing I tried was working, and  after sort use the sort of candle highs ,candle 1 would be the 2nd highest high? ,I'm not sure I'm even on the right track. Is there a method to get the 2nd highest high/ Lowest low values from the existing highs / lows arrays below?

Regards, Dash




int MAX_CANDLES = 100;
MqlRates candles [];  // values of the prices
double closes    [];  // values of the candle closes
double highs     [];  // values of the candle highs
double lows      [];  // values of the candle lows

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
 
int OnInit()

{
ArraySetAsSeries  (candles, true);   // sort array downwards from the current candle
ArraySetAsSeries  (closes, true);
ArraySetAsSeries  (highs, true);
ArraySetAsSeries  (lows, true);

return(INIT_SUCCEEDED);
}
  void OnTick (void) {
 
 
  CopyRates(_Symbol,_Period,0,MAX_CANDLES, candles);
   
 
  CopyClose(_Symbol,_Period,0,MAX_CANDLES, closes);
 

  CopyHigh (_Symbol,_Period,0,MAX_CANDLES,highs);
 
  
  CopyLow (_Symbol,_Period,0,MAX_CANDLES,lows);
 
 
  int index_highestClose = ArrayMaximum (closes,0,MAX_CANDLES);  //  
  int index_lowestClose  = ArrayMinimum (closes,0,MAX_CANDLES);  //
  int index_highestHigh  = ArrayMaximum (highs, 0,MAX_CANDLES);  //     
  int index_lowestLow    = ArrayMinimum (lows,  0,MAX_CANDLES);  //

 
Dashoshea:

Good day All, I have the basic code written below to obtain the Highs, Lows, Closes etc in arrays below on a indicator. Ive done a lot of reading to try and find a way to also obtain the 2nd highest high & lowest low values.  I was  trying to use ArraySort (highs, WHOLE_ARRAY, 1, MODE_DESCEND); but nothing I tried was working, and  after sort use the sort of candle highs ,candle 1 would be the 2nd highest high? ,I'm not sure I'm even on the right track. Is there a method to get the 2nd highest high/ Lowest low values from the existing highs / lows arrays below?

Regards, Dash




int MAX_CANDLES = 100;
MqlRates candles [];  // values of the prices
double closes    [];  // values of the candle closes
double highs     [];  // values of the candle highs
double lows      [];  // values of the candle lows

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
 
int OnInit()

{
ArraySetAsSeries  (candles, true);   // sort array downwards from the current candle
ArraySetAsSeries  (closes, true);
ArraySetAsSeries  (highs, true);
ArraySetAsSeries  (lows, true);

return(INIT_SUCCEEDED);
}
  void OnTick (void) {
 
 
  CopyRates(_Symbol,_Period,0,MAX_CANDLES, candles);
   
 
  CopyClose(_Symbol,_Period,0,MAX_CANDLES, closes);
 

  CopyHigh (_Symbol,_Period,0,MAX_CANDLES,highs);
 
  
  CopyLow (_Symbol,_Period,0,MAX_CANDLES,lows);
 
 
  int index_highestClose = ArrayMaximum (closes,0,MAX_CANDLES);  //  
  int index_lowestClose  = ArrayMinimum (closes,0,MAX_CANDLES);  //
  int index_highestHigh  = ArrayMaximum (highs, 0,MAX_CANDLES);  //     
  int index_lowestLow    = ArrayMinimum (lows,  0,MAX_CANDLES);  //

You may have 2 options to use

1. ZigZag 

2. Fractal

 
Arpit T #:

You may have 2 options to use

1. ZigZag 

2. Fractal

Thanks for the reply Arpit. I haven't looked at either of those indicators coding, but I will  look into it. I was hoping as there was already an array of stored highs, there may be a simple way to get it directly from that.
 
  1. Dashoshea: I was  trying to use ArraySort (highs, WHOLE_ARRAY, 1, MODE_DESCEND); but nothing I tried was working,

    Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2. “Doesn't work” is meaningless — just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires — meaningless.
         How To Ask Questions The Smart Way. (2004)
              When asking about code
              Be precise and informative about your problem

  3. Of course, you can use sort, then the highest is last element and the second is in the next to last.

    Not tested, not compiled, just typed.
    ArraySort(highs)
    double   HH  = highs[MAX_CANDLES-1),
             HH2 = highs[MAX_CANDLES-2);
    Not tested, not compiled, just typed.

    You can't sort MqlRates.

  4. Dashoshea:  int index_highestClose = ArrayMaximum (closes,0,MAX_CANDLES);  //   
    ArrayMaximum as that gives you one index in the range. You have to remove the highest to find the next highest.
    Not tested, not compiled, just typed.
    // Find highest.
    int      iHH = ArrayMaximum(highs, 0,MAX_CANDLES); 
    double    HH = highs[iHH];
    
    // Find second highest.
    highs[iHH] = highs[MAX_CANDLES-1];
    int      iHH2 = ArrayMaximum (highs, 0,MAX_CANDLES-1); 
    double    HH2 = highs[iHH];
    
    Not tested, not compiled, just typed.
  5. Or just search for them yourself.

    Not tested, not compiled, just typed.
    // int index_highestHigh  = ArrayMaximum (highs, 0,MAX_CANDLES);  // 
    double highest[]={DBL_MIN, DBL_MIN};
    for(int i=0; i < MAX_CANDLES) if(highs[i] >= highest[0]){
       highest[1] = highest[0]; highest[0] = highs[i]
    }
    Not tested, not compiled, just typed.
    Is that so hard you couldn't do it?
 
William Roeder #:
  1. You can't use ArrayMaximum as that gives you one index in the range.
  2. You can use sort, then the highest is last element and the second is in the next to last.

    “Doesn't work” is meaningless — just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires — meaningless.
         How To Ask Questions The Smart Way. (2004)
              When asking about code
              Be precise and informative about your problem

  3. Or just search for them yourself.
    Not tested, not compiled, just typed. Not tested, not compiled, just typed.
    Is that so hard you couldn't do it?

Thanks William for the correction and explanation on how to achieve the second highest/lowest. I'm only starting learning mql5 basics  recently.   This is the first project I'm working on that isn't a direct copy for copy mql5 learning video. So for me learning anything completely new is hard. I searched and tested the shit out of this, but realized I must be offtrack and wasn't getting anywhere without asking advice someone. So I'll follow your suggested method above, Cheers.

 

William Roeder #:

// int index_highestHigh  = ArrayMaximum (highs, 0,MAX_CANDLES);  // 
double highest[]={DBL_MIN, DBL_MIN};
for(int i=0; i < MAX_CANDLES) if(highs[i] >= highest[0]){
   highest[1] = highest[0]; highest[0] = highs[i]
}

What if he finds the highest overall first tho

 

@Dashoshea

You could try this :

Get the iHighest within the range you are interested in.

Then perform iHighest to its left (within your original range to the left boundary)

Then perform iHighest to its right (within your original range to the right boundary)

Compare the 2 side highests , pick the highest.

Rinse and repeat for the lovest

But that may fail too , the best solution is what @Arpit T suggested unless you enforce a minimum distance from the first highest/lowest on your subsequent seeks

 
Lorentzos Roussos #:

@Dashoshea

You could try this :

Get the iHighest within the range you are interested in.

Then perform iHighest to its left (within your original range to the left boundary)

Then perform iHighest to its right (within your original range to the right boundary)

Compare the 2 side highests , pick the highest.

Rinse and repeat for the lovest

But that may fail too , the best solution is what @Arpit T suggested unless you enforce a minimum distance from the first highest/lowest on your subsequent seeks

Hi Lorentzos,  That idea sounds like it could work too. I'm working away from home right now, but once I'm back home I'll give both these ideas a try. Thanks for your input, much appreciated !
 
Lorentzos Roussos #: What if he finds the highest overall first tho
highs[iHH] = highs[MAX_CANDLES-1];
int      iHH2 = ArrayMaximum (highs, 0,MAX_CANDLES-1); 

You missed the fact that the highest has been removed.

 
William Roeder #:

You missed the fact that the highest has been removed.

I was commenting on 5 , you have 2 slots in the highest array and you are blocking further discovery when you find the first high if its the highest 😇 .

However in pivot terms none of these work because the second high might be a bar leading up or away from the first high.

Trust me i'm an expert in highs and lows , lows expecially  😊 

Reason: