How to check whether last candle is completely out of bollinger band

 

Hi Friends,

I am trying to write a piece of code to check whether the previous candle body is completely outside of Bollinger bands. So far I wrote the following.

  double bolinger_upper_band=iBands(NULL,0,mPeriod,mDeviation,mShift,mAppliedto,MODE_UPPER,1);
  double bolinger_lower_band=iBands(NULL,0,mPeriod,mDeviation,mShift,mAppliedto,MODE_LOWER,1);

   if (Close[1] > Open[1] && Open[1] > bolinger_upper_band)
   {Comment("Outside");}  
   else if (Close[1] < Open[1] && Close[1] > bolinger_upper_band)
   {Comment("Outside");}  
  
   else if (Open[1] > Close[1] && Open[1] < bolinger_lower_band)
   {Comment("Outside");}  
   else if (Open[1] < Close[1] && Close[1] < bolinger_lower_band)
   {Comment("Outside");} 

My problem is this is not giving me the exact result that I am looking for.

Ex 1: Following Image shows a candle body that is completely outside of BB. Which is what I am looking for.

Capture1


Ex 2: Following shows a candle body which is not completely outside as BB is touching it. I am not sure how to check this as I am new to the programming. Just learning the ropes.

Capture2

I would really appreciate if anyone could help me with this.

 

To get a full bar outside, you need to use Low[1] for the upper band and High[1] for the lower band.

You are using Open[1] and Close[1], which only define the body of the bar.

 
Thanks. I only want to have only the body outside BB, not the whole candle.
 
don per:
Thanks. I only want to have only the body outside BB, not the whole candle.

Oh. Disregard my previous message.

Use MathMin(Close[i],Open[i]) for the upper band, and MathMax(Close[i],Open[i]) for the lower band.

Also note: the body of Ex. 2 (above) is outside the BB. A bar doesn't actually have width, so only the actual price counts, not if it appears to visually touch.

 

Thanks for the sample and explanation. Much appreciated.

Is there any way that I could code it so that it will only look for candles similar to in Ex. 1? I agree ex.2 is out of the BB, but I want to code it to look for candles in Ex.1

 
Is there any way that I could code it so that it will only look for candles similar to in Ex. 1?

That was my previous answer:

Anthony Garot:

To get a full bar outside, you need to use Low[1] for the upper band and High[1] for the lower band.

So, something like:

   if (Low[1] > bollinger_upper_band[1])
   {Comment("Above");}  
   else if (High[1] < bollinger_lower_band[1])
   {Comment("Below");}   
Reason: