Skip to content

EA Doctor rule · Volume and money

Division by the tick value without a zero check

A lot formula divides by the tick value or tick size, which is 0 until the symbol's data arrive, and the EA stops with a zero divide error.

Why it matters

SymbolInfoDouble() returns 0 for SYMBOL_TRADE_TICK_VALUE and SYMBOL_TRADE_TICK_SIZE when the symbol's data are not there yet: right after the terminal starts, for a symbol that is not in Market Watch, or for a cross whose conversion rate has not loaded.

A formula that divides by that value stops the EA with the critical runtime error "zero divide": MetaTrader removes the program from the chart, and open trades are left without their management.

When the rule fires

A variable read from SYMBOL_TRADE_TICK_VALUE or SYMBOL_TRADE_TICK_SIZE (or CSymbolInfo TickValue() / TickSize()) is used as a divisor and the file never compares that variable with zero; or the call itself is the divisor.

When it stays quiet

The variable is compared with 0 anywhere in the file (if(tickValue <= 0.0) return;), or it is only multiplied.

How to fix it

  1. Read the values into variables first and return early when one is 0 or less.
  2. Skip the trade on that tick; the values arrive with the symbol's next quotes.

Code that fires it

MQL5 · fires this rule
double RiskLots(const double riskMoney, const double slPoints)
  {
   const double tickValue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
   const double tickSize  = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
   return(riskMoney / (slPoints * _Point / tickSize * tickValue));
  }

Fixed code

MQL5 · fixed
double RiskLots(const double riskMoney, const double slPoints)
  {
   const double tickValue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
   const double tickSize  = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
   if(tickValue <= 0.0 || tickSize <= 0.0 || slPoints <= 0.0)
      return(0.0);   // no data yet: no trade on this tick
   return(riskMoney / (slPoints * _Point / tickSize * tickValue));
  }

The fixed code shows this rule’s fix and compiles in MetaEditor for MetaTrader 5 (with CTrade trade, InpLots and InpMagic declared, as in a typical EA). Other rules may still apply to it as a fragment: check your whole EA.

More rules on volume and money

Sources

Get your EA fixed

Check a whole EA for this and 29 other mistakes with EA Doctor, free and in your browser. Or send it to us: we confirm the price in writing, then fix it.