StructureX

Home › Guides › Platforms

Pine Script position size: work out your size from the stop

Your stop comes from the chart. Your size comes from the math. Pine Script can do that math for you, live, and show the answer in the corner of the chart.

Beginner guide · 9-minute read · Updated 2026-10-21

The rule behind the code

We size every trade the same way:

  1. Pick how much of the account to risk. Around 1% is a common start.
  2. Put the stop where the chart says the idea is wrong.
  3. Size = the money you're willing to risk ÷ the loss per share (or contract) if the stop is hit.

A wide stop gives a small size. A tight stop gives a bigger one. Either way, a loss costs about the same slice of the account. More in how much to risk per trade.

Doing that by hand on every chart is slow. A small script can do it on every candle. New to Pine Script? Start with Pine Script for beginners.

A worked example first

Account: 10,000. Risk: 1%, so 100.

A stock closes at 50.00. The stop goes under the recent low, at 48.00. That's 2.00 of risk per share.

Size = 100 ÷ 2.00 = 50 shares. If the stop is hit, the loss is about 100, or 1 unit of risk, plus costs and any Slippage.

A math example, not a promise. Losses are always possible, and price can jump past a stop.

The script

This indicator puts the stop under the lowest low of the last 10 candles, then shows the size in a small table:

//@version=6
indicator("Position size helper", overlay=true)
account = input.float(10000, "Account size", minval=1)
riskPct = input.float(1.0, "Risk per trade (%)", minval=0.1, maxval=5, step=0.1)
lookback = input.int(10, "Stop: lowest low of the last N candles", minval=1)
stopPrice = ta.lowest(low, lookback)
riskAmount = account * riskPct / 100
stopDistance = close - stopPrice
riskPerUnit = stopDistance * syminfo.pointvalue
size = riskPerUnit > 0 ? math.floor(riskAmount / riskPerUnit) : 0
plot(stopPrice, "Stop", color=color.red, style=plot.style_stepline)
var table box = table.new(position.top_right, 2, 3, bgcolor=color.new(color.black, 15), border_width=1)
if barstate.islast
    table.cell(box, 0, 0, "Risk", text_color=color.white)
    table.cell(box, 1, 0, str.tostring(riskAmount, "#.##"), text_color=color.white)
    table.cell(box, 0, 1, "Stop distance", text_color=color.white)
    table.cell(box, 1, 1, str.tostring(stopDistance, format.mintick), text_color=color.white)
    table.cell(box, 0, 2, "Size", text_color=color.white)
    table.cell(box, 1, 2, str.tostring(size), text_color=color.yellow)

Every line, in plain words

What syminfo.pointvalue does

Prices move in points. But a point is worth different amounts on different things.

So line 9 works for both. On ES, a 4-point stop is 4 × 50 = 200 per contract. With 100 to risk, the size rounds down to 0. That's the script telling you the stop is too wide for that contract. The micro might fit: 4 × 5 = 20, so 5 MES contracts.

Check the contract specs at cmegroup.com and try our futures tick value calculator.

A forex caution

On forex charts, the point value and its currency depend on how your data feed lists the pair. The result may be in the quote currency, not your account's. Check your numbers against our position size calculator before trusting them. Brokers also differ on lot sizes.

The live-candle catch

The size uses the close. On a live candle, the close is just the latest price. So the size changes every time price moves, until the candle closes.

That's fine for a rough number. For the number you actually use, plug in your real entry price and stop into the position size calculator.

Also, ta.lowest() only looks at past candles here. It doesn't peek ahead. A stop from "the lowest low" is a simple rule, not the only right one. Your plan decides where the stop goes.

Make it fit a sell

For a sell (a short), the stop sits above price. Two lines change:

stopPrice = ta.highest(high, lookback)
stopDistance = stopPrice - close

The code sizes the trade. It doesn't pick it. Whether there's a trade at all depends on the bigger chart, a clear level and your pre-trade checklist.

Try it today

  1. Paste the script into the Pine Editor on a computer. Save and add it to a stock's daily chart.
  2. Set your own account size and 1% risk.
  3. Change the lookback from 10 to 20. Watch the stop drop and the size shrink.
  4. Now open an ES chart, then an MES chart. Compare the sizes. That's pointvalue at work.

Lower risk while you learn. Many people practice at 0.5% or less, or on paper first. The math is the same at any %.

Learning from a real script

Our course includes the full source code of the StructureX chart tool to learn from and customize, for example adding your own size box beside its levels. It's source code, not a ready-made trading bot.

Common mistakes

Questions

How do I calculate position size in Pine Script?

Divide the money you'll risk by the loss per share or contract at your stop: (account × risk %) ÷ (stop distance × syminfo.pointvalue). Round down with math.floor.

What is syminfo.pointvalue in Pine Script?

The money value of a 1-point move for one share or contract of the chart's symbol. It's 1 for most stocks and 50 for the ES future.

Can Pine Script size trades in a strategy?

Yes. Pass the size to strategy.entry() with qty=. The strategy backtest guide shows an example that sizes from the stop.

Why does my position size show 0?

The stop is too wide for that symbol and your risk amount, so even 1 share or contract would risk more than you set. Try a smaller contract or a different stop.

Want the next step? We'll walk you through it.

Four free lessons by email, one every few days — each with a 10-minute exercise on a real chart.

Send Me the Free Lessons 4 short emails · No spam · Unsubscribe anytime

Or see the whole method, step by step:

See What’s Inside the Course Try everything free first. Decide when you're ready. · Open the free chart tool (works in the TradingView phone app)

One chart idea a day, in plain English. Follow along:

Follow on XFollow on TradingView