Interactive Lot/Position Calculator FTX/OKX DCA [RDM13-NOSTRA] 
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RDM13 / NOSTRA

//@version=5
indicator("Lot Calculator FTX/OKX DCA [NOSTRA] ", overlay=true)


// CELL DISPLAY

show_lotsize =           input.bool(title="Use Lot Size ?",  defval=false,    inline="1",  group = "Cell Display")
show_target =            input.bool(title="Use Target ? " ,  defval=true,   inline= "1", group = "Cell Display")
show_stop =             input.bool(title="Use Stop ? " ,     defval=false,   inline= "1", group = "Cell Display")
show_ratio =             input.bool(title="R ? " ,           defval=false,   inline= "1", group = "Cell Display")
show_p2    = input.bool(title="p2 ? " ,           defval=true,   inline= "2", group = "Cell Display")
show_p3    = input.bool(title="p3 ? " ,           defval=false,   inline= "2", group = "Cell Display")
show_p4    = input.bool(title="p4 ? " ,           defval=false,   inline= "2", group = "Cell Display")


// CHART DISPLAY
show_line = input.bool(title="Show Lines ? " ,  defval=true, inline="1",  group = "Chart Display")
show_line_target = input.bool(title="Show Target Line ? " ,  defval=false, inline="1", group = "Chart Display")
show_line_stop = input.bool(title="Show Stop Line ? " ,  defval=false, inline="1", group = "Chart Display")


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, 2)           // base = 0
    int(number * factor) / factor

// -------------------------
// PRICE
gr1         = 'Entry & SL Price'
eP          = input.price(10, 'Entry Price'     , confirm=true,  group=gr1, inline="1")
sl          = input.price(15, 'Target Price'  ,   confirm=true,  group=gr1, inline="1")
eP2          = input.price(8,  'P2'     ,         confirm=true,  group = gr1)
eP3          = input.price(7,  'P3'     ,         confirm=true,  group = gr1 ) 
eP4          = input.price(6,  'P4'     ,                        group = gr1 ) 
stop        = input.price(90,  'Stop Loss'     ,                 group = gr1) 



// RISK MANAGER
gr11        = 'MONEY MANAGEMENT'


equity      = input(100000, 'Account Size', inline="1", group=gr11)
equity_devise = input.string(defval="USD", title="", options=["USD", "USDT", "USDC", "BUSD", "DAI","EUR", "BTC", "ETH"] , inline="1", group=gr11)

riskmode    = input.string(defval="USD", title="Risk Mode", options=["%", "USD"], group=gr11) //== "%"
riskInp     = input(10000, title='Risk '  , group=gr11)
perLotSize  = input.float(10 , title='LotSize' , group=gr11, tooltip = " (ex: 1 lot = 10 USD). Unit per lot = 10")




risk        = riskInp
balance     = equity
riskCurrUSD = risk
riskCurrPerc = risk*balance/100

int riskCurr = (na)

if riskmode == "%"
    riskCurr := riskCurrPerc
else if riskmode == "USD"
    riskCurr := riskCurrUSD
    
    
// stop & target
slRangeL    = eP>sl? eP-sl : sl-eP
st          = slRangeL/syminfo.mintick 

slRangeL2 = eP>stop? stop-eP : eP-stop
st2          = math.abs(slRangeL2)/syminfo.mintick // math.abs ?


pz          = riskCurr/eP 
lz          = pz/perLotSize 


risk2USD  = risk*balance/100
risk2Perc = riskCurr*100 / balance

int risk2 = (na)

if riskmode == "%"
    risk2 := risk2USD
else if riskmode == "USD"
    risk2 := risk2Perc 

//target
targetlong  = ((sl-eP)/eP)*100 // long
targetshort = ((eP-sl)/sl)*100 // short

target_perc = sl > eP ? targetlong : targetshort


//stoploss
stoplong   = ((stop - eP) / eP)*100
stopshort  = ((eP-stop)/stop)*100

