Position and Risk Calculator (for Indices) [dR-Algo]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © dR-Algo

//@version=5
indicator("Position and Risk Calculator [dR-Algo]", overlay = true, shorttitle = "Position Size Calculator [dR]")
 
// INPUT 
equity = input.float(50000, title = "Equity", tooltip = "Equity", group='Risk Management')
risk_trade = input.float(1, title = "Risk in %", tooltip = "Risk per Trade in %", minval=0, maxval=100, step=0.1 , group='Risk Management')

entry = input.price(40000, title = "Entry Price", confirm = true, group='Price')
tp = input.price(40000, title = "TP", confirm = true, group='Price')
sl = input.price(40000, title = "SL", confirm = true, group='Price')

table_location_input = input.string("Top Right", title="Location", options=["Bottom Right", "Bottom Left", "Bottom Center", "Top Left", "Top Center", "Top Right", "Middle left", "Middle Center", "Middle Right" ], group='Dashboard Settings')
size = input.string(size.small, title='Text Size', options=[size.normal, size.small, size.tiny], group='Dashboard Settings')

string table_location = switch table_location_input
    "Bottom Right"  => position.bottom_right  
    "Top Left"      => position.top_left  
    "Top Center"    => position.top_center  
    "Top Right"     => position.top_right 
    "Middle left"   => position.middle_left 
    "Middle Center" => position.middle_center 
    "Middle Right"  => position.middle_right 
    "Bottom Left"   => position.bottom_left 
    "Bottom Center" => position.bottom_center 
    => position.bottom_right

// Draw Input Lines on Chart
plot(entry,     color = color.gray)
plot(tp,        color = color.green)
plot(sl,        color = color.red)

GetTickValue() =>
    syminfo.mintick * syminfo.pointvalue


// Dashboard Calculations
risk_money = risk_trade * equity / 100

if (tp > entry ) and (sl > entry)
    runtime.error("Please check SL and TP")

if (tp < entry ) and (sl < entry)
    runtime.error("Please check SL and TP")

tick_value = GetTickValue()

delta_tp = math.abs(tp - entry)
delta_sl = math.abs(sl - entry)
crv = delta_tp / delta_sl

delta_sl_ticks = delta_sl / syminfo.mintick
delta_tp_ticks = delta_tp / syminfo.mintick


lot_size_raw = risk_money / (delta_sl_ticks * tick_value)
lot_size_rounded = math.floor(lot_size_raw)

tp_profit = lot_size_rounded * math.round_to_mintick(delta_tp_ticks) * tick_value
sl_risk = lot_size_rounded * math.round_to_mintick(delta_sl_ticks) * tick_value


// Dashboard Settings
table_color         = color.new(#07071f, 5 )
table_borderColor   = color.new(#d1d1d1, 34)
table_text_color    = color.white
table_separator     = "────────────────"

// Draw Dashboard
var table dashboard = table.new(table_location, rows=15, columns=15, bgcolor=table_color , frame_color=color.gray, frame_width=2, border_color=table_borderColor, border_width = 0)

table.cell(dashboard, text_size=size, column=1, row=1, text_color = table_text_color, text_halign=text.align_left, text="Equity: " + str.tostring(equity) + ' 💰')
table.cell(dashboard, text_size=size, column=1, row=2, text_color = table_text_color, text_halign=text.align_left, text="Risk per Trade: " + str.tostring(risk_trade) + " %")
table.cell(dashboard, text_size=size, column=1, row=3, text_color = table_text_color, text_halign=text.align_left, text="Risk per Trade: " + str.tostring(risk_money) + " 💰")

table.cell(dashboard, text_size=size, column=1, row=4, text_color = table_text_color, text_halign=text.align_center, text=table_separator)
table.cell(dashboard, text_size=size, column=1, row=5, text_color = table_text_color, text_halign=text.align_left, text="Entry: " + str.tostring(entry, format.mintick))
table.cell(dashboard, text_size=size, column=1, row=6, text_color = color.green, text_halign=text.align_left, text="TP: " + str.tostring(tp, format.mintick) + " - " + str.tostring(delta_tp_ticks,format.mintick) + " Ticks")
table.cell(dashboard, text_size=size, column=1, row=7, text_color = color.red, text_halign=text.align_left, text="SL: " + str.tostring(sl, format.mintick) + " - " + str.tostring(delta_sl_ticks,format.mintick) + " Ticks")
table.cell(dashboard, text_size=size, column=1, row=8, text_color = table_text_color, text_halign=text.align_left, text="CRV: " + str.tostring(crv, format.mintick))
table.cell(dashboard, text_size=size, column=1, row=9, text_color = table_text_color, text_halign=text.align_center, text=table_separator)
table.cell(dashboard, text_size=size, column=1, row=10, text_color = table_text_color, text_halign=text.align_left, text="LOT SIZE: " + str.tostring(lot_size_rounded))


table.cell(dashboard, text_size=size, column=2, row=1, text_color = table_text_color, text_halign=text.align_left, text="Symbol: " + str.tostring(syminfo.ticker))
table.cell(dashboard, text_size=size, column=2, row=2, text_color = table_text_color, text_halign=text.align_left, text="Tick: " + str.tostring(syminfo.mintick))
table.cell(dashboard, text_size=size, column=2, row=3, text_color = table_text_color, text_halign=text.align_left, text="Tick Value: " + str.tostring(GetTickValue()) + " 💰")
table.cell(dashboard, text_size=size, column=2, row=4, text_color = table_text_color, text_halign=text.align_center, text=table_separator)

table.cell(dashboard, text_size=size, column=2, row=6, text_color = color.green, text_halign=text.align_left, text="Profit: " + str.tostring(tp_profit) + " 💰")
table.cell(dashboard, text_size=size, column=2, row=7, text_color = color.red, text_halign=text.align_left, text="Risk: " + str.tostring(sl_risk) + " 💰")