Download MetaTrader 5
To add comments, please log in or register
Share links on the Forum. Now they have Rich Content!
mop0
197
mop0 2014.03.11 18:29 

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.
Michele Lazzarini
470
Michele Lazzarini 2014.03.11 20:26  

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

Alain Verleyen
Moderator
28550
Alain Verleyen 2014.03.11 20:35  
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.


mop0
197
mop0 2014.03.11 21:27  

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?

Ian Venner
2583
Ian Venner 2014.03.11 22:45  

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.

/
To add comments, please log in or register