Risk-to-Lot Calculator

Risk-to-Lot Calculator is a free indicator by DinexSpace for MT5 and TradingView. Works out position size from the amount you are willing to lose and your stop-loss level. The MT5 version reads the symbol's real contract specifications and never places an order.

Download .zipDetailed installation guide36.1 KB · 8 files · no sign-up

What Risk-to-Lot Calculator does

Risk-to-Lot Calculator is a free indicator by DinexSpace for MT5 and TradingView. Works out position size from the amount you are willing to lose and your stop-loss level. The MT5 version reads the symbol's real contract specifications and never places an order.

Risk-to-Lot Calculator on an XAUUSD chart in TradingView
Risk-to-Lot Calculator on an XAUUSD chart in TradingView

MT5 version

Risk-to-Lot Calculator turns "how much am I willing to lose on this trade" into a lot size. It takes your contract data straight from the broker, so it works the same on forex, gold, indices and crypto CFDs.

TradingView version

Risk-to-Lot Calculator answers one question before every trade: how big should my position be so that hitting the stop loss costs exactly what I planned to risk? A table shows the lot size; entry, stop-loss and take-profit levels are drawn on the chart.

Features and modes

Risk-to-Lot Calculator has 11 documented features, listed below.

MT5 version

How it works

  • Risk per trade is a percent of your balance (or equity, or a manual amount) or a fixed amount in the account currency.
  • The stop loss can be a price, a distance in points or a multiple of ATR.
  • The loss for 1.00 lot at the stop is the stop distance divided by the tick size, times the symbol's tick value (loss side). This includes the conversion to your account currency done by the terminal.
  • The lot size is rounded down to the broker's lot step, so the money at risk is never above your target. The panel shows the risk after rounding.
  • It also shows the take-profit for a chosen R multiple and the margin needed, calculated by the terminal for the current account.
  • Entry, stop-loss and take-profit lines are drawn on the chart.

TradingView version

How it works

  • Risk = balance × risk % (or a fixed amount).
  • Loss per 1.00 lot = stop distance × contract size, converted to your account currency.
  • Lot size = risk ÷ loss per lot, rounded down to your broker's lot step, so the real risk never exceeds your plan. The table shows the real risk at the rounded lot.
  • Optional take profit at an R multiple (e.g. 2R = twice the stop distance), with the money gained.
  • Position value and the margin needed at your leverage.

Warnings shown in the table

  • The stop is on the wrong side of the entry.
  • The calculated size is below the minimum lot, so the minimum lot risks more than planned.
  • No exchange rate is available for the chosen account currency.

Parameters

Risk-to-Lot Calculator has 14 inputs in the MT5 version and 18 in the TradingView version; every input and its default is listed below.

MT5 version

  • Account: balance source, manual balance, risk mode and value.
  • Trade: direction, entry (0 = current price), stop-loss mode, price, points, ATR length and multiple, R multiple.
  • Display: lines on/off, panel corner.

TradingView version

  • Account: balance, account currency, risk mode (% or fixed), leverage.
  • Trade: direction, entry (last price or manual), stop from a price, a distance or an ATR multiple, R multiple for the target.
  • Contract: contract size per lot (Auto: forex 100,000, gold 100 oz, silver 5,000 oz, other 1 unit, or Manual), lot step, minimum lot.
  • Display: level lines on/off, table position.
Risk-to-Lot Calculator inputs – MT5 version
InputDefaultLabel in MT5
Account
InpBalModeBAL_ACCOUNTBalance used
InpBalance10000Manual balance (account currency)
InpRiskModeRISK_PERCENTRisk per trade
InpRisk1.0Risk value (% or amount)
Trade
InpDirDIR_LONGDirection
InpEntry0Entry price (0 = current price)
InpSlModeSL_ATRStop loss from
InpSlPrice0Stop-loss price
InpSlPts500Stop distance (points)
InpAtrLen14ATR length
InpAtrMult1.5ATR multiple
InpRR2.0Take profit at R multiple (0 = off)
Display
InpLinestrueEntry / SL / TP lines
InpCornerCORNER_RIGHT_UPPERPanel corner
Risk-to-Lot Calculator inputs – TradingView version
InputDefault
Account
Balance10000.0
Account currency"Quote currency"
Risk per trade"% of balance"
Leverage (for margin)100.0
Trade
Direction"Long"
Entry = last pricetrue
or entry price0.0
Stop loss from"ATR multiple"
Stop-loss price0.0
Stop distance (in price)5.0
ATR length14
x1.5
Take profit at R multiple (0 = off)2.0
Contract
Contract size per 1.00 lot"Auto"
Lot step0.01
Min lot0.01
Display
Entry / SL / TP linestrue
Table position"Top right"

Signals

Risk-to-Lot Calculator does not generate buy or sell signals. It is an analysis tool: you read what it plots and make your own decisions.

