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

//@version=4

// This script is intended to be used as a risk management calculator.
// It will calculate the best leverage to use based on the maximum percentage of loss you are willing to incur on your trading portfolio.
// Also calculates the order value and order qty based on your inputs.
// Please note this calculator does not take into account any trading fees imposed by the exchange you are using.
// *** Only risking 1% to 5% of your portfolio is considered good risk management ***

// ------ Settings Inputs -----------------------------------------------------------------------------------------------------
// "Portfolio Size" -- enter your portfolio balance
// "% Willing to lose on this trade" -- enter the percent of your portfolio you are willing to lose if the stop loss is hit
// "Entry Price" -- enter the price at which you will enter the trade
// "Stop Loss Price" -- enter the price at which your stop loss will be set
// ----------------------------------------------------------------------------------------------------------------------------

// ------ Outputs -------------------------------------------------------------------------------------------------------------
// "Portfolio" -- displays the portfolio balance entered in settings
// "max loss on trade" -- displays the % loss entered in settings and the corresponding amount of your portfolio
// "Entry Price" -- displays the entry price entered in settings
// "Stop Loss Price" -- displays the stop loss price entered in settings
// "Stop Loss %" -- displays the calculated percentage loss from the entry price
// "Leverage calc" -- displays the calculated leverage based on your max loss and stop loss settings
//  "Order Value" -- displays the value of the order based on the calculated leverage
// "Order Qty" -- displays the calculated order qty based on the calculated leverage


study("Leverage Calculator", "LevCalc", overlay = false)

// Get inputs
port_size = input(title="Portfolio Size", type=input.float, defval=1000, minval=.001)
max_loss_per_trade = input(title="% willing to lose this trade", type=input.float, defval=1, minval=0.1, maxval=100, step=0.1) * 0.01
entry_price = input(title="Entry Price", type=input.float, defval=0, minval=0)
stop_loss_price = input(title="Stop Loss Price", type=input.float, defval=0, minval=0)

// Calculations for risk
amt_max_loss_per_trade = port_size * max_loss_per_trade
stop_loss_pct = abs(((stop_loss_price/entry_price) - 1) * 100)
stop_loss_pct := round(stop_loss_pct * 1000) / 1000
leverage = (max_loss_per_trade / stop_loss_pct) * 100
leverage := round(leverage * 10) / 10
order_qty = port_size * leverage
// order_val := round(order_val * 1000) / 1000
order_val = entry_price * order_qty
order_val := round(order_val * 100) / 100
// order_qty := round(order_qty * 10000) / 10000

if entry_price == 0 or stop_loss_price == 0
    leverage := 0
    order_val := 0
    order_qty := 0

// Create and populate table
var theTable = table.new(position=position.bottom_left, columns=4, rows=2, bgcolor=color.blue, border_width=1)
if barstate.islast
    table.set_border_color(theTable, color.white)
    table.cell(theTable,0,0, text= "Portfolio = $" + tostring(port_size))
    table.cell(theTable,0,1, text= tostring(max_loss_per_trade*100) + "% max loss on trade = $" + tostring(amt_max_loss_per_trade))
    table.cell(theTable,1,0, text= "Entry Price = $" + tostring(entry_price))
    table.cell(theTable,1,1, text= "Stop Loss = $" + tostring(stop_loss_price))
    table.cell(theTable,2,1, text= "Stop Loss = " + tostring(stop_loss_pct) + "%")
    table.cell(theTable,2,0, text= "Leverage calc = " + tostring(leverage))
    table.cell(theTable,3,0, text= "Order Value = $" + tostring(order_val))
    table.cell(theTable,3,1, text= "Order Qty = " + tostring(order_qty))

