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 | Risk Lot Sizing Library - four risk bases, tick-value scaling, margin cap, daily l…

RobotFX curates the best open-source MetaTrader code to inspire your trading automation.

Image for Risk Lot Sizing Library - four risk bases, tick-value scaling, margin cap, daily limit

Position sizing is the arithmetic that turns a stop distance and an account balance into the cash amount a trade is allowed to lose. The library does it in one static call: risk money over what one lot loses at your stop, floored to the volume step, capped against free margin.

The four risk bases

  • RISK_FIXED_LOT - the risk value is a lot size; the stop is not read.
  • RISK_PERCENT_BALANCE - a percentage of the account balance.
  • RISK_PERCENT_EQUITY - a percentage of the account equity.
  • RISK_FIXED_AMOUNT - a cash amount, used as the money to risk.

How the lot is produced

Trade Renko charts automatically using the advanced Renko Expert Advisors. Clean trends, better entries. Discover them here.

  • The two percent modes multiply the account value by the risk divided by one hundred; the fixed-amount mode uses the value unchanged.
  • Stop points times point, over tick size, times tick value, gives the loss of one full lot.
  • Risk money over that loss gives the raw lot.
  • The normaliser floors to the volume step, clamps to the symbol's minimum and maximum, and takes decimals from the step.
  • The margin cap measures a buy at the ask and cuts the lot to fit 70 per cent of free margin.

The daily loss base

  • The base is the balance at the first call, printed to the Experts log, and reset when the calendar day of TimeCurrent changes.
  • The allowed loss is the base times the percentage; equity down by that much is a breach, and it prints an alert line to the Experts log. A base or a percentage of zero or less reports no breach.
  • A balance base against an equity reading means floating profit and loss move the number.
  • The loss and profit getters share that base and equity, and neither returns a negative.

What you supply

  • The symbol, which fixes the volume step, the lot limits, the tick size, the tick value and the margin test.
  • The mode, which decides what the risk value means: lots, a percentage or a cash amount.
  • The stop distance in points, from your own prices; it never reads your order.

Where it stops

  • A stop or risk of zero or less, an invalid tick size, tick value or point, or a zero money per lot returns the minimum lot.
  • That return is silent, and a result below the minimum is raised to it, so an unaffordable risk is exceeded, not refused.
  • The base is memory only: no file, no global variable, so a restart or re-init resets it to that moment's balance.
  • The day turns over on the calendar day of TimeCurrent, which is server time, not an hour you set.
  • The margin cap is per call, on the buy side, absent in fixed-lot mode, and skipped when the margin call fails.
  • It does not compute the stop, choose the symbol or convert currencies; it uses what you pass and the reported tick value.

Good to know

  • Both files are needed: RiskManager.mqh includes Common.mqh by a quoted include, so keep them in one folder under MQL5/Include.
  • The class is version 1.10; Common.mqh carries the four-mode enum and the lot normaliser.
  • No DLL, no trade call: it returns a lot size and a bool and places nothing.

Complement community code with advanced automation from RobotFX.

78992

Best MetaTrader Indicators + Profitable Expert Advisors