How to add +1 till the right value is achieved

 
Hi guys I have a question, the code below is supposed to check if current open price of the current bar opened between open and close prices of the previous bar, if not its supposed to add +1 and check the the other bar till it returns the number of the correct bar, but the max value it returns is 3 how can I fix this?
int shift_value(string symbol, ENUM_TIMEFRAMES period)
{ 
 int shift=2;
 
 double current_open= NormalizeDouble(iOpen(symbol,period,0),_Digits);
 double shift_open= NormalizeDouble(iOpen(symbol,period,shift),_Digits);
 double shift_close= NormalizeDouble(iClose(symbol,period,shift),_Digits);
 
 bool shift_red_candle= (shift_open>shift_close);
 bool shift_green_candle= (shift_open<shift_close);
 
 bool shift_red_check= (current_open<shift_open) && (current_open>shift_close);
 bool shift_green_check= (current_open>shift_open) && (current_open<shift_close);
 
 if(shift_red_candle==true && shift_red_check==false)
  {
   shift=shift+1;
  }
 else if(shift_green_candle==true && shift_green_check==false)
  {
   shift=shift+1;
  }
 
 return shift;
}
 
  1. Derrick Mutange but the max value it returns is 3 how can I fix this?

    You start with two and if condition1 add one, else if condition2 add one. Your code can only return two or three. Why does this surprise you? Fix what? You haven't explained what you want.

  2. Derrick Mutange: check the the other bar till it returns the number of the correct bar,

    You only check the current bar vs. shift bar. You don't check other bars. What do you mean by  “the number of the correct bar?” Bar index?

  3. You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and “if long entry” is an incomplete sentence.

 
William Roeder #:
  1. You start with two and if condition1 add one, else if condition2 add one. Your code can only return two or three. Why does this surprise you? Fix what? You haven't explained what you want.

  2. You only check the current bar vs. shift bar. You don't check other bars. What do you mean by  “the number of the correct bar?” Bar index?

  3. You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and “if long entry” is an incomplete sentence.

Hey William thanks for the reply. 
How would you suggest I go about checking the current bar vs the next bar if the bar 2 fails it checks 3 if 3 fails then 4 etc... till the bar that satisfies the conditions is reached? 

 

2, 3, 4, … is called a loop.

Don't double post! You already had this thread open.
          General rules and best pratices of the Forum. - General - MQL5 programming forum (2017)

 
Derrick Mutange #:

Do not double post!

I have deleted your duplicate topic.

Thanks for wasting my and other people's time!!!!!

 
Derrick Mutange:
Hi guys I have a question, the code below is supposed to check if current open price of the current bar opened between open and close prices of the previous bar, if not its supposed to add +1 and check the the other bar till it returns the number of the correct bar, but the max value it returns is 3 how can I fix this?

don't use NormalizeDouble it is not needed

use a loop to go through how many bars you want to check in the example below that is 99

current bar is index zero so start checking at 1

simplify the condition checking

return the number of true conditions counted 

Is this really what you want though because it will check all the bars and return a count, you talk about a correct bar but that is not defined.

int shift_value(string symbol, ENUM_TIMEFRAMES period)
{ 
   int iCounted = 0;
   double current_open= iOpen(symbol,period,0);
   for(int iShift=1; iShift<100; iShift++)
   {
      double shift_open = iOpen(symbol,period,iShift);
      double shift_close = iClose(symbol,period,iShift);
    
      if(current_open < MathMin(shift_open, shift_close) || current_open > MathMax(shift_open, shift_close))
         iCounted++;
   }
    
   return iCounted;
}
Reason: