zero divide problem

 
{BR1=CandleBody1*100/CandleRange1;BodyRange1=MathAbs(BR1);}

So im trying to get % of the candle body to candle rande, im getting this error zero divide and the ea stops trading.

How can i fix that?

Thanks Piero

 
pieronetto:

So im trying to get % of the candle body to candle rande, im getting this error zero divide and the ea stops trading.

How can i fix that?

Thanks Piero

Check

CandleRange1>0.0

Find out why this is 0 (not ini,...)

 
void start()
{
      double PCS,PCL,
             CandleRange1,CR1,
             CandleBody1 ,CB1,
             UpperWick1  ,UW1,
             LowerWick1  ,LW1,
             BodyRange1  ,BR1;
             
      PCL=false;
      PCS=false;
      
      double CO1=iOpen (NULL,0,1);
      double CH1=iHigh (NULL,0,1);
      double CL1=iLow  (NULL,0,1);
      double CC1=iClose(NULL,0,1);
      
      if(CO1<CC1)PCL=true;
      if(CO1>CC1)PCS=true;
//+------------------------------------------------------------------+
//|  Candle Range High to Low Last Candle                |
//+------------------------------------------------------------------+
      {CR1=CH1-CL1;CandleRange1=MathAbs(CR1/Point);}
//+------------------------------------------------------------------+
//|  Candle Body Open to Close Last Candle               |
//+------------------------------------------------------------------+
      {CB1=CO1-CC1;CandleBody1 =MathAbs(CB1/Point);}
//+------------------------------------------------------------------+
//|  Upper Wick Last Candle                              |
//+------------------------------------------------------------------+
      {if(PCL)UW1=CH1-CC1;UpperWick1=MathAbs(UW1/Point);}
      {if(PCS)UW1=CH1-CO1;UpperWick1=MathAbs(UW1/Point);}
//+------------------------------------------------------------------+
//|  Lower Wick Current Last Candle                              |
//+------------------------------------------------------------------+
      {if(PCL)LW1=CL1-CO1;LowerWick1=MathAbs(LW1/Point);}
      {if(PCS)LW1=CL1-CC1;LowerWick1=MathAbs(LW1/Point);}
//+------------------------------------------------------------------+
//|  Body to Range in % Last Candle                      |
//+------------------------------------------------------------------+
      {BR1=CandleBody1*100/CandleRange1;BodyRange1=MathAbs(BR1);}
//+------------------------------------------------------------------+
//|  Comment on Chart Last Candle                        |
//+------------------------------------------------------------------+
      {Comment("Previous Candle","\n",
              "Candle Range   ",CandleRange1,"\n",
              "Candle Body    ",CandleBody1,"\n",
              "Upper Wick      ",UpperWick1,"\n",
              "Lower Wick      ",LowerWick1,"\n",
              "Body to Range   ",BodyRange1,"%","\n");
      }

If body of the candel is Open=Close i have a 0 and dividing is the error. how i get around it do i have to say CandleBody>0

If Open=Close in the Body then the % to the range (High Low) should be 0% I understand u cant divid through 0 and i get the error

Please help im so ?????????

 

Just make something like

 if (Open==Close)var1=0;
 
pieronetto:

If body of the candel is Open=Close i have a 0 and dividing is the error. how i get around it do i have to say CandleBody>0

If Open=Close in the Body then the % to the range (High Low) should be 0% I understand u cant divid through 0 and i get the error

Please help im so ?????????

1) CandleBody = fmax(Point,fabs(Open-Close)); // there will be at least 1 Point difference, you can even try fmax(0.5*Point,..) it depends what you want.

2) If ( CandleBody== 0.0 ) CandleBody = x; // what ever your ideas need

 
deysmacro:

Just make something like


{CB1=CO1-CC1;CandleBody1 =MathAbs(CB1/Point);}
That where i get the 0 from when Open==Close, CandleBody1==0 and the MathAbs create a error, logic
{BR1=CandleBody1*100/CandleRange1;BodyRange1=MathAbs(BR1);}
0*100=0 and 0/any volume=0 all that=error
i allready say it is 0 why doing it again with
if (Open==Close)var1=0;
it came out to be the same result error
im still confusde here.
Thanks for you response.
 

Actually, if there is zero divide, what is your solution?

if (Open==Close)var1=0; 


OR


if (Open==Close){ do something here }


It really depends on what you want to do.

 

if(CandleRange1==0)
   BodyRange1=0;
else
   {BR1=CandleBody1*100/CandleRange1;BodyRange1=MathAbs(BR1);}
You could do something like this
 

As I remember 0 candle values (HLOCV) mean that history is not (yet) loaded. I don't know any programming solution for loading history, only hacks, workarounds.

Check the values for 0 before use. When 0 wait and try again later.

 
      if(CandleBody1==0)
      BodyRange1=0;
      {BR1=CandleBody1*100/CandleRange1;BodyRange1=MathAbs(BR1);}

Thank you you very much for all your response and that sames to fix the problem as you see,

thanks Piero

 
void start()
{
      double PCS,PCL,
             CandleRange1,CR1,
             CandleBody1 ,CB1,
             UpperWick1  ,UW1,
             LowerWick1  ,LW1,
             BodyRange1  ,BR1;
             
      PCL=false;
      PCS=false;
      
      double CO1=iOpen (NULL,0,1);
      double CH1=iHigh (NULL,0,1);
      double CL1=iLow  (NULL,0,1);
      double CC1=iClose(NULL,0,1);
      
      if(CO1<CC1)PCL=true;
      if(CO1>CC1)PCS=true;
//+------------------------------------------------------------------+
//|  Candle Range High to Low Last Candle                            |
//+------------------------------------------------------------------+
      {CR1=CH1-CL1;CandleRange1=MathAbs(CR1/Point);}
//+------------------------------------------------------------------+
//|  Candle Body Open to Close Last Candle                           |
//+------------------------------------------------------------------+
      {CB1=CO1-CC1;CandleBody1 =MathAbs(CB1/Point);}
//+------------------------------------------------------------------+
//|  Upper Wick Last Candle                                          |
//+------------------------------------------------------------------+
      {if(PCL)UW1=CH1-CC1;UpperWick1=MathAbs(UW1/Point);}
      {if(PCS)UW1=CH1-CO1;UpperWick1=MathAbs(UW1/Point);}
//+------------------------------------------------------------------+
//|  Lower Wick Current Last Candle                                  |
//+------------------------------------------------------------------+
      {if(PCL)LW1=CL1-CO1;LowerWick1=MathAbs(LW1/Point);}
      {if(PCS)LW1=CL1-CC1;LowerWick1=MathAbs(LW1/Point);}
//+------------------------------------------------------------------+
//|  Body to Range in % Last Candle                                  |
//+------------------------------------------------------------------+
      if(CandleBody1==0)
      BodyRange1=0;
      {BR1=CandleBody1*100/CandleRange1;BodyRange1=MathAbs(BR1);}
//+------------------------------------------------------------------+
//|  Comment on Chart Last Candle                                    |
//+------------------------------------------------------------------+
      {Comment("Previous Candle","\n",
              "Candle Range   ",CandleRange1,"\n",
              "Candle Body    ",CandleBody1,"\n",
              "Upper Wick      ",UpperWick1,"\n",
              "Lower Wick      ",LowerWick1,"\n",
              "Body to Range   ",BodyRange1,"%","\n");
      }
Reason: