Risk Reward Position Size Calculator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TraderCreatorPro

//@version=5
indicator("Position Size Calculator", "Calculator", true)


//User Inputs
AccountSize = input.float(10000, "Account Size to base Risk off of")
AccountRisk = input.float(10, "Percentage Risk Of your Account Size")
avg = input.price(00,title = "Average Entry")
stop = input.float(00,title = "Stop Price")
target =input.float(00,title = "Target Price")
StockorOption = input.bool(false, title = "Check = Option, Uncheck = Stock")

ShowAccountSize = input.bool (false, "Your Account Size")
ShowRisk = input.bool (true, "Percentage and Dollar Risk")
ShowRisk2 = input.bool (false, "Shows Dollar Risk Per share)")
ShowPositionSize = input.bool(true, "Calculated Position Size Based off your stop")
ShowRiskReward = input.bool(true, "Shows Risk Vs Reward")
Showavg = input.bool(true, "Show Avg Entry")
ShowStop = input.bool(true, "Show Stop Price")
ShowTarget = input.bool(true, "Show Target Price")
ShowTargetGain = input.bool(true, "Show Target Gain")

PlotStop = input.bool(true, "Draw Stop on Chart", group = "Plot Lines")
PlotTarget = input.bool(true, "Draw Target On Chart", group = "Plot Lines")
PlotAvg = input.bool(false, "Draw Avg Price On Chart", group = "Plot Lines")

textcolor = input.color(color.white, "Color of Table Text")
backgroundcolor= input.color (color.gray, "Color of Table Background")

BoxPlacement = input.string(position.top_right, "Box Placement", options = [position.bottom_right, position.bottom_left, position.top_right, position.top_center, position.bottom_center])


//Risk Pershare
DollarRisk1 = StockorOption? (math.abs(avg - stop))*100 : math.abs(avg - stop)

//Percentage risk to Dollar risk
PerctoDol = (AccountRisk/100) * AccountSize

//Calculated Position Size
CalcPosition = math.round(PerctoDol/DollarRisk1,2)

//Target Gain Per share
TargetGain = StockorOption? (math.abs(avg - target))*100 : math.abs(avg - target)

//Target Gain Percentage
Targetper= math.round((TargetGain + avg) / avg,2)

//TargetGain Dollar amount
TargetDollar = math.round(TargetGain * CalcPosition,2)

//Risk Reward Calculation
RiskReward = math.round(TargetGain/DollarRisk1,2)


//position size math stuff



var table myTable = table.new(BoxPlacement, 1, 9, border_width=1)
if barstate.islast
 

//Table Cells
    table.cell(myTable, 0,4,ShowRisk? "Risk: %" + str.tostring(AccountRisk) + " or $" + str.tostring(PerctoDol):na,text_color = ShowRisk? textcolor : na, bgcolor = ShowRisk? backgroundcolor : na)
    table.cell(myTable, 0,5,ShowRisk2? "Risk per shr $" + str.tostring(DollarRisk1):na,text_color = ShowRisk2? textcolor : na, bgcolor = ShowRisk2? backgroundcolor : na)
    table.cell(myTable, 0,0,ShowAccountSize? "Act Sz: $" + str.tostring(AccountSize):na,text_color = ShowAccountSize? textcolor : na, bgcolor = ShowAccountSize? backgroundcolor : na)
    table.cell(myTable, 0,3, ShowStop? "Stop: " + str.tostring(stop):na, text_color = ShowStop? textcolor : na, bgcolor = ShowStop? backgroundcolor : na)
    table.cell(myTable, 0,2, ShowTarget? "Target: " + str.tostring(target):na, text_color = ShowTarget? textcolor : na, bgcolor = ShowTarget? backgroundcolor : na)
    table.cell(myTable, 0,7, ShowRiskReward? "RR: " + str.tostring(RiskReward):na, text_color = ShowRiskReward? textcolor : na, bgcolor = ShowRiskReward? backgroundcolor : na)
    table.cell(myTable, 0,8, ShowPositionSize? "Max Position: " + str.tostring(CalcPosition):na,text_color = ShowPositionSize? textcolor : na, bgcolor = ShowPositionSize? backgroundcolor : na)
    table.cell(myTable, 0,1, Showavg? "Entry: " + str.tostring(avg):na, text_color = Showavg? textcolor : na, bgcolor = Showavg? backgroundcolor : na)
    table.cell(myTable, 0,6,ShowTargetGain? "Gain: %" + str.tostring(Targetper) + " or $" + str.tostring(TargetDollar):na,text_color = ShowTargetGain? textcolor : na, bgcolor = ShowTargetGain? backgroundcolor : na)
//Plot Lines? 
plot(PlotStop? stop: na, "Stop", color.red)
plot (PlotTarget? target :na, "Target", color.green)
plot(PlotAvg? avg :na, "Entry", color.yellow)


