Equities Risk Tool [vnhilton]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © vnhilton

//@version=5
indicator("Equities Risk Tool [vnhilton]", "your position", overlay=true)

//Account size
account = input.float(50000, "Account", minval=0, group="Account size")

//Manual inputs for entry position
pointA = input.price(0.00, "entry", group="Position Inputs", confirm=true)
pointB = input.price(0.00, "stop loss", group="Position Inputs", confirm=true)   

//Account percent risks for position
riskSize1 = input.float(0.5, "Risk #1", minval=0, maxval=100, tooltip="Enter your account 1st risk amount here as a percentage", group="Account Risk Options")
riskSize2 = input.float(0.25, "Risk #2", minval=0, maxval=100, tooltip="Enter your account 2nd risk amount here as a percentage", group="Account Risk Options")
riskSize3 = input.float(0.125, "Risk #3", minval=0, maxval=100, group="Account Risk Options")
riskSize4 = input.float(0.0625, "Risk #4", minval=0, maxval=100, group="Account Risk Options")

//Shares calculation types
longType = input.int(-1, "Round Down Factor (Long Shares)", maxval=0, tooltip="Enter the round down factor used to calculate shares for long positions e.g. a round down factor of '2' will round down to the nearest 2 decimal places. A factor of '-2' will round down to the nearest 100", group="Shares Calculation Types")
shortType = input.int(-1, "Round Down Factor (Short Shares)", maxval=0, tooltip="Enter the round down factor used to calculate shares for short positions", group="Shares Calculation Types")

//Percentage sizes of calculated shares
sharesSize1 = input.float(100, "Shares % #1", minval=0, maxval=100, tooltip="Enter your 1st percentage of calculated shares", group="Shares Percentage Results")
sharesSize2 = input.float(75, "Shares % #2", minval=0, maxval=100, tooltip="Enter your 2nd percentage of calculated shares", group="Shares Percentage Results")
sharesSize3 = input.float(50, "Shares % #3", minval=0, maxval=100, group="Shares Percentage Results")
sharesSize4 = input.float(25, "Shares % #4", minval=0, maxval=100, group="Shares Percentage Results")

//Profit target factors
targetMulti1 = input.float(1, "Profit Target Factor #1", minval=0, tooltip="Enter your 1st profit target factor (the factor is applied to the stop loss distance) e.g. if you're aiming for a 1 risk/reward ratio trade, then enter the factor 1 - Set factors equal to each other if you want to hide them.", group="Profit Target Factors")
targetMulti2 = input.float(2, "Profit Target Factor #2", minval=0, tooltip="Enter your 2nd profit target factor", group="Profit Target Factors")
targetMulti3 = input.float(3, "Profit Target Factor #3", minval=0, group="Profit Target Factors")
targetMulti4 = input.float(4, "Profit Target Factor #4", minval=0, group="Profit Target Factors")

//Calculations
stopLossDistance = pointA - pointB

shares1 = math.abs(((riskSize1 / 100) * account) / stopLossDistance)
shares2 = math.abs(((riskSize2 / 100) * account) / stopLossDistance)
shares3 = math.abs(((riskSize3 / 100) * account) / stopLossDistance)
shares4 = math.abs(((riskSize4 / 100) * account) / stopLossDistance)

//Round down function courtesy of tradingcode
roundDown(number, decimals) => math.floor(number * math.pow(10, decimals)) / math.pow(10, decimals)

//Position drawing configuration
entryColor = color.rgb(255, 255, 255, 0)
stopLossColor = color.rgb(242, 54, 69, 0)
profitTargetColor = color.rgb(129, 199, 132, 0)

stopLossFillColor = color.rgb(242, 54, 69, 90)
profitTargetFillColor = color.rgb(129, 199, 132, 90)

//Position line drawings
pB = plot(pointB, "Stop Loss Line", color=stopLossColor, style=plot.style_line)
pA = plot(pointA, "Entry Line", color=entryColor, style=plot.style_line)

fill(pA, pB, stopLossFillColor, "Stop Loss Fill Background")