Alerts

No. Risk-to-Lot Calculator has no built-in alerts.

Buffers for Expert Advisors

No. Risk-to-Lot Calculator draws chart objects (lines, boxes, labels or a panel) and has no indicator buffers for EAs.

Timeframes and markets

Risk-to-Lot Calculator runs on any chart timeframe. Its calculation reads the chart timeframe or a fixed timeframe (for example D1 or a higher timeframe), as described under Features.

Limitations

Risk-to-Lot Calculator is an analysis tool, not a trading system. Its documented limitations are listed below.

MT5 version

  • If the lot is below the broker's minimum, the panel says so instead of rounding up.
  • Tick value can change with exchange rates, and the real fill price can differ from the entry used here.
  • Margin comes from the terminal's own calculation for the current account and may differ on other accounts.

The indicator only calculates and draws. It never places, changes or closes orders, and uses no DLLs or web requests.

TradingView version

  • Contract sizes differ between brokers. Always check your broker's contract specification and use Manual if it differs.
  • Currency conversion uses TradingView's rates, which can differ slightly from your broker's. Commissions, swaps and slippage are not included.
  • The script does not place orders.

This is a calculation aid, not investment advice.

Pine Script code

Paste the whole script into the Pine Editor on TradingView.

risk_to_lot_calculator.pine · 154 lines
//@version=6
// DinexSpace | Risk-to-Lot Calculator
// Turns "how much am I willing to lose" into a position size. Enter your balance, the risk per trade
// (% or fixed amount) and a stop loss (price, distance or ATR multiple); the table shows the lot size,
// the money at risk after rounding to the broker's lot step, the take-profit for a chosen R multiple
// and the margin needed. Entry, stop and target are drawn on the chart.
// Free and open source. License: MIT. (c) 2026 DinexSpace
indicator("DinexSpace | Risk-to-Lot Calculator", shorttitle = "DNX Risk-Lot", overlay = true, max_lines_count = 10, max_labels_count = 10)

// ===== Brand colours =====
C_GREEN = #00FF7F   // SpringGreen: profit side
C_AQUA  = #00FFFF   // Aqua: loss side
C_AMBER = #FFB000   // warnings

// ===== Inputs: account =====
GRP_A   = "Account"
balance = input.float(10000.0, "Balance", minval = 0.0, group = GRP_A)
acctCcy = input.string("Quote currency", "Account currency", options = ["Quote currency", "USD", "EUR", "GBP", "JPY", "AUD", "CAD", "CHF", "NZD"], group = GRP_A, tooltip = "Quote currency = the second currency of the symbol (USD for XAUUSD, USDT for BTCUSDT). Other choices convert the result with TradingView's currency rates.")
riskMode = input.string("% of balance", "Risk per trade", options = ["% of balance", "Fixed amount"], inline = "r", group = GRP_A)
riskVal = input.float(1.0, "", minval = 0.0, step = 0.1, inline = "r", group = GRP_A)
leverage = input.float(100.0, "Leverage (for margin)", minval = 1.0, group = GRP_A)

// ===== Inputs: trade =====
GRP_T   = "Trade"
dir     = input.string("Long", "Direction", options = ["Long", "Short"], group = GRP_T)
useLast = input.bool(true, "Entry = last price", inline = "e", group = GRP_T)
entryIn = input.float(0.0, "or entry price", minval = 0.0, inline = "e", group = GRP_T)
slMode  = input.string("ATR multiple", "Stop loss from", options = ["Price", "Distance", "ATR multiple"], group = GRP_T)
slPrice = input.float(0.0, "Stop-loss price", minval = 0.0, group = GRP_T)
slDist  = input.float(5.0, "Stop distance (in price)", minval = 0.0, group = GRP_T)
atrLen  = input.int(14, "ATR length", minval = 1, inline = "atr", group = GRP_T)
atrMult = input.float(1.5, "x", minval = 0.0, step = 0.1, inline = "atr", group = GRP_T)
rr      = input.float(2.0, "Take profit at R multiple (0 = off)", minval = 0.0, step = 0.5, group = GRP_T)

// ===== Inputs: contract =====
GRP_C    = "Contract"
csMode   = input.string("Auto", "Contract size per 1.00 lot", options = ["Auto", "Manual"], inline = "cs", group = GRP_C, tooltip = "Auto: forex 100,000 units, gold (XAU) 100 oz, silver (XAG) 5,000 oz, everything else 1 unit. Check your broker's contract specification and use Manual if it differs.")
csManual = input.float(100.0, "", minval = 0.0, inline = "cs", group = GRP_C)
lotStep  = input.float(0.01, "Lot step", minval = 0.0, step = 0.01, inline = "lot", group = GRP_C)
minLot   = input.float(0.01, "Min lot", minval = 0.0, step = 0.01, inline = "lot", group = GRP_C)

