Small Zero Divide problem

 

Any help would be appreciated:

if(stopLoss >0)
{
dollarrisk= equitypercent*AccountEquity();
riskperpip=dollarrisk/stopLoss;
lots=riskperpip/10;
}
else
{
dollarrisk= equitypercent*AccountEquity();
riskperpip=dollarrisk/ATR2; -------------------> REASON FOR ERROR
lots=riskperpip/10;

ATR =iATR(NULL,0,14,0);

ATR2 =ATR*2;

I've tried multiplying ATR2 *10,000 to make it a regular integer but it still gives me the same error.

Thanks,

BB

 

We are missing some info to pinpoint the exact problem -- your variable data types at declaration. There is a priority rule in MQL4 that says that whatever the first variable you use in a calculation will be how the rest get treated. So.. If dollarrisk is an integer and ATR2 is 0.99 for example, the decimals of ATR2 will get lost and you will then be dividing by 0. Sometimes it becomes necessary to force double calculations by doing 0.0 + whatever_int_you_want_to_add.

Another thing is that in this code, you are not checking for ATR2 to be non-zero. In your first condition, you need stoploss to be greater than 0, so dividing by a stoploss there is fine since you know it's gonna be a non-zero value. ATR2 is not checked though.

Jon

 

Try this:

else
{

ATR =iATR(NULL,0,14,0);

ATR2 =ATR*2;

dollarrisk= equitypercent*AccountEquity();
riskperpip=dollarrisk/ATR2;
lots=riskperpip/10;

Reason: