Simple BOOLEAN FUNCTION HELP

 

Hi Coders,

I am struggling with this simple function.. All I want it to do is look back over the past 10 candles and return true if any of the candles where higher than the upper bollinger band.my error '}' Not all control paths return a value. I just can't see where I am going wrong here. 

 bool HasBollingerCrossed()
 {
  
   for(int i=Bars-1;i>=1;i=10)
      {
      if(High[i] > (iBands(NULL,0,21,2,0,PRICE_CLOSE,MODE_UPPER,i)))
       
      return true;
       }
  }

thanks in advance

 
Adam Woods:

Hi Coders,

I am struggling with this simple function.. All I want it to do is look back over the past 10 candles and return true if any of the candles where higher than the upper bollinger band.my error '}' Not all control paths return a value. I just can't see where I am going wrong here. 

thanks in advance

At first glance it looks like you need another return statement


bool HasBollingerCrossed()
 {
  
   for(int i=Bars-1;i>=1;i=10)
      {
      if(High[i] > (iBands(NULL,0,21,2,0,PRICE_CLOSE,MODE_UPPER,i)))
       
      return true;
       }

  return false; //<< Try this

  }
 
R4tna C #:

At first glance it looks like you need another return statement


I am not sure if this is intentional, but as far as I can tell, this makes no sense.

   for(int i=Bars-1;i>=1;i=10)

This for loop will stay at i == 10.

What is the intention? If this behaviour is intended, a while loop would be a better use case here.
 
Adam Woods:

Hi Coders,

I am struggling with this simple function.. All I want it to do is look back over the past 10 candles and return true if any of the candles where higher than the upper bollinger band.my error '}' Not all control paths return a value. I just can't see where I am going wrong here. 

thanks in advance

Read my post, I have replied to the wrong post.

It was meant for you, not for R4tna C.



 
Dominik Christian Egert #:
I am not sure if this is intentional, but as far as I can tell, this makes no sense.


This for loop will stay at i == 10.

What is the intention? If this behaviour is intended, a while loop would be a better use case here.

Well spotted - infinite loop by the looks of it

 
R4tna C #:

Well spotted - infinite loop by the looks of it

Thank you.

But more a conditional exit than an infinite loop. Isn't it?

 
Dominik Christian Egert #:
Thank you.

But more a conditional exit than an infinite loop. Isn't it?

Maybe, I was thinking that if i starts at >= 1, and is then set to 10 on every iteration, as the exit condition is i>=1, it could run forever... but now I see it would exit when i >= 1

 
R4tna C #:

Maybe, I was thinking that if starts at >= 1, and is then set to 10 on every iteration, as the exit condition is i>=1, it could run forever... 

Yes.

I am not sure if high[i] updates, but if it does, it will some time maybe exit, as it has a conditional return included.

Or it will fail with "array out of range"

Anyways, it seems wrong.

 
R4tna C #:

Maybe, I was thinking that if i starts at >= 1, and is then set to 10 on every iteration, as the exit condition is i>=1, it could run forever... but now I see it would exit when i >= 1

No, it continues to run as long as i >= 1...

It won't exit until the inner condition is met.

 
Dominik Christian Egert #:
No, it continues to run as long as i >= 1...

It won't exit until the inner condition is met.

Ah yes, confusion reigns (especially for me on Friday afternoons LOL)

I was intrigued so had to test it - it is infinite, and as you say will only exit if the inner if becomes true. Have a good weekend!

PS - @Adam Woods - please see the usage of Bars() in this image - yours did not seem right


 
  1. R4tna C #: PS - @Adam Woods - please see the usage of Bars() in this image - yours did not seem right
    Because the code is MT4
    High[i] > (iBands(NULL,0,21,2,0,PRICE_CLOSE,MODE_UPPER,i)))
  2. Adam Woods: I am struggling 

    Why did you post your MT4 question in the MT5 EA 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.

Reason: