Page 1 of 1

position size calculation using prices instead of pips(points)

Posted: Sat Nov 19, 2022 4:21 pm
by zwp
Hi Traders,
finishing Mastery course I came to the lesson about calculating position size. As for beginner calculating pips to points, risks etc seems to be fairly complex, I was wondering, if position size calculator can work with "just prices" to simplify the code. I compared this code with the pip-code and I am getting exactly the same results.

Can somebody please flag any benefits of working with pips instead of prices?


FYI: for the simplicity this training script assumes that account currency is the same as quote currency.
Appreciate your thoughts

Here is my solution:

Code: Select all

/

//@version=5
indicator("[zwp] position calculator", overlay=true)

//get user input
equity = input.int(10000, 'equity')
risk   = input.float(1.0, 'risk %', step=0.05)

//get variables
atr = ta.atr(14)
var float t_stop = na
var float t_profit = na
var int inTrade = 0
var int positionSize = na

//import library
import adamchmiel/zwp_entry_patterns/9 as zwp

//get entry values
bullEC = zwp.isBullEC(0.0)
bearEC = zwp.isBearEC(0.0)

//calculate stop loss
longSL = low - atr
longSLdistance = close - longSL
longPT = close + longSLdistance

shortSL = high + atr
shortSLdistance = shortSL - close
shortPT = close - shortSLdistance

//entry signal
longBO = bullEC and inTrade == 0 and not na(atr)
shortBO = bearEC and inTrade == 0 and not na(atr)

if longBO or shortBO
    t_stop := longBO ? longSL : shortSL
    t_profit := longBO ? longPT : shortPT
    inTrade := longBO ? 1 : -1

if inTrade == 1
    if high >= t_profit or low <= t_stop
        inTrade := 0

if inTrade == -1
    if high >= t_stop or low <= t_profit
        inTrade := 0

//plots
plotshape(longBO, style = shape.triangleup, color= color.lime, location = location.belowbar)
plotshape(shortBO, style = shape.triangledown, color= color.red, location = location.abovebar)
plot(inTrade != 0 ? t_stop : na, color=color.red, style= plot.style_linebr)
plot(inTrade != 0 ? t_profit : na, color=color.lime, style= plot.style_linebr)


//calculate trading position
t_risk = equity * risk/100
if longBO or shortBO
    positionSize := longBO ? math.floor(t_risk/longSLdistance) : math.floor(t_risk/shortSLdistance)

plot(positionSize, color=color.purple)