// ===== Inputs: display =====
GRP_D    = "Display"
showLine = input.bool(true, "Entry / SL / TP lines", group = GRP_D)
tblPos   = input.string("Top right", "Table position", options = ["Top right", "Top left", "Bottom right", "Bottom left"], group = GRP_D)

// ===== Contract size =====
bool isForex = syminfo.type == "forex"
float autoCs = isForex ? 100000.0 : str.contains(syminfo.ticker, "XAU") ? 100.0 : str.contains(syminfo.ticker, "XAG") ? 5000.0 : 1.0
float contract = csMode == "Manual" ? csManual : autoCs

// ===== Conversion from the symbol's quote currency to the account currency =====
string toCcy = acctCcy == "Quote currency" ? syminfo.currency : acctCcy
float fxRaw = request.currency_rate(syminfo.currency, toCcy, ignore_invalid_currency = true)
float fx = toCcy == syminfo.currency ? 1.0 : fxRaw

// ===== Prices =====
float atr   = ta.atr(atrLen)
bool isLong = dir == "Long"
float entry = useLast or entryIn <= 0 ? close : entryIn
float dist  = switch slMode
    "Price"    => math.abs(entry - slPrice)
    "Distance" => slDist
    => atrMult * atr
float sl = slMode == "Price" ? slPrice : isLong ? entry - dist : entry + dist
float tp = rr > 0 ? (isLong ? entry + rr * dist : entry - rr * dist) : na
bool wrongSide = slMode == "Price" and (isLong ? slPrice >= entry : slPrice <= entry)

// ===== Size =====
float riskMoney  = riskMode == "% of balance" ? balance * riskVal / 100.0 : riskVal
float lossPerLot = dist * contract * fx                    // account currency lost per 1.00 lot at the stop
float rawLot     = lossPerLot > 0 ? riskMoney / lossPerLot : na
float lot        = lotStep > 0 ? math.floor(rawLot / lotStep + 1e-9) * lotStep : rawLot
bool belowMin    = not na(lot) and lot < minLot
float useLot     = belowMin ? minLot : lot
float realRisk   = useLot * lossPerLot
float reward     = rr > 0 ? useLot * lossPerLot * rr : na
float notional   = useLot * contract * entry * fx
float margin     = notional / leverage

// ===== Formatting helpers =====
money(float v) => na(v) ? "-" : str.tostring(v, "#,##0.00") + " " + toCcy
px(float v) => na(v) ? "-" : str.tostring(v, format.mintick)
distText(float d) =>
    string t = px(d)
    isForex ? t + "  (" + str.tostring(d / syminfo.mintick / 10.0, "#.#") + " pips)" : t + "  (" + str.tostring(math.round(d / syminfo.mintick)) + " ticks)"

// ===== Table =====
tPos = switch tblPos
    "Top left"     => position.top_left
    "Bottom right" => position.bottom_right
    "Bottom left"  => position.bottom_left
    => position.top_right

var table tbl = table.new(tPos, 2, 13, bgcolor = color.new(color.black, 5), border_color = color.new(color.gray, 70), border_width = 1)

cell(int r, string k, string v, color vc) =>
    table.cell(tbl, 0, r, k, text_color = color.gray, text_size = size.small, text_halign = text.align_left)
    table.cell(tbl, 1, r, v, text_color = vc, text_size = size.small, text_halign = text.align_right)

if barstate.islast
    table.cell(tbl, 0, 0, "Risk-to-Lot", text_color = C_GREEN, text_size = size.normal, text_halign = text.align_left)
    table.cell(tbl, 1, 0, syminfo.ticker + " · " + dir, text_color = isLong ? C_GREEN : C_AQUA, text_size = size.small, text_halign = text.align_right)
    cell(1, "Balance", money(balance), color.white)
    cell(2, "Risk", money(riskMoney) + (riskMode == "% of balance" ? "  (" + str.tostring(riskVal, "#.##") + "%)" : ""), color.white)
    cell(3, "Entry", px(entry), color.white)
    cell(4, "Stop loss", px(sl), C_AQUA)
    cell(5, "Stop distance", distText(dist), color.white)
    cell(6, "Lot size", na(useLot) ? "-" : str.tostring(useLot, "#.####"), belowMin ? C_AMBER : C_GREEN)
    cell(7, "Risk at this lot", money(realRisk) + (balance > 0 ? "  (" + str.tostring(realRisk / balance * 100.0, "#.##") + "%)" : ""), belowMin ? C_AMBER : color.white)
    cell(8, "Take profit (" + str.tostring(rr, "#.#") + "R)", rr > 0 ? px(tp) + "  +" + money(reward) : "off", C_GREEN)
    cell(9, "Position value", money(notional), color.white)
    cell(10, "Margin (1:" + str.tostring(leverage, "#") + ")", money(margin), color.white)
    cell(11, "Contract / lot", str.tostring(contract, "#,##0.##") + (csMode == "Auto" ? " (auto)" : ""), color.gray)
    string note = wrongSide ? "Stop is on the wrong side of entry" : belowMin ? "Below min lot: min lot risks more" : na(fx) ? "No currency rate for " + toCcy : "DinexSpace"
    table.cell(tbl, 0, 12, note, text_color = note == "DinexSpace" ? C_GREEN : C_AMBER, text_size = size.tiny, text_halign = text.align_left)

