Loading…

Broker-Safe Lot Normalization

Error 131 on MQL4 (invalid trade volume) or a TRADE_RETCODE_INVALID_VOLUME result on MQL5 is one of the most common reasons an otherwise working EA fails on a broker it was not written against. Every server publishes three volume constraints: a minimum, a maximum, and a step. A volume that violates any of them is rejected outright. Rounding to the step looks like a one-line job and is not. The trap is floating point. In binary arithmetic 0.1 / 0.01 does not evaluate to 10, it evaluates to 9.99999999999999998. Round that down and you have silently halved a position, and because the code looks obviously correct the bug survives review. Every division in this header is nudged by an epsilon before it is rounded. This is a property of IEEE-754 binary floating point, not of either platform's trading model, so the fix is byte-for-byte the same arithmetic in both the MQL4 and MQL5 versions of the header. The part that matters more than the arithmetic is direction. Most libraries give you one normalize function, which forces the same rounding behaviour onto every call site. That is wrong, because the safe direction depends on intent: FloorLots never rounds up. Use it for any volume derived from a risk budget. Rounding up spends more risk than the settings authorised, and it does so quietly. When even the broker minimum would exceed the budget, FloorLots returns zero - a refusal, not a size. The caller must skip the trade. Substituting the minimum instead is exactly how an account ends up risking several times what the inputs claim. CeilLots never rounds down. Use it when closing, hedging or covering, where the dangerous outcome is an uncovered fragment left open rather than slightly too much closed. RoundLots takes the nearest step and clamps into range. Use it when the exact size is a preference rather than a limit. Also included: LotDigits derives the correct decimal count from the step so your prints and NormalizeDouble calls agree with the broker; IsValidLots for asserts and pre-trade validation; and LotConstraintsToString, a one-line summary to drop into OnInit. Printing that single line at attach time has saved more debugging hours than anything else in the file. Both headers are self-contained. No dependencies beyond the platform's own built-in functions, no DLLs, no classes - plain functions you can call from a script, an indicator or an EA. Notes and limitations - Volume-constraint values come from the server (MarketInfo on MQL4, SymbolInfoDouble on MQL5). On a symbol the terminal has never synchronised, they can be zero. The fallbacks here keep the code running, but a fallback is a guess - call LotConstraintsToString at startup and read it. - Neither header checks margin. A volume can be perfectly valid and still be refused for insufficient funds. Use a margin check as well. - The maximum is a per-order limit. Brokers frequently also impose a total volume limit per symbol that neither MarketInfo nor SymbolInfoDouble exposes at all. - FloorLots returning zero is deliberate and is the whole point of the design. If you find yourself writing MathMax(FloorLots(x), LotMin()), stop - you have reintroduced the bug the function exists to prevent. - No thread or state concerns: every function is pure and reads only broker properties.

Script · MT5 · v1.0 · Free

About Broker-Safe Lot Normalization

Broker-Safe Lot Normalization is a script for MetaTrader 5. It has 2 settings, such as Risk amount and Risk per lot. MQL source code included.

Files in the download: LotNormalize.mq5.mqh, LotNormalize.txt, LotNormalizeDemo.ex5, LotNormalizeDemo.mq5.

Settings

SettingDefaultWhat it does
Risk amount250.0Risk budget for the worked example
Risk per lot1800.0Loss on one lot at the intended stop

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: Volume · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria