I can't find the range with loop

 

Help. I'm stuck. I'm trying to find the range in a chart using the bool function. Unfortunately, I always get True. What am I doing wrong? Please help. This is my beginning with mql5.

input int   InpRangeBars = 4;             

double      RangeHi = 0;
double      RangeLo = 0;


bool FindReange (){


 double      rangeHi =   iHigh(_Symbol,PERIOD_CURRENT,iHighest(_Symbol,PERIOD_CURRENT,MODE_HIGH,InpRangeBars,4));
 double      rangeLo =   iLow(_Symbol,PERIOD_CURRENT, iLowest(_Symbol,PERIOD_CURRENT,MODE_LOW, InpRangeBars, 4));



       for(int i=4; i<InpRangeBars; i++){
         if ((iHigh(_Symbol,PERIOD_CURRENT,i) <= RangeHi) && ( iLow(_Symbol,PERIOD_CURRENT,i) >= RangeLo) )
         return (true);
         }
         
         RangeHi = rangeHi;
         RangeLo = rangeLo;
         return (false);
    }    
}
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
Hello. 

Are you looking to have the highest, lowest on 4 bars
 
Gerard Willia G J B M Dinh Sy #:
Hello. 

Are you looking to have the highest, lowest on 4 bars

 Hello,

It's easier to check if it works at 4 bars than at 15 or 20.

And I want to check whether I'm in range or not.

Regards!

 
Gerard Willia G J B M Dinh Sy #:
Look this way https://www.mql5.com/en/code/46989

Thank you. I have already tried DonChian channels and unfortunately they are not suitable for my needs. If I were to use a ready-made indicator, a better option would be Bollinger Bands Squeeze.

However, this won't help me understand where the error is in my idea of ​​​​finding the range.

 thank you again!

 
jakub idzikowski:

Help. I'm stuck. I'm trying to find the range in a chart using the bool function. Unfortunately, I always get True. What am I doing wrong? Please help. This is my beginning with mql5.

maybe so?

#property script_show_inputs

input int Bars = 200;
//+------------------------------------------------------------------+
void OnStart() {
   double price = 1.10359;
   bool isRange = IsRange(price, Bars);
   string result = isRange?"inside":"outside";
   Print("Price "+string(price) + " is "+result + " the range of the first "+string(Bars) +" bars");
}
//+------------------------------------------------------------------+
bool IsRange (double value, int bars, int start_bar=0) {
   double  max = iHigh(_Symbol,PERIOD_CURRENT,iHighest(_Symbol,PERIOD_CURRENT,MODE_HIGH,bars,start_bar));
   double  min = iLow(_Symbol,PERIOD_CURRENT, iLowest(_Symbol,PERIOD_CURRENT,MODE_LOW, bars,start_bar));
   return value<=max && value>=min;
}


2023.12.31 01:55:39.969 FindRange (EURUSD,M1)   Price 1.1 is outside the range of the first 200 bars
2023.12.31 01:56:24.212 FindRange (EURUSD,M1)   Price 1.10359 is inside the range of the first 200 bars
 
Nikolai Semko #:

maybe so?


Thank you Nikolai. I tried your solution and unfortunately, when I check whether the current price is within or outside the range, the result is true.

I would like to find the range, for example 20 bar, but iHigh/iLow updates the values ​​and each new result is within the range. I do not understand this.

 
jakub idzikowski #:

Thank you Nikolai. I tried your solution and unfortunately, when I check whether the current price is within or outside the range, the result is true.

I would like to find the range, for example 20 bar, but iHigh/iLow updates the values ​​and each new result is within the range. I do not understand this.

I can't understand the task.  You need to clearly formulate the task.  Write this problem in two languages: your native and English, please.
 
Nikolai Semko #:
I can't understand the task.  You need to clearly formulate the task.  Write this problem in two languages: your native and English, please.

Chcę znaleźć na wykresie zakres  przykładowo 20 świec w którym cena porusza się pomiędzy minimum i maksimum. Moim pomysłem było znalezienie minimum i maksimum za pomocą iHigh and iLow z ostatich 20 świec i ich aktualizacja jeżeli warunki nie będą spełnione, to znaczy jeżeli cena wybije minimum i maksimum wcześniej niż przed 20 świecą. Niestety funkcja którą napisałem zawsze ma wartość true.

Cel jaki chcę osiągnąć poprzez tą funkcję to znalezienie obszarów kiedy rynek porusza się w zasięgu i wyłączenie tych obszarów z handlu.


I want to find a range, for example, 20 bars on the chart in which the price moves between the minimum and maximum. My idea was to find the minimum and maximum using iHigh and iLow from the last 20 bars and update them if the conditions are not met, i.e. if the price hits the minimum and maximum earlier than before the 20th candle. Unfortunately, the function I wrote always has the value true.


The goal I want to achieve with this function is to find areas when the market is moving within range and exclude these areas from trading.

my Mql and  my English are not the best so thank you for your patience.

 
jakub idzikowski #:

Chcę znaleźć na wykresie zakres  przykładowo 20 świec w którym cena porusza się pomiędzy minimum i maksimum. Moim pomysłem było znalezienie minimum i maksimum za pomocą iHigh and iLow z ostatich 20 świec i ich aktualizacja jeżeli warunki nie będą spełnione, to znaczy jeżeli cena wybije minimum i maksimum wcześniej niż przed 20 świecą. Niestety funkcja którą napisałem zawsze ma wartość true.

Cel jaki chcę osiągnąć poprzez tą funkcję to znalezienie obszarów kiedy rynek porusza się w zasięgu i wyłączenie tych obszarów z handlu.


I want to find a range, for example, 20 bars on the chart in which the price moves between the minimum and maximum. My idea was to find the minimum and maximum using iHigh and iLow from the last 20 bars and update them if the conditions are not met, i.e. if the price hits the minimum and maximum earlier than before the 20th candle. Unfortunately, the function I wrote always has the value true.


The goal I want to achieve with this function is to find areas when the market is moving within range and exclude these areas from trading.

my Mql and  my English are not the best so thank you for your patience.

It is very difficult to understand from what is written above what you want, since you do not indicate which 20 bars.  The last ones?  What is the price for a range check?  The last one?  
If yes, then it is clear that the last price is already included in the last 20 bars and it will always be within this range.  
The most recent (right or new bar) bar is the bar with index 0, the penultimate bar is the bar with index 1, etc.  

Perhaps you want to catch a breakout from a horizontal price channel.  If so, then the last (zero) bar should be checked against the range of previous ones (for example, 20 bars) starting from the 1st bar to the 20th. 
Do you want that?
IIf so, then it is better to use not the range, but these two functions to check whether the last bar is an extremum for the last N bars:

bool IsNewMaxForLastN_Bars (int N_Bars) {
   return iHighest(NULL,0,MODE_HIGH,N_Bars)==0;
}
bool IsNewMinForLastN_Bars (int N_Bars) {
   return iLowest(NULL,0,MODE_LOW,N_Bars)==0;
}

You can, of course, save the previous values of the extremes (max and min) and the indices of these extremes to increase performance, but this is not necessary. Even if you search for the maximum and minimum among the last 1000 bars, the search time will be less than 1 microsecond (0.000001 sec), so there is no point in complicating the code.
Reason: