Koalafied Risk Management
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Koalafied_3

//@version=5
indicator("Koalafied Risk Management", overlay = true)

// ---------- INPUTS ---------- //
var GRP1 = "PORTFOLIO INPUTS"
portfolio_size = input.float(1000.00, "Portfolio Size", group = GRP1)
risk_sel = input.float(1, "Percentage Risk", minval = 0.1, maxval = 100, step = 0.1, group = GRP1)

var GRP2 = "TRADE INPUTS"
entry_price = input.float(0, "Entry Price", minval = 0, group = GRP2)
stop_price = input.float(0, "Stop Price", minval = 0, group = GRP2)
exit_price = input.float(0, "Exit Price", minval = 0, group = GRP2)

var GRP3 = "PLOT SELECTION"
show_table = input(true, "Show Table", group = GRP3)
show_labels = input(true, title='Show Trade Labels', group = GRP3)
_offset = input(15, "Line offset", group = GRP3)
transp_label = input.int(0, "Label Transparency", minval = 0, maxval = 100, group = GRP3)


// ---------- CALCS ---------- //

current_price = close 
long = entry_price > stop_price

risk_amount = portfolio_size * (risk_sel/100)        // Risk amount 
stop_per = long ? 100 - ((stop_price / entry_price) * 100) : (100 - ((stop_price / entry_price) * 100)) * -1

position_size = risk_amount / (stop_per / 100)
asset_quantity = position_size / entry_price

risk = (entry_price - stop_price) / entry_price
reward = ((exit_price - entry_price) / entry_price)
rr = math.round(reward / risk, 2)

// ---------- PLOTS ---------- //

// Plot Lines 

_offsettime = timenow + math.round(ta.change(time)*_offset)

if show_labels and entry_price > 0
    _entry = label.new(_offsettime, entry_price,"___Entry", xloc = xloc.bar_time, textcolor = color.new(color.white, transp_label), style = label.style_none, size = size.small)
    _stop = label.new(_offsettime, stop_price, "___Stop", xloc = xloc.bar_time, textcolor = color.new(color.red, transp_label), style = label.style_none, size = size.small)
    _exit = label.new(_offsettime, exit_price, "___Exit", xloc = xloc.bar_time, textcolor = color.new(color.green, transp_label), style = label.style_none, size = size.small)

    label.delete(_entry[1])
    label.delete(_stop[1])
    label.delete(_exit[1])


// Plot Table 

text_col = color.white
b_col = color.black
col_trade = color.new(color.green, 70)
col_risk = color.new(color.red, 70)
col_position = color.new(color.orange, 70)

var table RiskTable = table.new(position.bottom_right, 2, 12, frame_color=color.new(color.white, 50), frame_width=1, border_width=1, border_color=color.new(color.white, 50))
if barstate.islast
    
    // Headings
    if show_table
        table.cell(RiskTable, 0, 0, 'INFO', text_color = color.white, text_size = size.small, bgcolor = color.new(color.olive,75))
        table.cell(RiskTable, 1, 0, 'VALUE', text_color = color.white, text_size = size.small, bgcolor = color.new(color.olive,75))

    
if (show_table)
    table.cell(RiskTable, 0, 1, "Current Price", text_color = text_col, text_size = size.small, bgcolor = b_col)
    table.cell(RiskTable, 1, 1, '$' + str.tostring(math.round(current_price, 2)), text_color = text_col, text_size = size.small, bgcolor = b_col)
    
    table.cell(RiskTable, 0, 2, "Portfolio Size", text_color = text_col, text_size = size.small, bgcolor = b_col)
    table.cell(RiskTable, 1, 2, '$' + str.tostring(portfolio_size), text_color = text_col, text_size = size.small, bgcolor = b_col)
    
    table.cell(RiskTable, 0, 3, "Risk Percent", text_color = text_col, text_size = size.small, bgcolor = col_risk)
    table.cell(RiskTable, 1, 3, str.tostring(risk_sel) + '%', text_color = text_col, text_size = size.small, bgcolor = b_col)
    
    table.cell(RiskTable, 0, 4, "Risk Amount", text_color = text_col, text_size = size.small, bgcolor = col_risk)
    table.cell(RiskTable, 1, 4, '$' + str.tostring(math.round(risk_amount, 2)), text_color = text_col, text_size = size.small, bgcolor = b_col)

    table.cell(RiskTable, 0, 5, "Entry Price", text_color = text_col, text_size = size.small, bgcolor = col_trade)
    table.cell(RiskTable, 1, 5, str.tostring(math.round(entry_price, 2)), text_color = text_col, text_size = size.small, bgcolor = b_col)
    
    table.cell(RiskTable, 0, 6, "Stop Price", text_color = text_col, text_size = size.small, bgcolor = col_trade)
    table.cell(RiskTable, 1, 6, str.tostring(math.round(stop_price, 2)), text_color = text_col, text_size = size.small, bgcolor = b_col)
    
    table.cell(RiskTable, 0, 7, "Exit Price", text_color = text_col, text_size = size.small, bgcolor = col_trade)
    table.cell(RiskTable, 1, 7, str.tostring(math.round(exit_price, 2)), text_color = text_col, text_size = size.small, bgcolor = b_col)

    table.cell(RiskTable, 0, 8, "Stop Percent", text_color = text_col, text_size = size.small, bgcolor = col_risk)
    table.cell(RiskTable, 1, 8, str.tostring(math.round(stop_per, 2)) + '%', text_color = text_col, text_size = size.small, bgcolor = b_col)

    table.cell(RiskTable, 0, 9, "Risk / Reward", text_color = text_col, text_size = size.small, bgcolor = col_risk)
    table.cell(RiskTable, 1, 9, str.tostring(rr), text_color = text_col, text_size = size.small, bgcolor = b_col)
    
    table.cell(RiskTable, 0, 10, "Position Size", text_color = text_col, text_size = size.small, bgcolor = col_position)
    table.cell(RiskTable, 1, 10, '$' + str.tostring(math.round(position_size, 2)), text_color = text_col, text_size = size.small, bgcolor = b_col)
    
    table.cell(RiskTable, 0, 11, "Asset Quantity", text_color = text_col, text_size = size.small, bgcolor = col_position)
    table.cell(RiskTable, 1, 11, str.tostring(math.round(asset_quantity, 5)), text_color = text_col, text_size = size.small, bgcolor = b_col)
    
