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

//@version=5


// ------ Settings Inputs -----------------------------------------------------------------------------------------------------
// "Capital" -- enter your portfolio balance
// "Risk" -- enter the percent of your portfolio you are willing to lose if the stop loss is hit
// "Entry" -- enter the price at which you will enter the trade
// "Stop" -- enter the price at which your stop loss will be set
// "Target" -- enter the price at which your take profit will be set
// ----------------------------------------------------------------------------------------------------------------------------

// ------ Outputs -------------------------------------------------------------------------------------------------------------
// "Capital" -- displays the portfolio balance entered in settings
// "Risk" -- displays the % loss entered in settings and the corresponding amount of your portfolio
// "Entry" -- displays the entry price entered in settings
// "Stop" -- displays the stop loss price entered in settings
// "Stop %" -- displays the calculated percentage loss from the entry price
// "Target" -- displays the take profit price entered in settings
// "Target %" -- displays the calculated percentage profit from the entry price
// "Leverage" -- displays the calculated leverage based on your max loss and stop loss settings



indicator('OmidCapitalCal', overlay=true, max_bars_back = 500)

dicimals = input.bool(defval= false, title= "Display decimals up to 8 digits")


// Get inputs
Capital = input.float(title='Capital', defval=1000, minval=.001)
Risk = input.float(title='Risk%', defval=1, minval=0.01, maxval=100, step=0.1) * 0.01
Entry = input.price(defval=0, title="Entry$", confirm=true)
Stop = input.price(defval=0, title="Stop$", confirm=true)
Target = input.price(defval=0, title="Target$", confirm=true)
Entry_Line  = plot(Entry, title="Entry", color=#ffb74d, linewidth=1, offset= 100, show_last= 150)
Target_Line = plot(Target, title="Target", color=#5b9cf6, linewidth=1, offset= 100, show_last= 150)
Stop_Line = plot(Stop, title="Stop", color=#f77c80, linewidth=1, offset= 100, show_last= 150)

// Calculations for risk
amt_Risk = Capital * Risk
Reward = math.abs((Target - Entry) / (Entry - Stop))
Reward := math.round(Reward * 1000) / 1000
Profit = math.abs((Reward * amt_Risk))
Profit := math.round(Profit * 1000) / 1000
Stop_pct = math.abs((Stop / Entry - 1) * 100)
Stop_pct := math.round(Stop_pct * 1000) / 1000
Target_pct = math.abs((Target / Entry - 1) * 100)
Target_pct := math.round(Target_pct * 1000) / 1000
Leverage = Risk / Stop_pct * 100
Leverage := math.round(Leverage * 100) / 100

if Entry == 0 or Stop == 0
    Leverage := 0
    Leverage

// Create and populate table
var theTable= table.new(position=position.bottom_left, columns=3, rows=3, bgcolor=color.white, border_width=0)
if barstate.islast
    table.set_border_color(theTable, color.white)

    table.cell(theTable, 0, 0, text='Capital = $' + str.tostring(Capital))
    table.cell(theTable, 1, 0, text='Risk = $' + str.tostring(amt_Risk))
    table.cell(theTable, 2, 0, text='Leverage = ' + str.tostring(Leverage))
    table.cell(theTable, 0, 1, text='Entry = $' + str.tostring(Entry))
    table.cell(theTable, 1, 1, text='Stop = $' + str.tostring(Stop))
    table.cell(theTable, 2, 1, text='Target = $' + str.tostring(Target))
    table.cell(theTable, 0, 2, text='Profit = $' + str.tostring(Profit))
    table.cell(theTable, 1, 2, text='Stop% = ' + str.tostring(Stop_pct) + '%')
    table.cell(theTable, 2, 2, text='Reward = ' + str.tostring(Reward))

// Alerts
alertcondition(ta.cross(close,Entry),
     title="Entry", message="Price hit entry point ➜ \n" + "{{ticker}}, price = {{close}}")

alertcondition(ta.cross(close,Stop),
     title="Stop", message="Price hit Stop ➜ \n" + "{{ticker}}, price = {{close}}")

alertcondition(ta.cross(close,Target),
     title="Target", message="Price hit Target ➜ \n" + "{{ticker}}, price = {{close}}")
