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 Script | Risk Lot Calculator | MQL5 Code

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

Before every trade, a trader has to answer one question: how many lots can I open so that a stop loss hit costs exactly the risk I chose? This script answers it in one click. Drop it on any chart, enter your risk in percent and your stop distance in points, and it shows the lot size to trade together with the real money at risk after rounding.


Many lot calculators rely on the tick value alone. On some brokers this is wrong for gold, indices, stocks and cross pairs, because of the contract size, the profit currency or the way the broker reports tick values. This script does not guess: it asks the terminal how much a 1.0 lot buy would lose over your stop distance, using the built-in OrderCalcProfit() function. The broker's own rules for contract size, tick value and currency conversion are therefore applied automatically.

Capture price channel breakouts with the Price Channel Expert Advisor. Precise entries and exits. Explore now.


The lot size is always rounded down to the broker's volume step, so the trade never risks more than you asked for. The script then shows the actual risk of the rounded lot, in money and in percent. If even the minimum lot is too big for your risk, it does not suggest a trade: it tells you how much the minimum lot would actually risk, so you can decide yourself.

The result appears in an alert window, in the Experts log and in the top-left corner of the chart:


First line: the symbol, your risk in percent and in account currency, and the stop distance in points.

Second line, "Lot size": the lot to trade, followed by the actual risk of that lot after rounding.

Second line, "Too small": your risk is below what the minimum lot needs; the line shows the risk of the minimum lot instead.


Input parameters:


The stop distance is entered in points, the smallest price step of the symbol, so one input works on every market. On a 5-digit EURUSD quote, 10 points are 1 pip. On a 2-digit gold quote, 100 points are 1.00 USD per ounce, so 500 points are a 5.00 USD stop. On a 3-digit gold quote, the same 5.00 USD stop is 5000 points.


Example: on a 10,000 USD account, XAUUSD quoted with 2 digits, 1% risk and a 500 point stop, one lot of 100 ounces loses 500 USD, so the script shows 0.20 lots with an actual risk of 100 USD. Run the script while the market is open, so the symbol has a current price. The two helper functions, LossPerLot() and RoundLots(), are short and commented and can be reused in your own Expert Advisors.

Input Default Description
RiskPercent 1.0 Risk per trade, in percent of the balance or the equity
StopPoints 500 Stop loss distance in points (the symbol's smallest price step)
UseEquity false true: percent of equity; false: percent of balance

Image for Risk Lot Calculator

Complement community code with advanced automation from RobotFX.

80244

Best MetaTrader Indicators + Profitable Expert Advisors