mql5 help: for-loop for finding swing highs

 

Hi there! 

I was playing around programming some EAs that work with swing highs/lows. The way i detect swing highs/lows is (obviously) through the iHighest function. Now: I want to implement a user input lookback period, for example 20 bars, so that the EA finds the highest from the last 20 bars. Also works without problem. Now, the problem that comes up though, if the current bar is the highest over the lookback period, i dont want it to be detected. The starting shift of the iHighest function (the last parameter of the function) also doesnt work, since, if you get more than 5 bullish bars in a row the highest detected bar would be lower than the current market price. What I instead would like to do, is to increase the lookback period in that case in 5-bar-steps until there is a new (higher) swing high detected. and since im not a professional programmer, im really struggeling with the loop structure necessary for that, so i figured someone in here might be able to help. This is what i got so far, however i get an "expression has no effect" error in the editor: (also, this is only the findHigh function ripped out of the main EA for testing/diagnostic purposes)


input int lookbackPeriod = 20;

void OnTick(){

   findHigh();

}

double high = 0;

int highestBar = 0;

int lookbackTotal = lookbackPeriod;

double findHigh(){

   if(iHighest(_Symbol,PERIOD_CURRENT,MODE_HIGH,lookbackPeriod,0) >= 5){

      highestBar = iHighest(_Symbol,PERIOD_CURRENT,MODE_HIGH,lookbackPeriod,0);

      high = iHigh(_Symbol,PERIOD_CURRENT,highestBar);

      Print(" ",highestBar);

      return high;

   }

   else 

      while(iHighest(_Symbol,PERIOD_CURRENT,MODE_HIGH,lookbackTotal,0) < 5){

         highestBar = iHighest(_Symbol,PERIOD_CURRENT,MODE_HIGH,lookbackTotal,0);

         high = iHigh(_Symbol,PERIOD_CURRENT,highestBar);

         lookbackTotal + 1;

      }

      Print(high," ",highestBar);

      return high;

}


First part of the findHigh function works without any issues, it's just the for-loop that i cant get to work, trying out different stuff for 2 days already now and nothing works. if anybody has some better ideas on how to code what i want to do, please do tell, im just a beginner, every tip is appreciated!

 
EntropyM:

Hi there! 

I was playing around programming some EAs that work with swing highs/lows. The way i detect swing highs/lows is (obviously) through the iHighest function. Now: I want to implement a user input lookback period, for example 20 bars, so that the EA finds the highest from the last 20 bars. Also works without problem. Now, the problem that comes up though, if the current bar is the highest over the lookback period, i dont want it to be detected. The starting shift of the iHighest function (the last parameter of the function) also doesnt work, since, if you get more than 5 bullish bars in a row the highest detected bar would be lower than the current market price. What I instead would like to do, is to increase the lookback period in that case in 5-bar-steps until there is a new (higher) swing high detected. and since im not a professional programmer, im really struggeling with the loop structure necessary for that, so i figured someone in here might be able to help. This is what i got so far, however i get an "expression has no effect" error in the editor: (also, this is only the findHigh function ripped out of the main EA for testing/diagnostic purposes)



First part of the findHigh function works without any issues, it's just the for-loop that i cant get to work, trying out different stuff for 2 days already now and nothing works. if anybody has some better ideas on how to code what i want to do, please do tell, im just a beginner, every tip is appreciated!

If I got you right, you need a program that can detect the highest candle within the last 20 candles printed on the chart. Also if the highest candle is the most recent then you wouldn't want any output. Is that correct?

 
Chioma Obunadike #:

If I got you right, you need a program that can detect the highest candle within the last 20 candles printed on the chart. Also if the highest candle is the most recent then you wouldn't want any output. Is that correct?

more or less, I want a function that detects the highest candle within the last 20 candles, if the highest candle is a very recent one, i want the lookback period increased gradually until a higher swing high is found.
 
EntropyM #:
more or less, I want a function that detects the highest candle within the last 20 candles, if the highest candle is a very recent one, i want the lookback period increased gradually until a higher swing high is found.

I believe the function you are looking for is a "while" loop.  A "While" Loop is used to repeat a specific block of code an unknown number of times, until a condition is met. 

 
Chioma Obunadike #:

I believe the function you are looking for is a "while" loop.  A "While" Loop is used to repeat a specific block of code an unknown number of times, until a condition is met. 

if you look at the initial code i posted, i tried that. cant get the while loop to work though, im not sure why. error message is "expression has no effect" and i dont know why.
 

okay i got the while loop working now, didnt take lookbackTotal + 1 as a mathematical operation. thought it was the same as ++ but doesnt work like that as it seems. while loop works, but still it doesnt increase the lookback period, instead it still outputs the values from the recent candles < 5 for some reason...


edit: i let it print "lookbackTotal" as well, it DOES increase the lookback period, but still outputs bars below 5 though...

edit2: got it, found a solution, everything works as intended now.
 
  1. EntropyM #: I want a function that detects the highest candle within the last 20 candles, if the highest candle is a very recent one, i want the lookback period increased gradually until a higher swing high is found.
    double findHigh(int window=20, int minimumBar=5){
       int      iExtreme = 0;
       double   extreme  = iHigh(_Symbol,PERIOD_CURRENT, iExtreme);
    
       int   iBeg  = iExtreme;           // Go up hill until we find the highest high
       while(iExtreme < size){           // within the window.
          int   iEnd  = iBeg + window;
          for(; iBeg < iEnd; ++iBeg){
             double value = iHigh(_Symbol,PERIOD_CURRENT, iBeg);
             if(extreme < value){           // New high, adjust the window
                iExtreme = iBeg;  extreme = value;  iEnd = iExtreme + window;
          }  }
       }                                 // Find the next mountain and keep climbing.
       return extreme;
    }
  2. EntropyM #: edit2: got it, found a solution, everything works as intended now.
    You were using iHighest. Your code will likely find a new higher bar. It will not find the highest bar.
 
int FindmyHigh((ENUM_TIMEFRAMES) TF, int Per)
{
        int ret = -1;
        int size = iBars(Symbol(), TF);

        for (int i=0; i<size-Per; i++)
                {
                ret = iHighest(Symbol(), TF, MODE_HIGH, Per, i);
                if (ret!=0 && iHigh(Symbol(), TF, ret) > iHigh(Symbol(), TF, 0) && iLowest(Symbol(), TF, MODE_HIGH, ret+1, i) < ret) break;
                }

        return(ret);
}

OR

int FindmyHigh((ENUM_TIMEFRAMES) TF, int Per)
{
        int ret = -1;
        int size = iBars(Symbol(), TF);

        for (int i=0; i<size-Per; i++)
                {
                ret = iHighest(Symbol(), TF, MODE_HIGH, Per, i);
                if (ret!=0 && iLowest(Symbol(), TF, MODE_HIGH, ret+1, i) < ret) break;
                }

        return(ret);
}


Hi there, I think there is a conflict in your problem description but thats OK, I sent two different solution as I understand situation in two different way.