Position Size Calc. (Minimalist)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

//@version=5
indicator("Position Size Calc.", overlay=true, max_labels_count=5)

var string curr                 = input.string('USD', title="Account Currency", options=['USD','EUR','CHF','CAD','GBP','JPY','AUD','HKD','NZD'])
var string unitcurr             = syminfo.basecurrency
//var string tradepair            = syminfo.ticker
//var float tickerexchange        = request.security(tradepair, '3', open)
var string pair                 = unitcurr+curr
var string reversepair          = curr+unitcurr
var float exchangerate          = request.security(pair, '3', open, ignore_invalid_symbol=1)
var float reverserate           = 1/request.security(reversepair, '3', open, ignore_invalid_symbol=1)
var float userate               = 0

if (curr==unitcurr)
    userate                    := 1
else if (na(exchangerate)==1)
    userate                    := reverserate
else
    userate                    := exchangerate


var float risk_amt              = input.float(3, title="Risk Level in % (1%,2%,3%,etc.)")
var int deposit                 = input.int(1000, title="Account Size")
var float entry                 = input.float(1, title="Entry Price")
var float stop                  = input.float(1, title="Stop Price")
var float tprofit               = input.float(1, title="Take Profit")

//non-forex calculations
var float doll_risk             = (risk_amt/100)*deposit //this is printed as loss also
var float base_risk             = math.abs(entry-stop) //just takes absolute value
var float pos_size              = doll_risk/(base_risk/entry)
var float profit                = (math.abs(entry-tprofit)/entry)*pos_size

//forex calculations 
var float pos_size_base         = pos_size/userate
var float lots                  = (pos_size_base) / 100000

var string GROUP_OTHERS         = "Table"
var string tab_pos              = input.string(position.bottom_right, options=[position.top_left,position.top_right,position.bottom_left,position.bottom_right], title="Position of table", group=GROUP_OTHERS)
var color DEFAULT_CELL_COLOR    = #aaaaaa
var color DEFAULT_TXT_COLOR     = color.white
var color DEFAULT_HEADER_COLOR  = color.black

var tbl = table.new(tab_pos, 5, 5, frame_color=#151715, frame_width=1, border_width=2, border_color=color.new(DEFAULT_TXT_COLOR, 100))

var string text_size            = size.small
    
ignored_list(sym) =>
    bool ignore = switch sym
        "VIX" => true
        => false
    
if barstate.islast and not ignored_list(syminfo.root)

    table.cell(tbl, 0, 0, "Risk %", text_halign = text.align_left, bgcolor = DEFAULT_HEADER_COLOR, text_color = DEFAULT_TXT_COLOR, text_size = text_size)
    table.cell(tbl, 1, 0, str.tostring(risk_amt, "#.##"), text_halign = text.align_right, bgcolor = DEFAULT_HEADER_COLOR, text_color = DEFAULT_TXT_COLOR, text_size = text_size)

    table.cell(tbl, 0, 1, "Profit", text_halign = text.align_left, bgcolor = color.gray, text_color = DEFAULT_TXT_COLOR, text_size = text_size)
    table.cell(tbl, 1, 1, str.tostring(profit, "#.##"), text_halign = text.align_right, bgcolor = DEFAULT_CELL_COLOR, text_color = DEFAULT_TXT_COLOR, text_size = text_size)

    table.cell(tbl, 0, 2, "Loss", text_halign = text.align_left, bgcolor = color.gray, text_color = DEFAULT_TXT_COLOR, text_size = text_size)
    table.cell(tbl, 1, 2, str.tostring(doll_risk, "#.##"), text_halign = text.align_right, bgcolor = DEFAULT_CELL_COLOR, text_color = DEFAULT_TXT_COLOR, text_size = text_size)

    table.cell(tbl, 0, 3, "Pos. Size", text_halign = text.align_left, bgcolor = color.gray, text_color = DEFAULT_TXT_COLOR, text_size = text_size)
    table.cell(tbl, 1, 3, str.tostring(pos_size, "#.##"), text_halign = text.align_right, bgcolor = DEFAULT_CELL_COLOR, text_color = DEFAULT_TXT_COLOR, text_size = text_size)

    table.cell(tbl, 0, 4, "Lots", text_halign = text.align_left, bgcolor = color.gray, text_color = DEFAULT_TXT_COLOR, text_size = text_size)
    table.cell(tbl, 1, 4, str.tostring(lots, "#.######"), text_halign = text.align_right, bgcolor = DEFAULT_CELL_COLOR, text_color = DEFAULT_TXT_COLOR, text_size = text_size)