Position Sizing Calculator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © sosacur01

//@version=5
indicator("Position Sizing Calculator", overlay=true)

//Table Inputs
Equity      = input.float(defval=10000, title="Total Equity", group="Position Sizing")
i_pctStop   = input.float(defval=2, title="% of Equity at Risk", step=.5, group="Position Sizing")/100
sl_price    = input.float(defval=0, title="Stop Loss Price", group="Position Sizing")
entry_price = input.float(defval=0, title="Entry Price", group="Position Sizing")

//Table Values
positionSize = (Equity  * i_pctStop) / (math.abs(entry_price - sl_price)) 
open_pl      = (close - entry_price) / entry_price * 100

//table
var table myTable = table.new(position.bottom_right, 1, 8, border_width = 1)

if barstate.islast
    text1 = "Total Equity\n" + str.tostring(Equity)
    text2 = "% of Equity at Risk\n" + str.tostring(i_pctStop*100)
    text3 = "Stop Loss Price\n" + str.tostring(sl_price)
    text4 = "Entry Price\n" + str.tostring(entry_price)
    text5 = "Price\n" + str.tostring(close)
    text6 = "Position Size #\n" + str.tostring(positionSize)
    text7 = "Open PL\n" + str.tostring(open_pl)
    table.cell(myTable, 0, 1, text=text1, bgcolor = color.black, text_color = color.white) 
    table.cell(myTable, 0, 2, text=text2, bgcolor = color.black, text_color = color.white) 
    table.cell(myTable, 0, 3, text=text3, bgcolor = color.black, text_color = color.white) 
    table.cell(myTable, 0, 4, text=text4, bgcolor = color.black, text_color = color.white) 
    table.cell(myTable, 0, 5, text=text5, bgcolor = color.black, text_color = color.white) 
    table.cell(myTable, 0, 6, text=text6, bgcolor = color.black, text_color = color.white) 
    table.cell(myTable, 0, 7, text=text7, bgcolor = color.black, text_color = color.white) 
