Future Risk Calculator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © irchamaji

//@version=5
indicator("Future Risk Calculator", "Risk Calc", overlay=true)


balance = input.float(defval=1000.00, title="Balance ($)", group="Your Account")
leverage = input.int(defval=125, title="Leverage (x)", group="Your Account")



// direction = input.string(defval="LONG", options=["LONG","SHORT"], title="Direction", group="Your Trade")
entry_price = math.round_to_mintick(input.price(defval=21000, title="Entry Price ==", group="Your Trade"))

// float _price = switch entry_price
//     "Last Bar Close" => close
//     "Last Bar High" => low
tp_price = math.round_to_mintick(input.price(defval=22000, title="Target Price ++", group="Your Trade"))
sl_price = math.round_to_mintick(input.price(defval=20500, title="Stop Loss Price --", group="Your Trade"))
direction = tp_price>sl_price? entry_price>tp_price?"SETUP ERROR":entry_price<sl_price?"SETUP ERROR": "LONG" : entry_price<tp_price?"SETUP ERROR":entry_price>sl_price?"SETUP ERROR": "SHORT"

max_loss_percentage = input.float(defval=2, options=[0.25,0.5,0.75, 1, 2, 3, 5], title="Max Loss Risk (%)", group="Risk Management")

/// CALCULATION
max_loss_qty_usd = max_loss_percentage/100 * balance
initial_qty_coin = math.abs(max_loss_qty_usd/(sl_price-entry_price))
initial_qty_usd = initial_qty_coin*entry_price/leverage
margin_percentage = initial_qty_usd/balance*100

tp_price_percent = math.abs((tp_price-entry_price)*100/entry_price)
profit_usd = math.abs((tp_price*initial_qty_coin)-(entry_price*initial_qty_coin))
profit_roe = profit_usd/initial_qty_usd*100

sl_price_percent = math.abs((sl_price-entry_price)*100/entry_price)
loss_usd = math.abs((sl_price*initial_qty_coin)-(entry_price*initial_qty_coin))
loss_roe = loss_usd/initial_qty_usd*100

rr_ratio = profit_usd/loss_usd
rr_ratio_ideal = rr_ratio >= 2 ? direction == "SETUP ERROR" ? "SETUP ERROR": "IDEAL" : "NOT IDEAL"


tp_text = "Target: " + str.tostring(tp_price) +" (" + str.tostring(tp_price_percent, '#.##') +"%), ROE: " + str.tostring(profit_roe,'#.##') + "%"
entry_text = "Entry: " + str.tostring(entry_price)+ ", RR Ratio: " + str.tostring(rr_ratio,'#.##') + " ["+ rr_ratio_ideal+"]"
sl_text = "Stop: " + str.tostring(sl_price) +" (" + str.tostring(sl_price_percent, '#.##') +"%), ROE: " + str.tostring(loss_roe,'#.##') + "%"


if barstate.islastconfirmedhistory
    table_detail = table.new(position.top_right,columns=2, rows=15,bgcolor=color.new(color.yellow,20),frame_color=color.blue,frame_width=3)//,border_color=color.blue,border_width=2)
    table.merge_cells(table_detail, start_column=0, start_row=0, end_column=1, end_row=0)
    table.cell(table_detail, column=0, row=0, text="Risk Calculation")
    table.cell(table_detail,text_halign=text.align_left, column=0, row=1, text="Balance:")
    table.cell(table_detail,text_halign=text.align_left, column=1, row=1, text="$"+str.tostring(balance))
    table.cell(table_detail,text_halign=text.align_left, column=0, row=2, text="Leverage:")
    table.cell(table_detail,text_halign=text.align_left, column=1, row=2, text=str.tostring(leverage)+"x")
    table.cell(table_detail,text_halign=text.align_left, column=0, row=3, text="Direction:")
    table.cell(table_detail,text_halign=text.align_left, column=1, row=3, text=direction)
    table.cell(table_detail,text_halign=text.align_left, column=0, row=4, text="Risk:")
    table.cell(table_detail,text_halign=text.align_left, column=1, row=4, text=str.tostring(max_loss_percentage)+"%")
    table.cell(table_detail,text_halign=text.align_left, column=0, row=5, text="")
    table.cell(table_detail,text_halign=text.align_left, column=1, row=5, text="")
    table.cell(table_detail,text_halign=text.align_left, column=0, row=6, text="Quantity:")
    table.cell(table_detail,text_halign=text.align_left, column=1, row=6, text=str.tostring(initial_qty_coin,'#.###') + " coins")
    table.cell(table_detail,text_halign=text.align_left, column=0, row=7, text="Initial Margin:")
    table.cell(table_detail,text_halign=text.align_left, column=1, row=7, text=direction == "SETUP ERROR" ? "SETUP ERROR":"$" + str.tostring(initial_qty_usd, '#.##') + " (" + str.tostring(margin_percentage,'#.##') + "%)")
    table.cell(table_detail,text_halign=text.align_left, column=0, row=8, text="Potential Profit:")
    table.cell(table_detail,text_halign=text.align_left, column=1, row=8, text=direction == "SETUP ERROR" ? "SETUP ERROR":"$"+str.tostring(profit_usd, '#.##') + " ("+ str.tostring(profit_roe,'#.##') +"%)")
    table.cell(table_detail,text_halign=text.align_left, column=0, row=9, text="Potential Loss:")
    table.cell(table_detail,text_halign=text.align_left, column=1, row=9, text=direction == "SETUP ERROR" ? "SETUP ERROR":"$"+str.tostring(loss_usd, '#.##') + " ("+ str.tostring(loss_roe,'#.##') +"%)")

    profit_box = box.new(bar_index[20], tp_price, bar_index + 30, entry_price)
    box.set_bgcolor(profit_box, color.new(color.green, 90))
    box.set_border_width(profit_box, 0)
    
    loss_box = box.new(bar_index[20], sl_price, bar_index + 30, entry_price)
    box.set_bgcolor(loss_box, color.new(color.red, 90))
    box.set_border_width(loss_box, 0)
    
    entry_line = line.new(bar_index[20], entry_price, bar_index + 30, entry_price)
    line.set_width(entry_line,2)
    line.set_color(entry_line,color.blue)
    
    tp_line = line.new(bar_index[20], tp_price, bar_index + 30, tp_price)
    line.set_width(tp_line,2)
    line.set_color(tp_line,color.green)

    sl_line = line.new(bar_index[20], sl_price, bar_index + 30, sl_price)
    line.set_width(sl_line,2)
    line.set_color(sl_line,color.red)
    
    tp_label = label.new(bar_index+30, tp_price, text=tp_text)
    label.set_style(tp_label,label.style_label_left)
    label.set_color(tp_label, color.teal)
    label.set_textcolor(tp_label, color.white)
    
    entry_label = label.new(bar_index+30, entry_price, text=entry_text)
    label.set_style(entry_label,label.style_label_left)
    label.set_color(entry_label, rr_ratio >= 2 ? direction == "SETUP ERROR" ? color.red : color.teal : color.red)
    label.set_textcolor(entry_label, color.white)
    
    sl_label = label.new(bar_index+30, sl_price, text=sl_text)
    label.set_style(sl_label,label.style_label_left)
    label.set_color(sl_label, color.teal)
    label.set_textcolor(sl_label, color.white)

