Leverage and contracts tool
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ExpensiveJok
// This script calculates the necessary leverage to afford any position giving the entry price, the max loss and the whole capital involved.

//@version=5
indicator("Leverage calc", overlay = true, precision = 4)

//--------------------------------------- TIPS ---------------------------

in_lvl_tip   = "English: Entry price of the trade. // Spanish: Precio de entrada para la orden."
qty_risk_tip = "English: Capital risk in the operation. It is the capital that you will lose if the price target the SL. // Spanish: Capital que se perderá si el precio alcanza el SL."
qty_tot_tip  = "English: Total capital involved in the operation. // Spanish: Capital total que será invertido en la posición."
stop_lvl_tip = "English: Stop loss price // Spanish: Precio de Stop loss."

//--------------------------------------- INPUTS -------------------------

in_lvl  = input.float(title = "Entry lvl"   , defval = 0, tooltip = in_lvl_tip, group = "POSITION")
qty_risk= input.float(title = "Equity risk" , defval = 10, minval = 1, tooltip = qty_risk_tip, group = "POSITION" )
qty_tot = input.float(title = "Total eqity" , defval = 100, minval = 1, tooltip = qty_tot_tip, group = "POSITION")
stop_lvl = input.float(title ="SL level"    , defval = 0, tooltip = stop_lvl_tip, group = "POSITION")

    //-- Visual
tab_text_c  = input.color(title = "Text color", defval = color.white, group = "VISUAL")
tab_bg_c    = input.color(title = "Background color", defval = color.black, group = "VISUAL")
tab_pos     = input.string(title= "Table position", defval = "Top right", options = ["Top left", "Top center", "Top right", "Middle left", "Middle center", "Middle right", "Bottom left", "Bottom center", "Bottom right"], group = "VISUAL")
//--------------------------------------- PROCESS ------------------------
// The script determinates if the position side is long or short and select the correct formula.
contracts = (in_lvl - stop_lvl) > 0 ? qty_risk / (in_lvl - stop_lvl) : qty_risk / (stop_lvl - in_lvl)

leverage = (contracts * in_lvl) / qty_tot

contracts_string= str.tostring(contracts)
leverage_string = str.tostring(leverage)

tab_pos_switch = switch tab_pos
    "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
    "Bottom right"  => position.bottom_right
    
//---------------------------------------- PLOTS ------------------------
tab = table.new(position = tab_pos_switch, columns = 2, rows = 2, frame_width = 1, border_width = 1, bgcolor = tab_bg_c)
table.cell(tab, column = 0, row = 0, text = "Leverage:", text_color = tab_text_c, text_halign = text.align_center, text_valign = text.align_center, text_size = size.auto)
table.cell(tab, column = 1, row = 0, text = leverage_string, text_color = tab_text_c, text_halign = text.align_center, text_valign = text.align_center, text_size = size.normal)
table.cell(tab, column = 0, row = 1, text = "Contracts:", text_color = tab_text_c, text_halign = text.align_center, text_valign = text.align_center, text_size = size.normal)
table.cell(tab, column = 1, row = 1, text = contracts_string, text_color = tab_text_c, text_halign = text.align_center, text_valign = text.align_center, text_size = size.normal)

