Turtle Money Management
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © miraalgo

//@version=5
indicator("Turtle Money Management",overlay=true)
cap = input(10000,title="Capital")
risk = input.float(2,step=1,title="Risk % ")/100
mlt = input.float(2,step=0.1,title="ATR Multiplier",tooltip = "for stop-loss")
len = input.int(20,step=1,title="ATR Length")
direction = input.string(defval="Long",options=["Long","Short"],title="Direction")
b_daily = input.bool(defval=true,title="Use daily ATR")

////////////////////////////////////////////////

atr_daily= request.security(syminfo.tickerid, "1D", ta.atr(len))
voltly = b_daily ? atr_daily/close : ta.atr(len)/close
d = direction=="Long"? 1:-1
stopl = math.round_to_mintick((1-(d*(voltly*mlt)))*close)
invest = cap*(risk/(voltly*mlt))


plot(invest,display=display.none,title="Position ₵")
plot(stopl,color=color.yellow,title="Stop-loss",style=plot.style_circles)



var risktable = table.new(position = position.top_right, columns = 2, rows = 4, bgcolor = color.rgb(204, 230, 255), border_width = 2, frame_color= color.black, frame_width = 2)
table.set_border_color(risktable, color.black)
if barstate.islast
    table.cell(table_id = risktable, column = 0, row = 0 , text = "Capital ₵")
    table.cell(table_id = risktable, column = 1, row = 0,width = 15,  text = str.tostring( cap))
    
    table.cell(table_id = risktable, column = 0, row = 1, text = "Position 	₵")
    table.cell(table_id = risktable, column = 1, row = 1,width = 15,  text =str.tostring(math.round(invest)) )

    table.cell(table_id = risktable, column = 0, row = 2,  text = "Volatility %")
    table.cell(table_id = risktable, column = 1, row = 2,width = 15,  text =str.tostring(math.round(voltly*100,2)))
    
    table.cell(table_id = risktable, column = 0, row = 3, text = "Stop-Loss")
    table.cell(table_id = risktable, column = 1, row = 3,width = 15,  text =str.tostring(stopl) )
    
    
    
    