zero devide

 
Hi,
I am new to mql4 and I am trying to set up an ea. The problem is that the error zero devide keeps coming up in my experts tab and when i run a test there is a huge list with this error while trades were also made. From what I have read here on this forum the reason must be a division by zero in the code but looking over and over it again i couldn't find it.

The thing I dont't understand is that this ea also works as it should when I use it live on a demo account but also gives the zero devide error. Could someone please help me out here?? Below you'll find the code i think is causing this error as it is the only part in the ea where division takes place. With this code I try to determine the proper lotsize with a given stop loss and risk. The stop loss is a high/low from one of the previous bars or when it's closer than 5 pips to teh price, it is 5 pips per default. The risk is set to 2 pct of teh free margin. My broker uses 5 decimal prices.


int bar;
double sl_buy=Bid; //stop loss by buy
double sl_sell=Ask; //stop loss by sell

for(bar=0;bar<=Quant_Bars-1;bar++)
{
if (Low[bar]< sl_buy )
{
sl_buy = Low[bar];
if (Bid-sl_buy<=50*Point)
{
sl_buy=Bid-50*Point;
}
}


if (High[bar]> sl_sell) // by sell
{
sl_sell = High[bar];
if(sl_sell-Ask<50*Point) //If highest high<=5 pips, high = 5
{
sl_sell=Ask+50*Point;

}
}
}


double free, minlot,costppbuy,costppsell, lotrawbuy, lotrawsell,lotbuy, lotsell;
string sym =Symbol();

free=AccountFreeMargin();
minlot = MarketInfo( sym, MODE_MINLOT );


costppbuy = (free*risk)/((Bid-sl_buy)/Point);
lotrawbuy= MathRound((costppbuy)/minlot);
lotbuy= lotrawbuy*minlot;

costppsell=(free*risk)/((sl_sell-Ask)/Point);
lotrawsell= MathRound((costppsell)/minlot);
lotsell=lotrawsell*minlot;
 
Why don't you just narrow it down for yourself by sending each of your divisors to the log using Print()?

CB
 

I’m a bit addicted to debugging code. So, here we go:

1. You have:
double sl_buy=Bid; //stop loss by buy
double sl_sell=Ask; //stop loss by sell

2. The if(...) conditions didn't fire. So, sl_buy and and sl_sell remain with their initial values


3. In this case, the following assignments

costppbuy = (free*risk)/((Bid-sl_buy)/Point);

costppsell=(free*risk)/((sl_sell-Ask)/Point);


trigger a zero divide error.

 
abstract_mind wrote >>

I’m a bit addicted to debugging code. So, here we go:

1. You have:
double sl_buy=Bid; //stop loss by buy
double sl_sell=Ask; //stop loss by sell

2. The if(...) conditions didn't fire. So, sl_buy and and sl_sell remain with their initial values


3. In this case, the following assignments

costppbuy = (free*risk)/((Bid-sl_buy)/Point);

costppsell=(free*risk)/((sl_sell-Ask)/Point);


trigger a zero divide error.




Thanks for your quick reply. Do you perhaps know why the if(..) condition didn't work? It's a bit confusing... When I insert Alerts just to show the values used in the calculation, they seem to be working ok, ie no zero values are shown . Also, in between the zero devide errors, trades are made with different lotzises implying the code works. Still the zero devide error keeps coming up. I don't understand how this is possible.
 
In this case Print() will be your friend. Use it so that you can scan the log to find which of the components of your divisors is zero, along which the bar index at that point. CB
 
cloudbreaker wrote >>
Why don't you just narrow it down for yourself by sending each of your divisors to the log using Print()?

CB


Thnks for your reply. Thats's what I did. I only used alert instead of print. The thing is that there are no zero values shown so im a bit puzzled as to where this error comes from.
 
I found the MT4 mathematic stuff is a little buggy.
Some combination equation may lead to wrong.
Try to keep them simple.
 
// for the longs u want sl at the low of the last quant_bars or otherwise 50 points below the current bid.
// for the shorts .....
// check it:

int bar;
double sl_buy=Bid-50*Point; //stop loss by buy <------ HERE
double sl_sell=Ask+50*Point; //stop loss by sell <------ HERE

for(bar=0;bar<=Quant_Bars-1;bar++)
{
if (Low[bar]< sl_buy )
{
sl_buy = Low[bar];
}
//if condition removed HERE

if (High[bar]> sl_sell) // by sell
{
sl_sell = High[bar];
}
//if condition removed HERE
}


// there are better approaches to obtain the lowest low of the last quant_bars, without iterating.
// Use low and lowest arrays.
 
abstract_mind wrote >>


// for the longs u want sl at the low of the last quant_bars or otherwise 50 points below the current bid.
// for the shorts .....
// check it:

int bar;
double sl_buy=Bid-50*Point; //stop loss by buy <------ HERE
double sl_sell=Ask+50*Point; //stop loss by sell <------ HERE

for(bar=0;bar<=Quant_Bars-1;bar++)
{
if (Low[bar]< sl_buy )
{
sl_buy = Low[bar];
}
//if condition removed HERE

if (High[bar]> sl_sell) // by sell
{
sl_sell = High[bar];
}
//if condition removed HERE
}


// there are better approaches to obtain the lowest low of the last quant_bars, without iterating.
// Use low and lowest arrays.



Thanks for your help. Im gonna try this solution tonight.