Volume Rounding Policies
A position sizer's "desired" lot size, computed from risk math, is almost never a legal multiple of the broker's lot step. Most published sizing snippets round it silently and move on. This header treats the gap between desired and actual size as a first-class output: exposureErrorLots and exposureErrorPct are always reported alongside the rounded value, because a 0.03-lot desired size rounding down to 0.0 on a 0.1-step account is not a rounding detail, it is a trade that silently never happens - the caller needs to see that immediately, not discover it later while reconciling account history. NormalizeLotStep applies one of three policies - ROUND_DOWN (never exceed the desired size), ROUND_NEAREST (minimise the absolute error either direction), ROUND_UP (never undershoot) - and clamps the result into [MODE_MINLOT, MODE_MAXLOT]. A ROUND_DOWN result that lands below MODE_MINLOT correctly returns 0.0 rather than being silently forced up to the broker's minimum - it is the caller's decision whether a zero-lot result means "skip this trade", not this header's. SplitAcrossTickets spreads a desired size that exceeds MODE_MAXLOT across several separate tickets, filling each at MODE_MAXLOT until the remainder fits in a final ticket, and reports the same honest exposure-error figures for the total actually placed - splitting spreads the rounding error across tickets, it does not make the error disappear. The division by MODE_LOTSTEP is epsilon-guarded throughout: 0.1 / 0.01 evaluates to 9.99999999999999998 in binary, and flooring that raw would silently lose one whole lot step on affected brokers. Notes and limitations - roundedLots of 0.0 under ROUND_DOWN is a VALID, correct result meaning "the desired size does not clear this broker's minimum trade size" - it is not an error and must be checked for explicitly before sending an order. - SplitAcrossTickets can legitimately place fewer than maxTickets if the desired size is fully covered sooner, or leave a small remainder unplaced if it falls below one lot step after the last allowed ticket - the note field states which happened. - This header only rounds a SIZE the caller already decided on. It has no opinion on how that size was computed and performs no risk calculation itself. - No thread or state concerns beyond the live MarketInfo calls for lot metadata.
Script · MT5 · v1.0 · Free
About Volume Rounding Policies
Volume Rounding Policies is a script for MetaTrader 5. MQL source code included.
Files in the download: VolumeRoundingPolicies.mq5.mqh, VolumeRoundingPolicies.txt, VolumeRoundingPoliciesDemo.ex5, VolumeRoundingPoliciesDemo.mq5.
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Volume · Risk & Lot Size · MT5 Script · MQL5 Source Code