Forex Lot Size Calculator [AKCHOOO]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © akchooo
//credits - source adapted from @hanabil

//@version=5
indicator("Lot Size Calculator", max_bars_back=2000, overlay=true)

tst(x)=> str.tostring(x)
var int dec = str.length(str.tostring(syminfo.mintick))-2
trc(number) =>
    factor = math.pow(10, dec)
    int(number * factor) / factor
trc_(number) =>
    factor = math.pow(10, 0)
    int(number * factor) / factor


// -------------------------
// Price
gr1         = 'Stop Loss'
sl          = input.price(20, 'Ticks'  , group=gr1, inline='2')
partial          = input.price(25, 'Percentage of First Partial [%]'  , group=gr1, inline='3')
round          = input(3, 'Round Lot Size to Which Decimal'  , group=gr1, inline='4')
slpartial1  = partial/100
slpartial2 = 1-slpartial1

// RISK MANAGER
gr11        = 'Risk Manager'
equity      = input(100000, 'Equity', group=gr11)
riskInp     = input(0.500, title='Risk % of Equity'  , group=gr11)

risk        = riskInp
balance     = equity
riskCurr    = risk*balance/100
slRangeL    = sl
st          = slRangeL/syminfo.mintick
pzfull          = riskCurr/slRangeL 
pzpartial1         = pzfull*slpartial1
pzpartial2         = pzfull*slpartial2 

pz = math.round(pzfull,round)
pz1 = math.round(pzpartial1,round)
pz2 = math.round(pzpartial2,round)

// ----------------
// Smart Table
// --------
gr10        = 'Table'
tabPosI     = input.string('Top', 'Table Position', ['Top', 'Middle', 'Bot'], group=gr10)
tabCol      = input.color(color.new(color.black, 35), 'Table Color', inline='1', group=gr10)
tabCol2      = input.color(color.new(color.gray, 35), 'Highlight Lot Color', inline='1', group=gr10)
textCol     = input.color(color.white, 'Text', inline='1', group=gr10)
textSizeI   = input.string('Small', 'Text Size', ['Small', 'Tiny', 'Normal'], group=gr10)
textSize    = textSizeI=='Small'? size.small : textSizeI=='Tiny'? size.tiny : size.normal 

tabPos      = tabPosI=='Top'? position.top_right : tabPosI=='Bot'? position.bottom_right : position.middle_right
var smartTable  = table.new(tabPos, 50, 50, color.new(color.black,100), color.black, 1, color.black,1)

table.cell(smartTable, 0, 0, 'Equity [$]'   , text_color=textCol, text_size=textSize, bgcolor=tabCol)
table.cell(smartTable, 0, 1, 'Risk [%]'     , text_color=textCol, text_size=textSize, bgcolor=tabCol)
table.cell(smartTable, 0, 2, 'Risk [$]'     , text_color=textCol, text_size=textSize, bgcolor=tabCol)
table.cell(smartTable, 0, 3, 'Stoploss [Ticks]' , text_color=textCol, text_size=textSize, bgcolor=tabCol)
table.cell(smartTable, 0, 5, 'Full Lot Size' , text_color=textCol, text_size=textSize, bgcolor=tabCol2)
table.cell(smartTable, 0, 6, 'Partial Lot Size' , text_color=textCol, text_size=textSize, bgcolor=tabCol2)
table.cell(smartTable, 0, 7, 'Remainder Lot Size' , text_color=textCol, text_size=textSize, bgcolor=tabCol2)

table.cell(smartTable, 1, 0, tst(equity) , text_color=textCol, text_size=textSize, bgcolor=tabCol)
table.cell(smartTable, 1, 1, tst(risk)   , text_color=textCol, text_size=textSize, bgcolor=tabCol)
table.cell(smartTable, 1, 2, tst(riskCurr)   , text_color=textCol, text_size=textSize, bgcolor=tabCol)
table.cell(smartTable, 1, 3, tst(trc(sl)), text_color=textCol, text_size=textSize, bgcolor=tabCol)
table.cell(smartTable, 1, 5, tst(trc(pz)), text_color=textCol, text_size=textSize, bgcolor=tabCol2)
table.cell(smartTable, 1, 6, tst(trc(pz1)), text_color=textCol, text_size=textSize, bgcolor=tabCol2)
table.cell(smartTable, 1, 7, tst(trc(pz2)), text_color=textCol, text_size=textSize, bgcolor=tabCol2)



