crypto 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/
// © Awke


//@version=5
indicator(title = "crypto Position Size Calcultor",  shorttitle="CPSC" , overlay=true )

RULE1= " Trade Info "
Rule2= " Account Info"
headline= "Trade display info "


// trade info inputs 

entry = input.float(0.0, title="Entry" , group=RULE1)
stoploss = input.float(0.0, title = "Stop Loss", group =RULE1)
takeprofit = input.float(0.0, title="Target Price", group =RULE1)


// Acoount info inputs 

Account_balance= input.float(0.0 , title = "Account Balance",group=Rule2)
riskpertrade = input.float(0.0 , title="Risk % of the trade",group=Rule2)


// finding out the stop loss and target 
stop_loss_distanation = entry - stoploss
take_profit_distantion = takeprofit - entry

// rrturing into percentage 
sl_per = stop_loss_distanation / entry * 100
tg_per = take_profit_distantion / entry * 100

//  calculating  risk reward ratio and postion size

risk_reward_ratio = tg_per / sl_per
position_size = Account_balance* riskpertrade / sl_per








// diplaying info in table 

f_Format() =>
    _s = str.tostring(syminfo.mintick)
    _s := str.replace_all(_s, "25", "00")
    _s := str.replace_all(_s, "5",  "0")
    _s := str.replace_all(_s, "1",  "0")



    
ch_1 = 0.00000000002
ch_2 = 1
ch_3 = 0.00
    
// logic of display 

if (entry > ch_1 and stoploss > ch_1 and takeprofit > ch_1 and Account_balance > ch_2  and riskpertrade  > ch_3 )
  

    // options 
    var Bottom_left = position.bottom_left
    var Bottom_right= position.bottom_right
    var Top_left= position.top_left
    var Top_right = position.top_right
    
    
    
    display = input.bool(true, title="Display Info Table", group=headline)
    dposition = input.string(Bottom_right  ,options = [Bottom_left,Bottom_right,Top_left,Top_right ], title = "Display Positions")
    
    
    
    if (display)
     
        var table myTable = table.new(dposition, 2, 6, border_width = 1)
      
    
        txt1 = "Account Balance     " 
        txt2 = str.tostring(Account_balance) + "  USD"
        table.cell(myTable , 0,0,bgcolor = color.rgb(128,0,128) ,text_color = color.rgb(255,255,255), text=txt1, text_halign=text.align_center, text_size = size.auto)
        table.cell(myTable , 1,0,bgcolor = color.rgb(128,0,128) ,text_color = color.rgb(255,255,255), text=txt2, text_halign=text.align_center,text_size = size.auto)
        txt3 = "Risk Per Trade %" 
        txt4= str.tostring(riskpertrade) + " %"
        table.cell(myTable , 0,1,bgcolor = color.rgb(0,255,255) ,text_color = color.rgb(0,0,0), text=txt3, text_halign=text.align_center,text_size = size.auto)
        table.cell(myTable , 1,1,bgcolor = color.rgb(0,255,255) ,text_color = color.rgb(0,0,0), text=txt4, text_halign=text.align_center,text_size = size.auto)
        txt5 = "Entry  " 
        txt6= str.tostring(entry) 
        table.cell(myTable , 0,2,bgcolor = color.black ,text_color = color.white, text=txt5, text_halign=text.align_center,text_size = size.auto)
        table.cell(myTable , 1,2,bgcolor = color.black,text_color = color.white, text=txt6, text_halign=text.align_center,text_size = size.auto)
        txt7 = "Stop Loss" 
        txt8= str.tostring(stoploss) 
        table.cell(myTable , 0,3,bgcolor = color.red ,text_color = color.white, text=txt7, text_halign=text.align_center,text_size = size.auto)
        table.cell(myTable , 1,3,bgcolor = color.red,text_color = color.white, text=txt8, text_halign=text.align_center,text_size = size.auto)
        txt9 = "Take Profit" 
        txt10= str.tostring(takeprofit) 
        table.cell(myTable , 0,4,bgcolor = color.green ,text_color = color.white, text=txt9, text_halign=text.align_center,text_size = size.auto)
        table.cell(myTable , 1,4,bgcolor = color.green,text_color = color.white, text=txt10, text_halign=text.align_center,text_size = size.auto)
        txtx1 = "Position Size Of Trade" 
        txtx2= str.tostring(math.abs(position_size), f_Format()) + " USD"
        table.cell(myTable , 0,5,bgcolor = color.blue ,text_color = color.white, text=txtx1, text_halign=text.align_center,text_size = size.auto)
        table.cell(myTable , 1,5,bgcolor = color.blue,text_color = color.white, text=txtx2, text_halign=text.align_center,text_size = size.auto)
        
        
        



// displaying trade lines like entry , stoploss , takeprofit 
display_trade_lines=input.bool(true, title="Display trade lines")

hline(display_trade_lines ? entry :na , color= color.blue ,linestyle = hline.style_dotted  , linewidth=3)
hline(display_trade_lines ? stoploss :na , color= color.red ,linestyle = hline.style_dotted  , linewidth=3)
hline(display_trade_lines ? takeprofit :na , color= color.green ,linestyle = hline.style_dotted  , linewidth=3)
