Position & Lot Size Calculator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © hanabil
// Versi lama di v56

//@version=5
indicator("Position/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         = 'Entry & SL Price'
eP          = input.price(100, 'Entry Price'     , group=gr1, inline='1')
sl          = input.price(200, 'StopLoss Price'  , group=gr1, inline='2')
eCol        = input.color(color.green, '', inline='1', group=gr1)
sCol        = input.color(color.red  , '', inline='2', group=gr1)


// RISK MANAGER
gr11        = 'Risk Manager'
equity      = input(100000, 'Equity', group=gr11)
riskInp     = input(0.100, title='Risk % of Equity'  , group=gr11)
perLotSize  = input(100000, title='Unit Size per Lot', group=gr11)

risk        = riskInp
balance     = equity
riskCurr    = risk*balance/100
slRangeL    = eP>sl? eP-sl : sl-eP
st          = slRangeL/syminfo.mintick
pz          = riskCurr/slRangeL 
lz          = pz/perLotSize


// ----------------
// Smart Table
// --------
gr10        = 'Table'
tabPosI     = input.string('Top', 'Table Position', ['Top', 'Middle', 'Bot'], group=gr10)
tabCol      = input.color(color.new(#512da8, 35), 'Table 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, 'Entry'    , text_color=textCol, text_size=textSize, bgcolor=tabCol)
table.cell(smartTable, 0, 3, 'Stoploss' , text_color=textCol, text_size=textSize, bgcolor=tabCol)
table.cell(smartTable, 0, 4, 'SL Tick'  , text_color=textCol, text_size=textSize, bgcolor=tabCol)
table.cell(smartTable, 0, 5, 'Position Size' , text_color=textCol, text_size=textSize, bgcolor=tabCol)
table.cell(smartTable, 0, 6, 'Lot Size' , text_color=textCol, text_size=textSize, bgcolor=tabCol)


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(trc(eP)), 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, 4, tst(trc_(st)), text_color=textCol, text_size=textSize, bgcolor=tabCol)
table.cell(smartTable, 1, 5, tst(trc(pz)), text_color=textCol, text_size=textSize, bgcolor=tabCol)
table.cell(smartTable, 1, 6, tst(trc(lz)), text_color=textCol, text_size=textSize, bgcolor=tabCol)

transCol = color.new(color.red, 100)
eL  = line.new (bar_index, eP, bar_index+10, eP, extend=extend.left, color=eCol)
sL  = line.new (bar_index, sl, bar_index+10, sl, extend=extend.left, color=sCol)
eL1 = label.new(bar_index+10, eP, 'Entry : '    + tst(trc(eP)), textcolor=eCol, color=transCol, style=label.style_label_left)
sL1 = label.new(bar_index+10, sl, 'Stoploss : ' + tst(trc(sl)), textcolor=sCol, color=transCol, style=label.style_label_left)

line.delete (eL[1])
line.delete (sL[1])
label.delete(eL1[1])
label.delete(sL1[1])



