Watch how to download trading robots for free
Find us on Telegram!
Join our fan page
Interesting script?
So post a link to it -
let others appraise it
You liked the script? Try it in the MetaTrader 5 terminal
Libraries

Risk Lot Calculator - lot size from risk percent and stop distance - library for MetaTrader 5

Lukas Szyry
Lukas Szyry
Gold Trading Specialist | XAUUSD Expert Advisors
I develop rule-based Expert Advisors for gold (XAUUSD) on MetaTrader 5. My focus is on robust systems for long-term account growth: no martingale, no grid, no averaging down.
Views:
19
Published:
MQL5 Freelance Need a robot or indicator based on this code? Order it on Freelance Go to Freelance

I needed this in every EA so here it is as one small include file. It calculates the position size (lot) for a trade from the risk you want to take and the stop loss distance.

You give it entry, stop loss and the risk in % (or a fixed money amount) and it gives you back the lot. It uses tick value and tick size of the symbol, so it works for gold, indices and forex the same way. The lot is always rounded DOWN to the volume step, so the real risk is never higher than what you set. If the lot would be below the minimum lot it returns 0 instead of opening a bigger trade than planned. It also checks the free margin and makes the lot smaller if the margin is not enough.

Demo script output in the Experts journal

Demo script output in the Experts journal

Functions

  • LS_LotByRisk(symbol, type, entry, sl, riskPercent) - lot for x % of the balance
  • LS_LotByMoney(symbol, type, entry, sl, riskMoney) - lot for a fixed money amount
  • LS_NormalizeLot(symbol, lot) - rounds down to the volume step, checks min/max/limit
  • LS_RiskOfLot(symbol, lot, entry, sl) - shows the real money at risk after rounding

Both lot functions have an optional last parameter maxMarginShare (default 0.8 = max 80 % of free margin for this trade).

Usage

#include <LS_RiskLot.mqh>
double lot = LS_LotByRisk(_Symbol, ORDER_TYPE_BUY, entry, sl, 1.0);   // 1 % of balance
if(lot > 0) trade.Buy(lot, _Symbol, entry, sl, tp);

Demo script

The small script LS_RiskLot_Demo shows how to use it: drop it on a chart, set risk and stop in points and it prints the lot and the real risk in the Experts journal. It doesnt open any trade. Inputs: InpRiskPct (risk in % of balance, 0 = use money), InpRiskMoney (risk in account currency), InpStopPts (stop distance in points), InpBuy (buy or sell side).

Small accounts: with 0.01 minimum lot and a wide stop the function can return 0. Thats on purpose, better no trade than 3x the risk.

Closed Trades to CSV - one row per closing deal (partial closes, reversals) Closed Trades to CSV - one row per closing deal (partial closes, reversals)

Exports closed trades to a CSV file, one row per closing deal: partial closes, close-by and netting reversals are split, each row with the position's open time, volume-weighted open price and its share of the entry commission.

XAUUSD Hourly Volatility Clock: typical range by hour vs today XAUUSD Hourly Volatility Clock: typical range by hour vs today

A 24-hour clock of typical H1 ranges for XAUUSD (and any other symbol) versus the range of the current hour. The profile uses closed hours only. Display only — it does not trade.

Accelerator Oscillator (AC) Accelerator Oscillator (AC)

The Acceleration/Deceleration Indicator (AC) measures acceleration and deceleration of the current driving force.

MACD Signals MACD Signals

Indicator edition for new platform.