HomePine Script Snippets › Position Size Calculator

Pine Script Position Size Calculator

Calculates contract quantity from risk dollars, stop distance, and tick value so every trade risks the same fixed amount.

//@version=6
strategy("Position Size by Risk", overlay=true, calc_on_every_tick=false)

// ── Inputs ──────────────────────────────────────────────
riskDollars = input.float(100.0, "Risk per Trade ($)", step=25, minval=1)
stopTicks   = input.int(30, "Stop Distance (ticks)", minval=1)
tickValue   = input.float(0.50, "Tick Value ($)", step=0.05, minval=0.01,
     tooltip="MNQ = $0.50, MES = $1.25, NQ = $5.00, ES = $12.50.")

// ── Risk-based quantity ──────────────────────────────────
qty = math.floor(riskDollars / (stopTicks * tickValue))
qty := math.max(qty, 1)

// ── Entry logic (replace with your own conditions) ───────
longCondition = ta.crossover(ta.ema(close, 9), ta.ema(close, 21))
if longCondition and strategy.position_size == 0
    strategy.entry("Long", strategy.long, qty=qty)
    strategy.exit("Long Exit", "Long", loss=stopTicks, profit=stopTicks * 2)

// ── Readout ──────────────────────────────────────────────
var table t = table.new(position.top_right, 1, 1)
if barstate.islast
    table.cell(t, 0, 0, "Qty: " + str.tostring(qty) + " contracts",
     bgcolor=color.new(color.blue, 80), text_color=color.white)

Settings

How to use

Frequently Asked Questions

How do I calculate position size in Pine Script?

Divide your per-trade risk in dollars by the dollar risk per contract: qty = floor(riskDollars / (stopTicks x tickValue)). Risking $100 with a 30-tick stop on MNQ ($0.50 per tick) gives floor(100 / 15) = 6 contracts. Pass the result to strategy.entry via the qty parameter.

How do I risk a fixed dollar amount per trade?

Fix riskDollars as an input and recompute quantity from the current stop distance before every entry. When the stop is wide you trade fewer contracts, when it is tight you trade more, and the dollar loss on a full stop-out stays constant — which is exactly what trailing-drawdown prop accounts need.

Related

Want a complete eval-ready strategy with sizing built in?

This snippet is a building block. Our paid scripts are complete strategies — entries, risk-based sizing, kill switch, session filter, and EOD flatten tested on 3 years of MES/NQ data. Instant delivery from $50.

View Plans — From $50