p1 = plot(stopLossDistance >= 0 ? pointA + (stopLossDistance * targetMulti1) : pointA - (math.abs(stopLossDistance) * targetMulti1), "Profit Target Line #1", color=profitTargetColor, style=plot.style_line)
fill(pA, p1, profitTargetFillColor, "Profit Target #1 Fill Background")

p2 = plot(stopLossDistance >= 0 ? pointA + (stopLossDistance * targetMulti2) : pointA - (math.abs(stopLossDistance) * targetMulti2), "Profit Target Line #2", color=profitTargetColor, style=plot.style_line, display=display.none)
fill(pA, p2, profitTargetFillColor, "Profit Target #2 Fill Background", display=display.none)

p3 = plot(stopLossDistance >= 0 ? pointA + (stopLossDistance * targetMulti3) : pointA - (math.abs(stopLossDistance) * targetMulti3), "Profit Target Line #3", color=profitTargetColor, style=plot.style_line, display=display.none)
fill(pA, p3, profitTargetFillColor, "Profit Target #3 Fill Background", display=display.none)

p4 = plot(stopLossDistance >= 0 ? pointA + (stopLossDistance * targetMulti4) : pointA - (math.abs(stopLossDistance) * targetMulti4), "Profit Target Line #4", color=profitTargetColor, style=plot.style_line, display=display.none)
fill(pA, p4, profitTargetFillColor, "Profit Target #4 Fill Background", display=display.none)

//Main table configuration
tblePos = input.string("Bottom Left", "Table Position", options=["Top Left", "Top Center", "Top Right", "Middle Left", "Middle Center", "Middle Right", "Bottom Left", "Bottom Center", "Bottom Right"], group="Main table Configuration")

tbleFrameColor = input(defval=color.rgb(209, 212, 220, 0), title="Table Frame Color", group="Main table Configuration")
tbleFrameWidth = input.int(1, "Table Frame Width", minval=0, group="Main table Configuration")

tbleBorderColor = input(defval=color.rgb(255, 255, 255, 0), title="Table Border Color", group="Main table Configuration")
tbleBorderWidth = input.int(0, "Table Border Width", minval=0, group="Main table Configuration")

headerTxtSize = input.string("Tiny", "Header Text Size", options=["Auto", "Tiny", "Small", "Normal", "Large", "Huge"], group="Main table Configuration")
bodyTxtSize = input.string("Tiny", "Body Text Size", options=["Auto", "Tiny", "Small", "Normal", "Large", "Huge"], group="Main table Configuration")

textColor = input(defval=color.rgb(0, 0, 0, 0), title="Text Color", group="Main table Configuration")

longColor = input(defval=color.rgb(129, 199, 132, 0), title="Long Cell Color", group="Main table Configuration")
shortColor = input(defval=color.rgb(242, 54, 69, 0), title="Short Cell Color", group="Main table Configuration")

risk1Color = input(defval=color.rgb(202, 255, 191, 0), title="Risk #1 Cell Color", group="Main table Configuration")
risk2Color = input(defval=color.rgb(253, 255, 182, 0), title="Risk #2 Cell Color", group="Main table Configuration")
risk3Color = input(defval=color.rgb(255, 214, 165, 0), title="Risk #3 Cell Color", group="Main table Configuration")
risk4Color = input(defval=color.rgb(255, 173, 173, 0), title="Risk #4 Cell Color", group="Main table Configuration")

sharesSize1Color = input(defval=color.rgb(162, 200, 204, 0), title="Shares Size #1 Cell Color", group="Main table Configuration")
sharesSize2Color = input(defval=color.rgb(109, 168, 175, 0), title="Shares Size #2 Cell Color", group="Main table Configuration")
sharesSize3Color = input(defval=color.rgb(72, 125, 131, 0), title="Shares Size #3 Cell Color", group="Main table Configuration")
sharesSize4Color = input(defval=color.rgb(43, 75, 79, 0), title="Shares Size #4 Cell Color", group="Main table Configuration")

shares1Color = input(defval=color.rgb(183, 212, 216, 0), title="Shares % #1 Cell Color", group="Main table Configuration")
shares2Color = input(defval=color.rgb(131, 181, 187, 0), title="Shares % #2 Cell Color", group="Main table Configuration")
shares3Color = input(defval=color.rgb(83, 145, 152, 0), title="Shares % #3 Cell Color", group="Main table Configuration")
shares4Color = input(defval=color.rgb(54, 95, 100, 0), title="Shares % #4 Cell Color", group="Main table Configuration")