stop_perc  = stop < eP ? stoplong : stopshort

//ratio

ratio = (target_perc / math.abs(stop_perc))

//************************//

int risk_label = na
if riskmode =="%"
    risk_label := risk*balance/100
else if riskmode == "USD"
    risk_label := risk


riskp2 = risk_label
pz2 = riskp2/eP2

riskp3 = risk_label * 4
pz3 = riskp3/eP3

riskp4 = risk_label * 8
pz4 = riskp4/eP4



// ----------------
// Smart Table
// --------
gr10        = 'TABLE DISPLAY'

tabPosI     = input.string('Top', 'Table Position', ['Top', 'Middle', 'Bot'], group=gr10)
tabCol      = input.color(color.new(#673ab7, 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 

size_label_Inp = input.string('Small', 'Label Size', ['Small', 'Tiny', 'Normal'], group=gr10)
size_label    = size_label_Inp=='Small'? size.small : size_label_Inp=='Tiny'? size.tiny : size.normal 

tabPos      = tabPosI=='Top'? position.top_center : tabPosI=='Bot'? position.bottom_right : position.middle_right
var smartTable  = table.new(tabPos, 50, 50, color.new(color.black,100), color.new(color.black,100), 2, border_color= color.new(color.black,100) ,border_width=3)


gr13 = "STYLE SETTINGS"

long_col = input.color(color.new(color.blue, 70), 'Long Color', inline='2', group=gr13)
short_col = input.color(color.new(color.red, 70), 'Long Color', inline='2', group=gr13)
line_style   = input.string(line.style_dotted, title="Line Style", options=[line.style_dotted, line.style_solid, line.style_dashed], inline='2', group=gr13)
sCol        = input.color(color.green ,  "Target Color",  inline='2', group=gr13)
stopCol     = input.color(color.orange ,  "Stop Color",  inline='2', group=gr13)

extline = input.string(extend.none, "Line Extended", options=[extend.none,extend.left, extend.right], inline="2", group=gr13)


table.cell( smartTable , 1, 0, 'Risk' + " " + equity_devise     , text_color=textCol, text_size=textSize, bgcolor=tabCol)
if show_lotsize
    table.cell(smartTable, 2, 0, 'Unit/Lot'    , text_color=textCol, text_size=textSize, bgcolor=tabCol)

table.cell( smartTable, 5, 0, 'Position Size' , text_color=textCol, text_size=textSize, bgcolor=tabCol)
if show_lotsize
    table.cell(smartTable, 6, 0, 'Lot Size' , text_color=textCol, text_size=textSize, bgcolor=tabCol)

if show_target
    table.cell(smartTable, 7, 0, 'Target Tick'  , text_color=textCol, text_size=textSize, bgcolor=tabCol)

if show_stop
    table.cell(smartTable, 8, 0, 'Stop Tick'  , text_color=textCol, text_size=textSize, bgcolor=tabCol)
    
if show_ratio
    table.cell(smartTable, 9, 0, 'Ratio'  , text_color=textCol, text_size=textSize, bgcolor=tabCol)


select_usd_1   = tst(risk)  + " (" + tst(trc(risk2)) + "%)"
select_perc_1  = tst(risk)+ "%"  + " (" + tst(trc(risk2))  + ")"


table.cell(smartTable , 1, 1, riskmode=="%" ? select_perc_1 : select_usd_1   , text_color=textCol, text_size=textSize, bgcolor=tabCol) 

if show_lotsize
    table.cell(smartTable, 2, 1, tst(perLotSize), text_color=textCol, text_size=textSize, bgcolor=tabCol) // add


table.cell(smartTable, 5, 1, tst(trc_(pz))+ " " + syminfo.basecurrency, text_color=textCol, text_size=textSize, bgcolor=tabCol)

if show_lotsize
    table.cell(smartTable, 6, 1, tst(trc_(lz))+" lots", text_color=textCol, text_size=textSize, bgcolor=tabCol)

if show_target
    table.cell(smartTable, 7, 1, tst(trc_(st)) + " (" + tst(trc_(target_perc)) + "%)" , text_color=textCol, text_size=textSize, bgcolor=tabCol)
    //table.cell(smartTable, 8, 1, tst(trc(target_perc)) + " %", text_color=textCol, text_size=textSize, bgcolor=tabCol) // ad

if show_stop
    table.cell(smartTable, 8, 1, tst(trc_(st2)) + " (" + tst(trc_(stoplong)) + "%)" , text_color=textCol, text_size=textSize, bgcolor=tabCol)

if show_ratio
    table.cell(smartTable, 9, 1, "R " + tst(trc_(ratio))  , text_color=textCol, text_size=textSize, bgcolor=tabCol)



var color entry_col = na

if show_line == true
    entry_col := sl > eP ? long_col : short_col
else if show_line == false
    entry_col := color.gray



transCol = entry_col
l_col = color.new(entry_col,5)


// entry

eL  = line.new (bar_index[1], eP, bar_index+40, eP, color=l_col, style=line_style, extend=extline)
eL1 = label.new(bar_index+40, eP, 'P1 : '    + tst(eP) + " (" + tst(risk_label) + " " + equity_devise + " = " + tst(trc_(pz)) + " " + syminfo.basecurrency  + ") ", textcolor=color.white, color=transCol, style=label.style_label_left, size=size_label) 
    
line.delete (eL[1])
label.delete(eL1[1])

//*****************************//
if show_p2
    eL_2  = line.new (bar_index[1], eP2, bar_index+40, eP2, color=l_col, style=line_style, extend=extline)
    eL2 =   label.new(bar_index+40, eP2, 'P2 : '    + tst(eP2) + " (" + tst(riskp2) + " " + equity_devise  + " = " + tst(trc_(pz2)) + " " + syminfo.basecurrency  + ") " , textcolor=color.white, color=transCol, style=label.style_label_left , size=size_label) 
    
    line.delete (eL_2[1])
    label.delete(eL2[1])


if show_p3
    eL_3  = line.new (bar_index[1], eP3, bar_index+40, eP3, color=l_col, style=line_style, extend=extline)
    eL3 = label.new(bar_index+40, eP3, 'P3 : '    + tst(eP3) + " (" + tst(riskp3) + " " + equity_devise + " = " + tst(trc_(pz3)) + " " + syminfo.basecurrency  + ") ", textcolor=color.white, color=transCol, style=label.style_label_left, size=size_label)  
    
    line.delete (eL_3[1])
    label.delete(eL3[1])


if show_p4
    eL_4  = line.new (bar_index[1], eP4, bar_index+40, eP4, color=l_col, style=line_style, extend=extline)
    eL4 = label.new(bar_index+40, eP4, 'P4 : '    + tst(eP4) + " (" + tst(riskp4) + " " + equity_devise + " = " + tst(trc_(pz4)) + " " + syminfo.basecurrency  + ") ", textcolor=color.white, color=transCol, style=label.style_label_left, size=size_label)  
    
    line.delete (eL_4[1])
    label.delete(eL4[1])


if show_line_target

    sL  = line.new (bar_index[1], sl, bar_index+40, sl  , color=color.new(sCol,5) , style=line_style, extend=extline)
    sL1 = label.new(bar_index+40, sl, 'Target : ' + tst(sl) + " ("           + tst(trc_(target_perc)) + " %) "   , textcolor=color.white, color=sCol, style=label.style_label_left, size=size_label) 

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



if  show_line_stop
    stopL  = line.new (bar_index[1], stop, bar_index+40, stop  , color=color.new(stopCol,5), style=line_style, extend=extline)
    stopL1 = label.new(bar_index+40, stop, 'Stop Loss : ' + tst(stop)  + " (" + tst(trc_(stoplong)) + "%)"           , textcolor=color.white, color=stopCol, style=label.style_label_left, size=size_label) 

    line.delete (stopL[1])
    label.delete(stopL1[1])
