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
- Read the step with
SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP). - Round down to it:
lots = MathFloor(lots / step) * step;(down, so the risk is never above the plan). - Then apply the minimum and maximum volume (see the next rule).
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);
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
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.
Related MQL5 errors
More rules on volume and money
- 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.
- HighDivision by the tick value without a zero checkA 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.
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.