//Importing table
tblePosition = tblePos == "Top Left" ? position.top_left :
     tblePos == "Top Center" ? position.top_center : 
     tblePos == "Top Right" ? position.top_right :
     tblePos == "Middle Left" ? position.middle_left :
     tblePos == "Middle Center" ? position.middle_center :
     tblePos == "Middle Right" ? position.middle_right :
     tblePos == "Bottom Left" ? position.bottom_left :
     tblePos == "Bottom Center" ? position.bottom_center : position.bottom_right

headerTextSize = headerTxtSize == "Auto" ? size.auto :
     headerTxtSize == "Tiny" ? size.tiny :
     headerTxtSize == "Small" ? size.small :
     headerTxtSize == "Normal" ? size.normal :
     headerTxtSize == "Large" ? size.large : size.huge

bodyTextSize = bodyTxtSize == "Auto" ? size.auto :
     bodyTxtSize == "Tiny" ? size.tiny :
     bodyTxtSize == "Small" ? size.small :
     bodyTxtSize == "Normal" ? size.normal :
     bodyTxtSize == "Large" ? size.large : size.huge

tble = table.new(tblePosition, 5, 5, frame_color=tbleFrameColor, frame_width=tbleFrameWidth, border_width=tbleBorderWidth, border_color=tbleBorderColor)

table.cell(tble, 0, 0, stopLossDistance >= 0 ? "LONG (SL: " + str.tostring(roundDown(stopLossDistance, 2)) + ")" : "SHORT (SL: " + str.tostring(math.abs(roundDown(stopLossDistance, 2))) + ")", text_size=headerTextSize, text_color=textColor, bgcolor=stopLossDistance >= 0 ? longColor : shortColor)
table.cell(tble, 1, 0, str.tostring(riskSize1) + "%", text_size=headerTextSize, text_color=textColor, bgcolor=risk1Color)
table.cell(tble, 2, 0, str.tostring(riskSize2) + "%", text_size=headerTextSize, text_color=textColor, bgcolor=risk2Color)
table.cell(tble, 3, 0, str.tostring(riskSize3) + "%", text_size=headerTextSize, text_color=textColor, bgcolor=risk3Color)
table.cell(tble, 4, 0, str.tostring(riskSize4) + "%", text_size=headerTextSize, text_color=textColor, bgcolor=risk4Color)
 
table.cell(tble, 0, 1, "Shares (" + str.tostring(sharesSize1) + "%)", text_size=headerTextSize, text_color=textColor, bgcolor=sharesSize1Color)
table.cell(tble, 0, 2, "Shares (" + str.tostring(sharesSize2) + "%)", text_size=headerTextSize, text_color=textColor, bgcolor=sharesSize2Color)
table.cell(tble, 0, 3, "Shares (" + str.tostring(sharesSize3) + "%)", text_size=headerTextSize, text_color=textColor, bgcolor=sharesSize3Color)
table.cell(tble, 0, 4, "Shares (" + str.tostring(sharesSize4) + "%)", text_size=headerTextSize, text_color=textColor, bgcolor=sharesSize4Color)

table.cell(tble, 1, 1, stopLossDistance >= 0 ? str.tostring(roundDown((sharesSize1 / 100) * shares1, longType)) : str.tostring(roundDown((sharesSize1 / 100) * shares1, shortType)), text_size=bodyTextSize, text_color=textColor, bgcolor=shares1Color)
table.cell(tble, 2, 1, stopLossDistance >= 0 ? str.tostring(roundDown((sharesSize1 / 100) * shares2, longType)) : str.tostring(roundDown((sharesSize1 / 100) * shares2, shortType)), text_size=bodyTextSize, text_color=textColor, bgcolor=shares1Color)
table.cell(tble, 3, 1, stopLossDistance >= 0 ? str.tostring(roundDown((sharesSize1 / 100) * shares3, longType)) : str.tostring(roundDown((sharesSize1 / 100) * shares3, shortType)), text_size=bodyTextSize, text_color=textColor, bgcolor=shares1Color)
table.cell(tble, 4, 1, stopLossDistance >= 0 ? str.tostring(roundDown((sharesSize1 / 100) * shares4, longType)) : str.tostring(roundDown((sharesSize1 / 100) * shares4, shortType)), text_size=bodyTextSize, text_color=textColor, bgcolor=shares1Color)

