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
riskDollars— fixed dollar risk per trade. Default $100.stopTicks— stop distance in ticks. Default 30.tickValue— dollar value of one tick. Default 0.50 (MNQ). MES = 1.25, ES = 12.50, NQ = 5.00.
How to use
- Paste into the Pine Editor and replace the EMA-crossover placeholder with your own entry conditions.
- Set
tickValuefor your contract — or pull it automatically with the tick value helper snippet. - The
math.max(qty, 1)guard prevents a zero-contract order when the stop is wide relative to risk. - Cap the result with the max contract limit snippet so sizing never exceeds your firm’s scaling plan.
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