Skip to content

EA Doctor rule · Volume and money

Calculated lot not rounded to the volume step

A 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.

Why it matters

A lot that comes out of a formula (risk per trade, a share of the balance, a martingale multiplier) is rarely a whole multiple of the symbol's SYMBOL_VOLUME_STEP: 0.01 on many forex accounts, but 0.1 or 1 on others, and different again for indices and crypto.

The server refuses such a volume with 10014 (invalid volume). Rounding to two decimals is not the same thing: it fails on every symbol whose step is not 0.01.

When the rule fires

The EA trades, a variable whose name contains lot or volume is assigned the result of arithmetic (*, /, MathPow(), AccountInfoDouble() …), and SYMBOL_VOLUME_STEP (or CSymbolInfo::LotsStep()) appears nowhere in the file.

When it stays quiet

The step is read anywhere in the file, or the lots are fixed inputs passed on unchanged.

How to fix it

  1. Read the step with SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP).
  2. Round down to it: lots = MathFloor(lots / step) * step; (down, so the risk is never above the plan).
  3. Then apply the minimum and maximum volume (see the next rule).

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);
   if(tickValue <= 0.0 || tickSize <= 0.0 || slPoints <= 0.0)
      return(0.0);
   double lots = riskMoney / (slPoints * _Point / tickSize * tickValue);
   return(NormalizeDouble(lots, 2));
  }

void OpenBuy(const double riskMoney, const double slPoints)
  {
   if(!trade.Buy(RiskLots(riskMoney, slPoints), _Symbol))
      Print("Buy failed: ", trade.ResultRetcodeDescription());
  }

Fixed code

MQL5 · fixed
double NormalizeLots(double lots)
  {
   const double step = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
   const double minV = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
   const double maxV = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);
   if(step <= 0.0)
      return(0.0);
   lots = MathFloor(lots / step) * step;
   if(lots < minV)
      return(0.0);          // too small for this symbol: no trade
   return(MathMin(lots, maxV));
  }

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);
   double lots = riskMoney / (slPoints * _Point / tickSize * tickValue);
   return(NormalizeLots(lots));
  }

void OpenBuy(const double riskMoney, const double slPoints)
  {
   const double lots = RiskLots(riskMoney, slPoints);
   if(lots <= 0.0)
      return;
   if(!trade.Buy(lots, _Symbol))
      Print("Buy failed: ", trade.ResultRetcodeDescription());
  }

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.