table.cell(tble, 1, 2, stopLossDistance >= 0 ? str.tostring(roundDown((sharesSize2 / 100) * shares1, longType)) : str.tostring(roundDown((sharesSize2 / 100) * shares1, shortType)), text_size=bodyTextSize, text_color=textColor, bgcolor=shares2Color)
table.cell(tble, 2, 2, stopLossDistance >= 0 ? str.tostring(roundDown((sharesSize2 / 100) * shares2, longType)) : str.tostring(roundDown((sharesSize2 / 100) * shares2, shortType)), text_size=bodyTextSize, text_color=textColor, bgcolor=shares2Color)
table.cell(tble, 3, 2, stopLossDistance >= 0 ? str.tostring(roundDown((sharesSize2 / 100) * shares3, longType)) : str.tostring(roundDown((sharesSize2 / 100) * shares3, shortType)), text_size=bodyTextSize, text_color=textColor, bgcolor=shares2Color)
table.cell(tble, 4, 2, stopLossDistance >= 0 ? str.tostring(roundDown((sharesSize2 / 100) * shares4, longType)) : str.tostring(roundDown((sharesSize2 / 100) * shares4, shortType)), text_size=bodyTextSize, text_color=textColor, bgcolor=shares2Color)
  
table.cell(tble, 1, 3, stopLossDistance >= 0 ? str.tostring(roundDown((sharesSize3 / 100) * shares1, longType)) : str.tostring(roundDown((sharesSize3 / 100) * shares1, shortType)), text_size=bodyTextSize, text_color=textColor, bgcolor=shares3Color)
table.cell(tble, 2, 3, stopLossDistance >= 0 ? str.tostring(roundDown((sharesSize3 / 100) * shares2, longType)) : str.tostring(roundDown((sharesSize3 / 100) * shares2, shortType)), text_size=bodyTextSize, text_color=textColor, bgcolor=shares3Color)
table.cell(tble, 3, 3, stopLossDistance >= 0 ? str.tostring(roundDown((sharesSize3 / 100) * shares3, longType)) : str.tostring(roundDown((sharesSize3 / 100) * shares3, shortType)), text_size=bodyTextSize, text_color=textColor, bgcolor=shares3Color)
table.cell(tble, 4, 3, stopLossDistance >= 0 ? str.tostring(roundDown((sharesSize3 / 100) * shares4, longType)) : str.tostring(roundDown((sharesSize3 / 100) * shares4, shortType)), text_size=bodyTextSize, text_color=textColor, bgcolor=shares3Color)

table.cell(tble, 1, 4, stopLossDistance >= 0 ? str.tostring(roundDown((sharesSize4 / 100) * shares1, longType)) : str.tostring(roundDown((sharesSize4 / 100) * shares1, shortType)), text_size=bodyTextSize, text_color=textColor, bgcolor=shares4Color)
table.cell(tble, 2, 4, stopLossDistance >= 0 ? str.tostring(roundDown((sharesSize4 / 100) * shares2, longType)) : str.tostring(roundDown((sharesSize4 / 100) * shares2, shortType)), text_size=bodyTextSize, text_color=textColor, bgcolor=shares4Color)
table.cell(tble, 3, 4, stopLossDistance >= 0 ? str.tostring(roundDown((sharesSize4 / 100) * shares3, longType)) : str.tostring(roundDown((sharesSize4 / 100) * shares3, shortType)), text_size=bodyTextSize, text_color=textColor, bgcolor=shares4Color)
table.cell(tble, 4, 4, stopLossDistance >= 0 ? str.tostring(roundDown((sharesSize4 / 100) * shares4, longType)) : str.tostring(roundDown((sharesSize4 / 100) * shares4, shortType)), text_size=bodyTextSize, text_color=textColor, bgcolor=shares4Color)