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
- Read the values into variables first and return early when one is 0 or less.
- Skip the trade on that tick; the values arrive with the symbol's next quotes.
Code that fires it
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
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
- HighCalculated lot not rounded to the volume stepA lot calculated from risk, balance or a multiplier is sent without rounding it to the symbol's volume step, which the server refuses with 10014.
- HighCalculated lot not checked against the minimum and maximumA calculated lot is sent without checking the symbol's minimum and maximum volume, so small accounts and large multipliers get 10014.
- MediumNo margin check before a new tradeThe EA opens trades without asking whether the free margin covers them, so it keeps sending orders that the server refuses with 10019.
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.