position size for short selling-Roy Lee
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © snagy23

//@version=4
study("position size", overlay= true)
InputLookBack = input(title="lookback period", type = input.integer, defval=50, minval=1)
InputRiskVal = input(title="dollar risk value", type = input.integer, defval=100, minval=1)
InputOffSet = input(title="postion offset by precentage above Highest price period", type = input.integer, defval=0, minval=0)
InputStopLoss = input(title="Stop Loss Price", type = input.float, defval=0, minval=0)
InputAvgPrice = input(title="Your current average price", type = input.float, defval=0, minval=0)
InputTotalShares = input(title="Your current total shares", type = input.integer, defval=0, minval=0)
ShareVal = InputAvgPrice*InputTotalShares
Highesthigh = highest(high,InputLookBack)
StopLossPrice = if InputStopLoss >0
    InputStopLoss
else
    Highesthigh*((InputOffSet/100)+1)
CurrentVal = if InputStopLoss > 0 
    InputStopLoss
else
    close
PositionSize = if InputAvgPrice > 0 and InputTotalShares > 0
    InputRiskVal/ShareVal
else
    InputRiskVal/(StopLossPrice-CurrentVal)

labelexample = if PositionSize > 0
    label.new(bar_index, na, tostring(PositionSize,"#"), color=color.green, textcolor=color.white, style=label.style_label_down, yloc=yloc.abovebar)
else
    label.new(bar_index, na, ("Max Size reached"), color=color.green, textcolor=color.red, style=label.style_label_down, yloc=yloc.abovebar)

label.delete(labelexample[1])
plot(StopLossPrice, color=color.red, linewidth =2)
