Risk Based Lot Sizing
The lot-sizing formula most EAs use multiplies stop-in-points by Point and calls that "money per point per lot". It is wrong on anything that is not a plain 4/5-digit FX pair. A JPY cross quotes with two or three digits instead of four or five, so a naive Point-based formula misprices the stop by a factor of a hundred. Gold has its own contract size and its quote currency is not always the account currency. Indices have no fixed relationship between a point move and a currency amount at all - it depends entirely on the instrument's contract specification. The value that is actually correct for every instrument is the broker's own published tick value (MODE_TICKVALUE on MQL4, SYMBOL_TRADE_TICK_VALUE on MQL5), because this relationship cannot be derived from Point alone - the broker has already done the JPY conversion, the gold contract math, and the index point value for you. This header builds money-per-point from tick value and tick size together, because tick size and Point are not guaranteed to be the same value either. RiskLotsForBudget is deliberately a floor, never a round or a ceiling. Rounding up a risk-derived size spends more of the budget than was authorised, and it does so quietly - the position looks fine until the stop is hit and the loss is larger than the settings implied. When even the broker's minimum lot would cost more than the budget allows, the function returns 0.0. That zero is a refusal, not a size: the correct response is to skip the trade or widen the budget, never to substitute the minimum lot in its place. This header does not include LotNormalize.mqh (or its MQL5 counterpart). Per this catalogue's rule, every library must work as a standalone download, so the floor-rounding logic here is a second, independent copy of the same idea rather than a dependency on another submission - duplication that is a deliberate trade against a shared include that would break the moment only one of the two libraries is downloaded. The header is self-contained. No dependencies, no DLLs, no classes - plain functions callable from a script, an indicator or an EA, on either platform. Notes and limitations - Broker property values come from the server. On a symbol the terminal has never synchronised, the tick value can read as 0 - RiskValuePerPoint returns 0.0 rather than guess, and RiskLotsForBudget then correctly refuses rather than producing a silently wrong size. Call RiskSizingConstraintsToString at startup and read it. - This header sizes a position; it does not check margin. A volume can be perfectly sized for the risk budget and still be refused for insufficient free margin - check that separately (OrderCalcMargin on MQL5). - Stop distance must already be in points, not pips. A 5-digit broker's pip is 10 points; convert before calling if your inputs are in pips. - The maximum returned is a per-order limit from the broker's published maximum volume. Brokers frequently also impose a total volume limit per symbol that this property does not expose, on either platform. - No thread or state concerns: every function is pure and reads only broker properties.
Script · MT5 · v1.0 · Free
About Risk Based Lot Sizing
Risk Based Lot Sizing is a script for MetaTrader 5. It has 2 settings, such as Risk amount and Stop points. MQL source code included.
Files in the download: RiskSizing.mq5.mqh, RiskSizing.txt, RiskSizingDemo.ex5, RiskSizingDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Risk amount | 100.0 | Risk budget in account currency for the worked example |
| Stop points | 300.0 | Stop distance in points for the worked example |
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: Gold (XAUUSD) · Risk & Lot Size · MT5 Script · MQL5 Source Code