Download MetaTrader 5
To add comments, please log in or register
Publish programs in Code Base. Improve your rating!
Stephen Reynolds
484
Stephen Reynolds 2016.02.03 12:52 

When i use these 2 similar codes to find candle patterns how come the 1st one has no errors on lowerShadow or upperShadow objects yet the 2nd one

shows error on mainBodyBear, mainBodyBull, midBody objects. I cant figure it out?

//------------- 
// Candle Patterns   (Hammer/ShootingStar)
//--------------   
double bearBody = MathAbs(Open[1] - Close[1]);  
double bullBody = MathAbs(Close[1] - Open[1]);     
double lowerShadow, upperShadow;                           

if(Close[1] < Open[1])    // Bearish body (not candle signal)   
{
   lowerShadow = Close[1] - Low[1];       
   upperShadow = High[1] - Open[1];    
}
else                                  // Bullish body (not candle signal)   
{
   lowerShadow = Open[1] - Low[1];
   upperShadow = High[1] - Close[1]; 
}

bool bullHammer = lowerShadow > (body * 2) && upperShadow < body;
bool bearShootingstar = upperShadow > (body * 2) && lowerShadow < body;



//------------- 
// Candle Patterns   (Morning/Evening Star)
//--------------    
// Candlestick patterns EveningStar/MorningStar  
double mainBody = 30 * _Point; 
double midBodyPlus = 10 * _Point; 
double midBodyMinus = -10 * _Point;    
double mainBodyBear,mainBodyBull,midBody; 

if(Close[3] < Open[3]) 
{
   mainBodyBear = Open[3] - Close[3]; 
}
else
{
   mainBodyBull = Close[3] - Open[3];   
}
             
if(Close[2] < Open[2] || Close[2] > Open[2])
{
   midBody = Open[2] - Close[2];     
}
                 
bool candleMorningStar = Close[1] > Open[1] && Close[1] > Open[3] && mainBodyBear > mainBody && midBody < midBodyPlus && midBody > midBodyMinus;
bool candleEveningStar = Close[1] < Open[1] && Close[1] < Open[3] && mainBodyBull > mainBody && midBody < midBodyPlus && midBody > midBodyMinus;

















Blahtech Limited
2760
James Cater 2016.02.03 13:12  

Because lowerShadow, upperShaddow are guaranteed to be initilised before they are used (they are BOTH initialised in the if and the else of the first if block/condition)

However only one of the mainBodyBear or mainBodyBull is initialised in the second if block

Generally you should initialise all stack based varibles as it can often get you into trouble


double  mainBodyBear = 0;

double  mainBodyBull = 0;

double  midBody = 0;
whroeder1
13620
whroeder1 2016.02.03 15:04  
Quiet the compiler
double lowerShadow=0, upperShadow=0;

if(Close[1] < Open[1])    // Bearish body (not candle signal)   
{
   lowerShadow = Close[1] - Low[1];       
   upperShadow = High[1] - Open[1];    
}
else                                  // Bullish body (not candle signal)   
{
   lowerShadow = Open[1] - Low[1];
   upperShadow = High[1] - Close[1]; 
}
Or just initialize them as you declare them
double lowerShadow = Close[1] < Open[1] ? Close[1] -  Low[1] : Open[1] -   Low[1];
double upperShadow = Close[1] < Open[1] ?  High[1] - Open[1] : High[1] - Close[1];
 Or simplify and self document.
 
double lowerBody   = MathMin(Close[1], Open[1]),
       upperBody   = MathMax(Close[1], Open[1]);
double lowerShadow = lowerBody -    Low[1];
double upperShadow = High[1]   - upperBody;
/
To add comments, please log in or register