Position Size By Risk For Bar Size
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © trader-ap2

//@version=5
indicator("Position Size By Risk For Bar Size", max_labels_count = 500)

// Constant
YES                 = "Yes"
YES_COMPACT         = "Yes Compact"
NO                  = "No"
TOOLTIP1            = "First input box - Enter normal risk amount per trade.\n\nSecond input box - Configure output:\n\tYes - Shows position size and risk amount\n\tYes Compact - Shows only position size\n\tNo - Does not show position size or risk amount"
TOOLTIP2            = "First input box - Enter an alternative risk amount per trade.  For example 2x normal risk amount or 1/2 normal risk amount.\n\nSecond input box - Configure output:\n\tYes - Shows position size and risk amount\n\tYes Compact - Shows only position size\n\tNo - Does not show position size or risk amount"
TOOLTIP3            = "Show the High and Low of the bar in the output?"
TOOLTIP4            = "Show volume for each bar?"

// User input
riskAmountInput1       = input.int(defval=100, title="   Risk Amount Normal", minval = 1,
                         tooltip = TOOLTIP1,
                         inline = "RA1", group = "Risk Amount Normal")

showPositionSizeInput1 = input.string(defval=YES_COMPACT, title="", options = [YES, YES_COMPACT, NO],
                         tooltip = TOOLTIP1,
                         inline = "RA1", group = "Risk Amount Normal")

riskAmountInput2       = input.int(defval=200, title="   Risk Amount Alternative", minval = 1,
                         tooltip = TOOLTIP2,
                         inline = "RA2", group = "Risk Amount Alternative")

showPositionSizeInput2 = input.string(defval=YES_COMPACT, title="", options = [YES, YES_COMPACT, NO],
                         tooltip = TOOLTIP2,
                         inline = "RA2", group = "Risk Amount Alternative")

showHighLowInput       = input.string(defval=YES, title="   Show High/Low", options = [YES, NO],
                         tooltip = TOOLTIP3,
                         group = "Show High/Low") == YES

showVolumeInput        = input.string(defval=NO, title="   Show Volume", options = [YES, NO],
                         tooltip = TOOLTIP4,
                         group = "Volume") == YES

stopSize = high-low

showPositionSize1   = showPositionSizeInput1 == YES or showPositionSizeInput1 == YES_COMPACT
positionSizeStr1    = showPositionSize1 ? "Position Size 1: " + str.tostring(math.floor(riskAmountInput1 / stopSize)) : ""
riskAmountStr1      = showPositionSizeInput1 == YES ?  "\nRisk Amount 1: " + str.tostring(riskAmountInput1) : ""
carriageReturnStr1  = showPositionSize1 ? "\n\n" : ""

showPositionSize2   = showPositionSizeInput2 == YES or showPositionSizeInput2 == YES_COMPACT
positionSizeStr2    = showPositionSize2 ? "Position Size 2: " + str.tostring(math.floor(riskAmountInput2 / stopSize)) : ""
riskAmountStr2      = showPositionSizeInput2 == YES ? "\nRisk Amount 2: " + str.tostring(riskAmountInput2) : ""
carriageReturnStr2  = showPositionSize2 ? "\n\n" : ""

stopSizeStr         = "Stop Size: " + str.tostring(stopSize, format.mintick)

highLowStr          = showHighLowInput ? "\n\nHigh: " + str.tostring(high, format.mintick) + "\nLow: " + str.tostring(low, format.mintick) : ""

label.new(bar_index, 0, ".", style=label.style_none, size=size.huge,
     tooltip=
     positionSizeStr1 +
     riskAmountStr1 +
     carriageReturnStr1 +
     positionSizeStr2 +
     riskAmountStr2 +
     carriageReturnStr2 +
     stopSizeStr +
     highLowStr
     )

plot(showVolumeInput ? volume : na, color = close >= open ? color.rgb(38, 166, 154, 50) : color.rgb(239, 83, 80, 50), style=plot.style_columns)