Pine Script Position Size Calculator
Converts a fixed dollar risk, a stop distance, and a tick value into a contract count, so every trade risks the same amount regardless of stop width.
Pine Script
//@version=6
strategy("Target Filled - Position Size Calculator Example", overlay=true, calc_on_every_tick=false)
// --- Inputs ---------------------------------------------------
riskBudget = input.float(100.0, "Risk Per Trade ($)", step=25, minval=1)
stopTicks = input.int(30, "Stop Distance (ticks)", minval=1)
tickDollar = 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 — or pull it live with the tick value helper snippet.")
// --- Risk-based contract count ------------------------------------
rawQty = riskBudget / (stopTicks * tickDollar)
qty = math.max(math.floor(rawQty), 1)
// --- Sample signal — replace with your own entry logic ---------
if ta.crossover(ta.ema(close, 9), ta.ema(close, 21)) 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 sizer = table.new(position.top_right, 1, 1)
if barstate.islast
table.cell(sizer, 0, 0, "Qty: " + str.tostring(qty) + " contracts",
bgcolor=color.new(color.blue, 80), text_color=color.white) Settings
| Input | Default | Purpose |
|---|---|---|
| riskBudget | $100 | Fixed dollar risk per trade. |
| stopTicks | 30 | Stop distance in ticks. |
| tickDollar | $0.50 | Dollar value of one tick for the traded symbol. |
How to use
- Paste into the Pine Editor and swap the EMA-crossover placeholder for your own entry conditions.
- Set
tickDollarfor whatever contract you're trading, 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 the risk budget. - Cap the result with the max contract limit snippet so sizing never exceeds your firm's scaling plan.
Common tick values
| Symbol | Tick Size | Tick Value |
|---|---|---|
| MNQ | 0.25 pts | $0.50 |
| MES | 0.25 pts | $1.25 |
| NQ | 0.25 pts | $5.00 |
| ES | 0.25 pts | $12.50 |
Frequently Asked Questions
How do I calculate position size in Pine Script?
Divide the dollar risk you're willing to take by the dollar risk of a single contract at your stop distance:
qty = floor(riskDollars / (stopTicks × tickValue)). Risking $100 with a 30-tick stop on MNQ ($0.50 per tick) gives floor(100 / 15) = 6 contracts. Pass the result straight into strategy.entry via qty=.How do I risk a fixed dollar amount per trade?
Fix the dollar risk as an input and recompute quantity from the live stop distance ahead of every entry. A wider stop trades fewer contracts, a tighter one trades more, and the dollar loss on a full stop-out stays constant either way — exactly what a trailing-drawdown prop account needs.