Intermittent divide by zero error

 

My EA was giving me an intermittent divide by zero error while doing backtests of AUD/NZD. The journal pointed the source of the error to my lot size calculation below, specifically the (1/NUrate) part.

double NUrate = iClose("NZDUSD",0,0); //also tried   MarketInfo("NZDUSD",MODE_BID)
Lots = (Risk*Balance) / (SL*1000) * (1/NUrate);

It was not a consistent error, sometimes it would occur and sometimes it wouldn't, making it hard to pinpoint the exact source of the error and making it even harder to verify if I fixed it.

At the end of the day I thought it might be because I was using the 0 bar for the iClose("NZDUSD",0,0) so I changed it to:

double NUrate = iClose("NZDUSD",0,1);

I am aware that there might be an issue when trying to run a test using the zero bar of another timeframe of the same symbol, so I thought that might be also true of trying to use another symbol's 0 bar.

As I continue testing, time will tell if the issue crops up again but maybe someone knows for sure if I have fixed the problem?

Edit: additional info, it seems to be something to do with the dates I choose to test. If it starts too early, the divide by zero error might occur.
 

to avoid the error, before to divide check the divisor is > 0

add checkpoints with Alert() or Print() to display the status of each variable involved

 
eempc:

My EA was giving me an intermittent divide by zero error while doing backtests of AUD/NZD. The journal pointed the source of the error to my lot size calculation below, specifically the (1/NUrate) part.

It was not a consistent error, sometimes it would occur and sometimes it wouldn't, making it hard to pinpoint the exact source of the error and making it even harder to verify if I fixed it.

At the end of the day I thought it might be because I was using the 0 bar for the iClose("NZDUSD",0,0) so I changed it to:

I am aware that there might be an issue when trying to run a test using the zero bar of another timeframe of the same symbol, so I thought that might be also true of trying to use another symbol's 0 bar.

As I continue testing, time will tell if the issue crops up again but maybe someone knows for sure if I have fixed the problem?

Edit: additional info, it seems to be something to do with the dates I choose to test. If it starts too early, the divide by zero error might occur.

Did you read the documentation of iClose()?

Returned value

Close price value for the bar of specified symbol with timeframe and shift. If local history is empty (not loaded), function returns 0. To check errors, one has to call the GetLastError() function.


 

So how does one ensure that the local history is always loaded?

If I just had the NZDUSD chart open in the background as well as the AUDNZD chart running the EA, would that always ensure that the local history is loaded?

 

I believe if you call the iClose() before you need it and if the history is not there for that pair and timeframe, calling it forces the history to be loaded so it should be there by the next tick.

Reason: