Code Library / Risk Management

Pine Script Position Calculator

On-chart position size calculator.

Language: Pine Script · Category: Risk Management · Tags: position sizing, calculator, Pine Script, risk management, lot size

Source code

//@version=5
indicator("Pos Size Calc",overlay=true)
acc=input.float(10000,"Account $"); rk=input.float(1,"Risk %%")
sl=input.float(30,"SL Pips")
risk=acc*rk/100; pv=syminfo.pointvalue*10
lot=sl>0?risk/(sl*pv):0
var table t=table.new(position.top_right,2,3)
table.cell(t,0,0,"Risk"); table.cell(t,1,0,"$"+str.tostring(risk,"#.##"))
table.cell(t,0,1,"SL"); table.cell(t,1,1,str.tostring(sl)+" pips")
table.cell(t,0,2,"Lot"); table.cell(t,1,2,str.tostring(lot,"#.##"),text_color=color.green)

How to use this snippet

This is a building block, not a finished trading system. Paste it into your own Pine Script project, compile it, and test it on a demo account with your broker's real spreads before relying on it. You can convert it to MQL4 or MQL5 with the Pine Script converter.

Related snippets