Weird behaviour, calculating a Bollinger Band Squeeze and issues with division of variables?

 

Hi

I'm really quite stuck and not sure how to proceed, so I'm hoping for some inspiration really.

I've written a function that uses a Bollinger band with deviation of 2 and another with a deviation 1 which I use to look for a 'zone' and a 'squeeze'. The squeeze is found using the calculation  (upperBB value - lower BB value)/mid band value. I was then dividing that by the ATR value to give me the bandwidth as a ratio to ATR. After a 'squeese' the the EA I'm developing uses a combination of RSI, ADX and OBV values and where price is in relation to the BB bands, to look for valid trade. 

Initially it all seemed to work, then I noticed my EA would generate unexpected trades in strange places. I used the MetaEditor's debuger to step through my code and found logical conditions not following the rules of logic! So basically although the individual indicator functions were returning false for say a long trade. The condition was being set to true. For example, the code below would end up with newSignalsGoLong holding true, when currBBzone had the string "upperZone" or "middleZone". 


bool newSignalsGoLong = currBBZone=="LowerZone"  &&   openSignalAVGOBV=="Long" && openSignalRSI=="Buy";
I even made made newSignalsGoLong=false and commented out the other conditions and still newSignalsGoLong gets miraculously set to true and a trade is actioned. 


This reminded me of similar problems way back in my programming history with Borland C, where I had done something that caused my code to corrupt memory, because say I had messed up using arrays. I've spent two days double checking array definitions and such and and they seem ok and look like those used in others people's tutorials. It seems that the problem comes down to the function to calculate the squeeze. This is the function:

double CalcBandWidthRatio(double upperBB,double lowerBB,double midBB,double currATRvalue, bool &squeeze){


//check for 0 values (and other non number) as division cause coruption and random trades!
double ratio=0;
if (midBB>0 && currATRvalue>0  && MathIsValidNumber(currATRvalue) && MathIsValidNumber(midBB)){
    double diff=NormalizeDouble((upperBB-lowerBB),_Digits);
//***************this is the bit that seems to be causing the problems************    
    double bandwidth=(diff/midBB);
    if (bandwidth>0 && currATRvalue>0  && MathIsValidNumber(currATRvalue) && MathIsValidNumber(bandwidth))
        ratio =(bandwidth/currATRvalue);
   //if I comment out these three lines above no wierd logic occures 
//********* the code below here does not seem to cause problems
    ratio=NormalizeDouble(ratio,2);
    if (ratio <InpSqueeze){ 
      scanMode ="walkthechannel";
      countCandlesSinceSqueeze=true;
      }
    
    if (ratio >(InpSqueezeThreshold + InpSqueeze)) squeeze=false;
    else squeeze = true;
    }
else {
    ratio =5; //just a value I've picked randomly.
    Print ("phew that was lucky we had a duff number there!");//This has never been printed though
    }
return ratio;
}

  
I back tested this for 6 months. with those two lines of division commented out and the other conditions commented out no trades are opened. However, I've copied this function into the TruthfullTrading BB Tutorial code and it seems to run without weird behaviour, but I guess that could just be luck and how the compiler has compiled the code.   

Now I'm at a loss on how to proceed, I've spent over 24 hours on this 'weirdness' and I'm out of ideas. I'm wondering if anyone else has experience such problems and what else may cause the problems?

Thank you for reading and even more thanks if you can you point me in the direction of the problem.