// ===== Lines on the chart =====
var line  lE = na
var line  lS = na
var line  lT = na
var label bE = na
var label bS = na
var label bT = na

level(line ln, label lb, float y, color c, string txt) =>
    line l = ln
    label b = lb
    if na(y)
        line.delete(l)
        label.delete(b)
        l := na
        b := na
    else
        if na(l)
            l := line.new(bar_index, y, bar_index + 1, y, extend = extend.right, color = c, width = 1)
            b := label.new(bar_index, y, txt, color = color(na), textcolor = c, style = label.style_label_lower_left, size = size.small)
        l.set_xy1(bar_index - 20, y)
        l.set_xy2(bar_index, y)
        // Label sits at the left end of the line so the price scale never hides it.
        b.set_xy(bar_index - 20, y)
        b.set_text(txt)
    [l, b]

if barstate.islast and showLine
    [l1, b1] = level(lE, bE, entry, color.silver, "Entry " + px(entry))
    [l2, b2] = level(lS, bS, sl, C_AQUA, "SL " + px(sl) + "  -" + money(realRisk))
    [l3, b3] = level(lT, bT, tp, C_GREEN, "TP " + px(tp) + "  +" + money(reward))
    lE := l1
    bE := b1
    lS := l2
    bS := b2
    lT := l3
    bT := b3

Risk-to-Lot Calculator FAQ

Risk-to-Lot Calculator is free, runs on MT5 and TradingView and does not place trades.

Is Risk-to-Lot Calculator free?

Yes. Risk-to-Lot Calculator is free to download from this page with no sign-up, and the download includes the full source code.

Which platforms does Risk-to-Lot Calculator run on?

Risk-to-Lot Calculator is available for MetaTrader 5 (MQL5) and TradingView (Pine Script v6).

Does Risk-to-Lot Calculator give buy and sell signals?

Risk-to-Lot Calculator does not generate buy or sell signals. It is an analysis tool: you read what it plots and make your own decisions.

Does Risk-to-Lot Calculator have alerts?

No. Risk-to-Lot Calculator has no built-in alerts.

Can an EA read Risk-to-Lot Calculator?

No. Risk-to-Lot Calculator draws chart objects (lines, boxes, labels or a panel) and has no indicator buffers for EAs.

Which timeframes does Risk-to-Lot Calculator work on?

Risk-to-Lot Calculator runs on any chart timeframe. Its calculation reads the chart timeframe or a fixed timeframe (for example D1 or a higher timeframe), as described under Features.

Does Risk-to-Lot Calculator place trades?

No. It is a read-only indicator: it only reads market data and shows the result on the chart or writes it to a file. The source contains no calls that open, close or modify orders.

How do I install Risk-to-Lot Calculator?

Download the .zip and follow the step-by-step installation guide for MT5 and TradingView: https://indicators.dinexspace.com/install-guide/

Measured win rates (backtest)

Arrows and win rates are those of SuperTrend (the default source indicator). SuperTrend

  • Costs: Real spread of an ECN-type MT5 account, no commission.
  • Data source: MT5 tick data (BNB: Binance spot).

TradingView H4/D1 bars may follow the exchange time zone of the TradingView symbol, not the MT5 server time (UTC) used for the measurement; sessions and the H4 trend may differ on the Pine version. Same signals on TradingView not checked yet.

Past test statistics are not a promise of future results.

More in this category

All Risk Management indicators

ATR Stop Levels

Risk Management · Indicator

Plots reference stop-loss levels at ATR multiples above and below price. Use it to size stops by volatility instead of a fixed number of pips.

FreeMT5TradingViewMeasured

Correlation Matrix

Risk Management · Indicator

A table of correlation coefficients between the symbols you choose. Use it to avoid stacking positions that are really the same bet.

FreeMT5TradingViewMeasured

Drawdown Monitor

Risk Management · Indicator

Tracks drawdown from the peak: price drawdown on TradingView, account drawdown on MT5. Use it to know when to cut your position size.

FreeMT5TradingViewFilter tool

Lot Size Script

Risk Management · MT5 Script

An MT5 script that calculates lot size from your risk percentage and stop distance. Use it before every trade so you never risk more than you planned.

FreeMT5Uses SuperTrend