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


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

var string GROUP_RM             = "Risk Mgt. with ATR Based Stop"
var float risk_amt              = input.float(10, title="Amount risk", group=GROUP_RM)
var int atr_len                 = input.int(14,title="ATR Length", inline="RM1", group=GROUP_RM)
var float atr_multi             = input.int(2 , title="ATR multiplier", group=GROUP_RM, inline="RM1")

var string _TT                  = "Adjustments for how much one point is worth for this specific symbol"
var string sym_filter1          = input.symbol("MNQ1!", inline="_point_worth1", title="Filter:", group=GROUP_RM)
var float  point_worth1         = input.float(2, title="Point value", inline="_point_worth1", group=GROUP_RM)
var string sym_filter2          = input.symbol("MES1!", inline="_point_worth2", title="Filter:", group=GROUP_RM)
var float  point_worth2         = input.float(5, title="Point value", inline="_point_worth2", group=GROUP_RM)

var string GROUP_SLTPLINES      = "Stoploss Guidelines"
var string STR_CURRENT_ATR      = "Current ATR"
var string STR_SUPTREND         = "Supertrend"
var string type_guideline       = input.string(STR_CURRENT_ATR, title="Guideline type (Current ATR / Supertrend)", options=[STR_CURRENT_ATR, STR_SUPTREND], group=GROUP_SLTPLINES)
var int offset                  = input.int(20, title="Offset SL/TP Lines (For Current ATR)", group=GROUP_SLTPLINES)

var string GROUP_OTHERS         = "Others"
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.gray

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

var line ln_sl_short    = na, line.delete(ln_sl_short[1])
var line ln_sl_long     = na, line.delete(ln_sl_long[1])
var string text_size    = size.small

var label lbl_top       = na, label.delete(lbl_top[1])
var label lbl_bott      = na, label.delete(lbl_bott[1])
var line ln_tp_long     = na, line.delete(ln_tp_long[1])
var line ln_tp_short    = na, line.delete(ln_tp_short[1])
var line ln_crnt_close  = na, line.delete(ln_crnt_close)

// Show the table (Unless ignored, ie for VIX)
ignored_list(sym) =>
    bool ignore = switch sym
        "VIX" => true
        // every else not ignored
        => false

float sl_buffer = atr_multi * ta.atr(atr_len)

float _denom = 1
_denom := array.get(str.split(sym_filter1,":"),1) == syminfo.ticker ? point_worth1 : _denom
_denom := array.get(str.split(sym_filter2,":"),1) == syminfo.ticker ? point_worth2 : _denom
safetytab_pos_size = risk_amt / sl_buffer / _denom

if barstate.islast and not ignored_list(syminfo.root)
    // (0,0):[Risk amt.]     (1,0):[data] 
    // (0,1):[ATR n*multi]   (1,1):[data] 
    // (0,2):[Pos. Size]     (1,2):[data]

    table.cell(tbl, 0, 0, "Risk amt.", 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, "ATR(" + str.tostring(atr_len,"#") + ") x "+str.tostring(atr_multi,"#"), text_halign = text.align_left, bgcolor = color.gray, text_color = DEFAULT_TXT_COLOR, text_size = text_size)
    table.cell(tbl, 1, 1, str.tostring(sl_buffer, "#.#"), text_halign = text.align_right, bgcolor = DEFAULT_CELL_COLOR, text_color = DEFAULT_TXT_COLOR, text_size = text_size)

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

 
    if type_guideline == STR_CURRENT_ATR
        
        // TP SL Lines
        float y_top = open+sl_buffer
        float y_bott = open-sl_buffer
    
        ln_sl_short := line.new(bar_index-1+offset, y_top, bar_index+offset+1, y_top, color=color.red, extend=extend.right)
        ln_sl_long := line.new(bar_index-1+offset, y_bott, bar_index+offset+1, y_bott, color=color.green, extend=extend.right)
    
        _y_offset = sl_buffer/10
        lbl_top := label.new(bar_index+offset+5, y_top+_y_offset, str.tostring(y_top,"#.##"), xloc.bar_index, yloc.price,color.new(color.gray,100), style=label.style_label_right)
        lbl_bott := label.new(bar_index+offset+5, y_bott-_y_offset, str.tostring(y_bott,"#.##"), xloc.bar_index, yloc.price,color.new(color.gray,100), style=label.style_label_right)

// Supertrend guided TSL
[supertrend, direction] = ta.supertrend(atr_multi, atr_len)
p0 = plot((open + close) / 2, display=display.none)
p1_up = plot(direction[1] < 0 ? supertrend[1] : na, "Up Trend", color = type_guideline == STR_SUPTREND ? color.green : na, style=plot.style_linebr)
p2_dn = plot(direction[1] < 0? na : supertrend[1], "Down Trend", color = type_guideline == STR_SUPTREND ? color.red : na, style=plot.style_linebr)
fill(p0, p1_up, type_guideline == STR_SUPTREND ? color.new(color.green, 90) : na, fillgaps=false)
fill(p0, p2_dn, type_guideline == STR_SUPTREND ? color.new(color.red, 90) : na, fillgaps=false)
    
