Edgoro
Critical runtime error · MQL5

MQL5 'zero divide' error: causes and fix

Why an MT5 Expert Advisor stops with "zero divide", the values that are 0 more often than you think, and how to guard every division in MQL5.

Experts tab · critical error, the program stops
zero divide in 'MyEA.mq5' (31,40)

What it means

The program divided by zero. Like an array out of range, it's a critical error: MetaTrader stops the program at that line, and nothing after it runs. There's no error code to read afterwards.

Usual causes

  • A distance that can be 0. A stop distance when the entry equals the stop, a range when a bar's high equals its low, an ATR on a flat market or before it has data.
  • A broker value that can be 0. The spread (SYMBOL_SPREAD) on accounts and moments where it is zero, or a symbol property read before the terminal has data for the symbol: MetaQuotes' reference warns that the value can be undefined until the first quote arrives.
  • Counters. A win rate or an average computed from a count of trades or bars that is still 0.
  • Integer division. With two integers, 5 / 10 is 0, so an intermediate result that you later divide by can be 0 even when both inputs aren't.

With double values, don't count on the program stopping: depending on the case you may get the same error, or an infinite or not-a-number result that flows silently into your lot size or prices. Either way, check the divisor.

How to detect it

  • The Experts tab shows a line such as zero divide in 'MyEA.mq5' (31,40): the file, then the line and column of the division.
  • Go to that line in MetaEditor and print the divisor just before it to see which input made it 0.

Broken code

This EA compiles. The first tick with a zero spread stops it with zero divide.

MQL5
input double InpMinStopSpreads = 3.0; // Minimum stop, in spreads
input int    InpStopPoints     = 150; // Stop distance, points

void OnTick()
  {
   int spread = (int)SymbolInfoInteger(_Symbol, SYMBOL_SPREAD);
   // The stop as a multiple of the spread: the spread can be 0
   int stopInSpreads = InpStopPoints / spread;
   if(stopInSpreads < InpMinStopSpreads)
      return;
   Comment("Stop is ", stopInSpreads, " spreads wide");
  }

Fixed code

The same program with the check that prevents the error. It compiles in MetaEditor with 0 errors and 0 warnings.

MQL5
input double InpMinStopSpreads = 3.0; // Minimum stop, in spreads
input int    InpStopPoints     = 150; // Stop distance, points

void OnTick()
  {
   int spread = (int)SymbolInfoInteger(_Symbol, SYMBOL_SPREAD);
   // Compare by multiplying instead of dividing: no divisor, no zero divide
   if(InpStopPoints < InpMinStopSpreads * spread)
      return;
   if(spread <= 0)
     {
      Comment("Spread is 0: the stop filter passes");
      return;
     }
   Comment(StringFormat("Stop is %.1f spreads wide", (double)InpStopPoints / spread));
  }

Why the fix works

The filter now multiplies instead of dividing: "stop < 3 × spread" says the same thing as "stop ÷ spread < 3", and has no divisor. The one division that remains, for display, runs only after checking that the spread is above 0, and it divides as double, so 150 / 100 gives 1.5 instead of 1.

For lot sizing, the same rule applies: check the stop distance and the tick value before dividing by them, and skip the trade when one of them is 0 or negative.

Questions

Does dividing a double by zero also stop an EA?

Don't rely on either behaviour. You may get the zero divide error, or an infinite or not-a-number value that silently ends up in a lot size or a price. Check every divisor before dividing, whatever its type.

How do I find which line divides by zero?

The Experts tab names the file and gives the line and column in brackets, for example (31,40). Open the file in MetaEditor, go to that line, and print the divisor just before the division.

What's a safe way to divide in MQL5?

Check the divisor first and decide what the result should be when it's zero: skip the trade, use a default, or return early. A small helper that returns a fallback value when the divisor is 0 keeps the checks in one place. Where possible, compare by multiplying instead of dividing.

The programs on this page are minimal examples written to show the error and its fix, not trading systems. Some of them send orders when they run: try them on a demo account. Trading involves substantial risk of loss and is not suitable for every investor. Edgoro is a software tool, not a broker or financial adviser: nothing it produces is investment advice, and past or backtested performance does not guarantee future results.

Working code that avoids it

All MQL5 examples

Rather not write the error handling yourself?

Edgoro's MT5 EA generator writes complete MQL5 Expert Advisors from plain English and compiles each one in MetaEditor before you get it. A clean build proves the code compiles, not that a strategy works: test it on a demo account.