anyone can help me to find which line of code is causing ZeroDivideError? MQL message is not with enough details. - page 2

 
Emanuel Cavalcante Amorim Filho #:


Glad you found it. I had a similar problem once (another error) and it took me a while to understand that the visual mode differs from the non-visual mode. For anyone with a similar issue, just insert lots of Print calls in your indicator code and try to figure out where the error is, since there isn't really an optimal way to debug it in this case.

Thanks @Emanuel Cavalcante Amorim Filho

 
Anil Varma #:

Hi @Michael Charles Schefe

No the indicator does not used any resource files.

Yes it used include files, which I am sure is not treated as 'resource'.

a resource file must be included with #resource key word

sorry -- include, not resource.

my comment was directed at the responder, not you.

 

You need to genearate a debug build. To do this use the Play button in the IDE. You can still add that build to a chart (don't need to run in debugger) then on a zero divide error will get the line number and column:

2025.07.17 08:58:50.727 runwiseFX_EA (EURUSD,M30) zero divide, check divider to avoid this error in 'runwiseFX_EA.mq5' (40,26)

Where with both Optimized and non Optimized builds you will get the less useful:

2025.07.17 09:02:50.006 runwiseFX_EA (EURUSD,H1) Zero divide, check divider for zero to avoid this error in 'X:\...\MQL5\Experts\Advisors\runwiseFX_EA.ex5'




 
Anil Varma:

Hi

Can someone help me to know which piece of code is causing Zero Divide error, so I can fix it.

2024.03.09 13:30:06.707 login (build 4153)

2024.03.09 13:30:06.722 template file tester.tpl added. 7012 bytes loaded

2024.03.09 13:30:06.725 expert file added: Experts\Algo\EAToTest\TestEA_iVP-v6.ex5. 89413 bytes loaded

2024.03.09 13:30:06.744 successfully initialized

2024.03.09 13:30:06.820 XAUUSD: symbol to be synchronized

2024.03.09 13:30:06.821 XAUUSD: symbol synchronized already, 18 bytes received

2024.03.09 13:30:06.821 XAUUSD: history synchronization started

2024.03.09 13:30:06.825 XAUUSD: load 27 bytes of history data to synchronize in 0:00:00.001

2024.03.09 13:30:06.825 XAUUSD: history synchronized from 2016.01.04 to 2024.03.06

2024.03.09 13:30:06.948 XAUUSD,M15: history cache allocated for 52008 bars and contains 43368 bars from 2022.01.03 01:00 to 2023.10.31 23:45

2024.03.09 13:30:06.948 XAUUSD,M15: history begins from 2022.01.03 01:00

2024.03.09 13:30:06.949 XAUUSD,M15 (Pepperstone-Demo): 1 minutes OHLC ticks generating

2024.03.09 13:30:06.949 XAUUSD,M15: testing of Experts\Algo\EAToTest\TestEA_iVP-v6.ex5 from 2023.11.01 00:00 to 2024.03.06 00:00 started with inputs:

2024.03.09 13:30:06.949   InpSymbol=XAUUSD

2024.03.09 13:30:06.970 2023.11.01 00:00:00   Zero divide, check divider for zero to avoid this error in 'C:\Users\anilh\AppData\Roaming\MetaQuotes\Tester\73B7A2420D6397DFF9014A20F1201F97\Agent-127.0.0.1-3000\MQL5\Experts\Algo\EAToTest\TestEA_iVP-v6.ex5::Indicators\Development\iVP-v6\iVP-v6-V2.01.ex5'

The indicator iVP-v6-V2.01.ex5 is tested without any error in Strategy Tester!!! However, when used it's class in EA, it is giving this error.

attached is screen shot of indicator running well in Strategy Tester.

Situation: Indicator running by itself in the Strategy Tester
MT5 first loads the chart, then calls
OnInit() then OnCalculate() after enough historical bars are already present to fill the entire calculation buffer. All sums, ranges, periods, etc. are immediately > 0, so every division executes


Situation:
Indicator used via iCustom handle inside an EA
1)
EA creates the handle with iCustom(…) (handle only, no calculation).
2)
EA immediately calls CopyBuffer() inside OnInit() or OnTick().
3)
Only now is the indicator calculated for the first timewith just 1 or 2 bars.
With so few bars, many running sums or period lengths are still
0, so any direct division or modulo using them will fault.

Quick fix: Search the entire source for / and % and wrap each operation so the denominator is validated first:

if (denominator == 0.0) continue; result = numerator / denominator;

or

double SafeDiv(double numerator, double denominator) {return denominator ? numerator / denominator : 0.0;}
or contract a freelancer to resolve it.