ATR Position Size Automation
// 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("ATR Position Size", "Position Size", true)


//User Inputs
TimeFrame = input.timeframe("D", "Timeframe the ATR is Calculated off of")
AccountSize = input.float(10000, "Account Size to base Risk off of")
AccountRisk = input.float(2, "Percentage Risk Of your Account Size")
ATRPercentageRisk = input.float (100, "Percentage Risk of the ATR (put in 200 for 2x ATR etc.)")

ShowAccountSize = input.bool (true, "Your Account Size")
ShowRisk = input.bool (true, "Percentage on the table?")
ShowDollarRisk = input.bool(true, "Dollar Risk (based off your percentage risk)")

ShowStop = input.bool (true, "Long Stop Price")
ShowStopS = input.bool (true, "Short Stop Price")

ShowATR = input.bool(true, "Show ATR  on the table?")
ShowATRP = input.bool(true, "Shows ATR * The Percentage of the ATR you want (ATR STOP)")

PlotLongStop = input.bool(true, "A line on the Long Stop Level", tooltip = "These will keep moving")
PlotShortStop = input.bool(true, "A line on the Short Stop Level",  tooltip = "These will keep moving")

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])

//import time frame 
O = request.security(syminfo.ticker, TimeFrame, open)
H = request.security(syminfo.ticker, TimeFrame, high)
L = request.security(syminfo.ticker, TimeFrame, low)
C = request.security(syminfo.ticker, TimeFrame, close)

//Indicator math stuff
atr = request.security(syminfo.ticker, TimeFrame, ta.atr(14))
atr2 = math.round(atr,2)
dtr = request.security(syminfo.ticker, "D", math.round(high - low,2))
atrp = math.round(dtr/atr * 100)

//position size math stuff
ATRPRisk = (ATRPercentageRisk/100) * atr2
StopPsize = math.round((AccountRisk /100) * AccountSize,precision = 2) //200
PositionSize = math.round((StopPsize / ATRPRisk),2)
Stop = C - ATRPRisk
StopS = C + ATRPRisk


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

//Table Cells
    table.cell(myTable, 0,0, "Max Size: " + str.tostring(PositionSize), text_color = textcolor, bgcolor = backgroundcolor)
    table.cell(myTable, 0,6, "ATR: " + str.tostring(atr2),text_color = ShowATR? textcolor : na, bgcolor = ShowATR? backgroundcolor : na)
    table.cell(myTable, 0,2,ShowRisk? "Risking: " + str.tostring(AccountRisk) + "%":na,text_color = ShowRisk? textcolor : na, bgcolor = ShowRisk? backgroundcolor : na)
    table.cell(myTable, 0,1,ShowAccountSize? "Act Sz: $" + str.tostring(AccountSize):na,text_color = ShowAccountSize? textcolor : na, bgcolor = ShowAccountSize? backgroundcolor : na)
    table.cell(myTable, 0,4, ShowStop? "St Long: " + str.tostring(Stop):na, text_color = ShowStop? textcolor : na, bgcolor = ShowStop? backgroundcolor : na)
    table.cell(myTable, 0,5, ShowStopS? "St Short: " + str.tostring(StopS):na, text_color = ShowStopS? textcolor : na, bgcolor = ShowStopS? backgroundcolor : na)
    table.cell(myTable, 0,3, ShowDollarRisk? "Risking: $" + str.tostring(StopPsize):na, text_color = ShowDollarRisk? textcolor : na, bgcolor = ShowDollarRisk? backgroundcolor : na)
    table.cell(myTable, 0,7, ShowATRP? "ATR Stop: " + str.tostring(ATRPRisk):na,text_color = ShowATRP? textcolor : na, bgcolor = ShowATRP? backgroundcolor : na)

//Plot Lines? 
plot(PlotLongStop? Stop: na, "Long Stop", color.green)
plot (PlotShortStop? StopS :na, "Short Stop", color.red)


