Position Size Tool
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © shaaah10

//@version=5
indicator("Position Size Tool", overlay = true)

// INPUT
var string gp1 = "CANDLE"
boolTable = input.bool(defval=false, title="Remove", tooltip = "Remove the the related table", group = gp1)
AccountSize = input(title="Account Size", defval=100000, group = gp1)
AccountRisk = input.float(title="Account Risk %", defval=1, group = gp1)

var string gp2 = "LINE"
boolLine = input.bool(defval=false, title="Remove", tooltip = "Remove the movable line and related table", group = gp2)
AccountSizeDt = input(title="Account Size", defval=100000, group = gp2)
AccountRiskDt = input.float(title="Account Risk %", defval=1, group = gp2)

// VARIABLE
riskHigh = close - high
percentHigh = math.abs((riskHigh / close) * 100)
sharesHigh = math.round((AccountSize * (AccountRisk/100)) / riskHigh)

valueMid = (open + close) / 2
riskMid = close - valueMid
percentMid = math.abs((riskMid / close) * 100)
sharesMid = math.round((AccountSize * (AccountRisk/100)) / riskMid)

riskLow = close - low
percentLow = math.abs((riskLow / close) * 100)
sharesLow = math.round((AccountSize * (AccountRisk/100)) / riskLow)

changeOpen = close - open

// CUSTOM LINE
test(x)=> str.tostring(x)
var int dec = str.length(str.tostring(syminfo.mintick))-2
trace(value) =>
    factor = math.pow(10, dec)
    int(value * factor) / factor
trace_(value) =>
    factor = math.pow(10, 0)
    int(value * factor) / factor

sl_value = input.price(100, "Line", group = gp2)
line_color = sl_value > close ? color.green : sl_value < close ? color.red : color.black
line_table_color = sl_value > close ? color.red : sl_value < close ? color.green : na
riskLine = close - sl_value
percentLine = math.abs((riskLine / close) * 100)
sharesLine = math.round((AccountSizeDt * (AccountRiskDt/100)) / riskLine)

if (boolLine == false) 
    sl_line = line.new(bar_index, sl_value, bar_index+10, sl_value, extend=extend.left,color=line_color)
    price_label = label.new(bar_index+10, sl_value, test(trace(sl_value)), textcolor=line_color, color=color.white, style=label.style_label_left)
    line.delete (sl_line[1])
    label.delete(price_label[1])

// TABLE
var string Layout = 'Display'
string  tableSize = input.string("normal", "Table Size", inline = "1", options = ["huge", "large", "normal", "small", "tiny"], group = Layout)
string  tableY = input.string("top", "Position ", inline = "2", options = ["top", "middle", "bottom"], group = Layout)
string  tableX = input.string("right", "", inline = "2", options = ["left", "center", "right"], group = Layout)

var table sizeDisplay = table.new(tableY + "_" + tableX, columns = 4, rows = 4, border_width = 1, border_color = color.black, frame_width = 1, frame_color = color.black)

if (boolTable == false) 
    table.cell(table_id = sizeDisplay, text_size = tableSize, column = 0, row = 2, text = "L", text_color = color.white, bgcolor = color.green)
    table.cell(table_id = sizeDisplay, text_size = tableSize, column = 0, row = 1, text = "M", text_color = color.white, bgcolor = (changeOpen > 0 ? color.green : color.red))
    table.cell(table_id = sizeDisplay, text_size = tableSize, column = 0, row = 0, text = "H", text_color = color.white, bgcolor = color.red)
    table.cell(table_id = sizeDisplay, text_size = tableSize, column = 1, row = 2, text = str.tostring(low, "#.##"), text_color = color.black, bgcolor = color.white)
    table.cell(table_id = sizeDisplay, text_size = tableSize, column = 1, row = 1, text = str.tostring(valueMid, "#.##"), text_color = color.black, bgcolor = color.white)
    table.cell(table_id = sizeDisplay, text_size = tableSize, column = 1, row = 0, text = str.tostring(high, "#.##"), text_color = color.black, bgcolor = color.white)
    table.cell(table_id = sizeDisplay, text_size = tableSize, column = 2, row = 2, text = str.tostring(percentLow, "#.##"), text_color = color.black, bgcolor = color.white)
    table.cell(table_id = sizeDisplay, text_size = tableSize, column = 2, row = 1, text = str.tostring(percentMid, "#.##"), text_color = color.black, bgcolor = color.white)
    table.cell(table_id = sizeDisplay, text_size = tableSize, column = 2, row = 0, text = str.tostring(percentHigh, "#.##"), text_color = color.black, bgcolor = color.white)
    table.cell(table_id = sizeDisplay, text_size = tableSize, column = 3, row = 2, text = str.tostring(sharesLow), text_color = color.black, bgcolor = color.white)
    table.cell(table_id = sizeDisplay, text_size = tableSize, column = 3, row = 1, text = str.tostring(sharesMid), text_color = color.black, bgcolor = color.white)
    table.cell(table_id = sizeDisplay, text_size = tableSize, column = 3, row = 0, text = str.tostring(sharesHigh), text_color = color.black, bgcolor = color.white)

if (boolLine == false) 
    table.cell(table_id = sizeDisplay, text_size = tableSize, column = 0, row = 3, text = "─", text_color = color.black, bgcolor = color.rgb(120, 123, 134, 97))
    table.cell(table_id = sizeDisplay, text_size = tableSize, column = 1, row = 3, text = str.tostring(sl_value, "#.##"), text_color = color.black, bgcolor = color.rgb(120, 123, 134, 97))
    table.cell(table_id = sizeDisplay, text_size = tableSize, column = 2, row = 3, text = str.tostring(percentLine, "#.##"), text_color = color.black, bgcolor = color.rgb(120, 123, 134, 97))
    table.cell(table_id = sizeDisplay, text_size = tableSize, column = 3, row = 3, text = str.tostring(sharesLine), text_color = color.black, bgcolor = color.rgb(120, 123, 134, 97))