Expert Advisors • Indicators • Scripts • Libraries

MQL.RobotFX.org is the biggest collection of MetaTrader expert advisors (MT5 & MT4), indicators, scripts and libraries that can be used to improve trading results, minimize risks or simply automate trading tasks

Metatrader 5 Libraries | Position sizing from risk, using the broker's own tick value and volume step | MQL…

New free code from MQL5: indicators, EAs, and scripts for traders.

Every lot size calculator that assumes one pip is worth ten dollars is wrong on gold, wrong on indices, wrong on JPY crosses and wrong on any account not denominated in USD - which is exactly where oversizing hurts. This header does the arithmetic from what the server actually publishes for the symbol instead.

What it does Image for Position sizing from risk, using the broker's own tick value and volume step

CalcLots() takes a risk amount in account currency, an entry price and a stop price, and returns the volume to trade together with what that volume really risks once the broker's constraints have been applied.

  • It always rounds volume DOWN onto the broker's volume step. Rounding up would silently risk more than you asked for, which is the one thing a sizing routine must never do.
  • When the requested risk is smaller than the minimum lot it does not pretend. It returns the minimum, sets clamped_min, and reports the risk that position actually carries - which will be larger than the request.
  • It is side-agnostic. The stop may sit above or below the entry.

It also returns the stop distance in points and the value of one point per 1.00 lot, so a caller can show its working rather than just a number.

Trade the powerful Traders Dynamic Index strategy automatically with this dedicated TDI Expert Advisor. More details.

Why the maths is a separate header

Calc.mqh holds no terminal state. It takes a SymbolSpec struct - point, tick size, tick value, volume min/max/step, digits - so the whole of the sizing logic can be exercised offline against synthetic instruments.

SpecFromSymbol() is the only bridge to live data. It reads SYMBOL_TRADE_TICK_VALUE_LOSS first, because that is the figure the server uses when a trade goes against you, which is exactly the side a stop loss sits on; it falls back to SYMBOL_TRADE_TICK_VALUE for brokers that leave the loss variant at zero.

That separation is what makes the included test script possible at all.

The test script

RiskLotCalculatorTests.mq5 runs on any chart, with no broker connection and no history. It asserts 41 cases: volume-step rounding, both clamps and their flags, point value scaling when tick size differs from point size, a 5-digit major, gold, an index with a 0.1 lot step, both trade directions, and every rejection path - zero risk, negative risk, a stop sitting on the entry, and missing tick data.

Drag it onto a chart and read the Experts tab. It ends with ALL PASS, passed=41, failed=0.

Both files are self-contained: drop them in the same folder and the test script compiles as-is.

Level up your trading with professional RobotFX expert advisors and indicators. Visit robotfx.org for proven MT4/MT5 tools.

77488

Best MetaTrader Indicators + Profitable Expert Advisors