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.
The rule behind the code
We size every trade the same way:
- Pick how much of the account to risk. Around 1% is a common start.
- Put the stop where the chart says the idea is wrong.
- 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
- Line 1 says this is Pine Script version 6.
- Line 2 names the script and puts it on the price chart.
- Line 3 is your account size, as a setting.
- Line 4 is the % you'll risk. maxval=5 stops a typo like 50%.
- Line 5 is how many candles back to look for the stop.
- Line 6 finds the lowest low of those candles. That's the stop, for a buy.
- Line 7 turns the % into money: 1% of 10,000 is 100.
- Line 8 is the distance from the close down to the stop.
- Line 9 turns that distance into money per share or contract. More on syminfo.pointvalue below.
- Line 10 divides, then rounds down to a whole number with math.floor. If the distance is zero or less, the size is 0. Never divide by zero.
- Line 11 draws the stop as a red step line.
- Line 12 makes a 2-column, 3-row table. Tables are covered in Pine Script tables.
- Lines 13 to 19 fill it on the newest candle: the risk, the stop distance, and the size in yellow.
What syminfo.pointvalue does
Prices move in points. But a point is worth different amounts on different things.
- On most stocks, 1 point = 1 dollar per share. pointvalue is 1.
- On an ES future (E-mini S&P 500), 1 point = 50 dollars per contract. pointvalue is 50.
- On an MES (the micro), 1 point = 5 dollars. pointvalue is 5.
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
- ta.highest finds the highest high of the last N candles.
- The distance flips, so it's still a positive number.
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
- Paste the script into the Pine Editor on a computer. Save and add it to a stock's daily chart.
- Set your own account size and 1% risk.
- Change the lookback from 10 to 20. Watch the stop drop and the size shrink.
- 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
- Rounding up. Always round down. Rounding up risks more than you planned.
- Forgetting pointvalue. On futures, the size comes out 50 times too big without it.
- Dividing by zero. Guard against a stop distance of 0.
- Trusting the live number. It moves until the candle closes.
- Ignoring costs. Commissions and slippage add to every loss.
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 anytimeOr 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: