Crypto and FX PSC
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © hanabil
//modified by brianusdtperp

//@version=5
indicator("Crypto and FX PSC", max_bars_back=50, overlay=true)
// -------------------------
// Stop Loss
gr1         = 'Stoploss Input'
slp         = input.float(1, 'Stoploss'     , group=gr1, inline='1', tooltip = "Input Percentage for Crypto and PIPS for FX")

// RISK Profile
gr11        = 'Risk Profile'
gr12        = "Crypto"
gr13        = "Forex"
equity      = input(1000, 'Equity (USD)', group=gr11)
p_risk      = input.float(1, title='Risk %'  , group=gr11, maxval=100)
leverage    = input.int(10, title="Leverage", group=gr12, maxval=125, minval=1)
cz          = input.int(100000,"Contract Size",options=[100000,10000,1000,100],group=gr13, tooltip="100,000 = Standard lot\n10,000 = Mini lot\n1,000 = Micro lot\n100 = Nano lot")

// Calculation crypto
slpv       = slp/100
riskv      = p_risk/100
riskdv     = equity*riskv
pz         = riskdv/slpv
margin     = pz/leverage

//Detect Market
market = syminfo.type

//Calculation Forex
//Exchange Rate / Pip Value
iscrypto = "crypto" == market ? "" : "USD"+syminfo.currency
currency = "USD" == syminfo.currency ? "" : iscrypto
exRate = "USD" == syminfo.currency ? 1 : request.security(currency, timeframe.period, close)
exRateFlPoint = "JPY" == syminfo.currency ? 0.01 : 0.0001
//lot size
lotSize = ((riskdv * exRate) / (slp * exRateFlPoint)) / cz

// ----------------
// Smart Table
// --------
gr10        = 'Table'
tabPosI     = input.string('Bot', 'Table Position', ['Bot', 'Middle', 'Top'], group=gr10)
tabCol      = input.color(color.new(#ffffff, 29), 'Table Color', inline='1', group=gr10)
textCol     = input.color(color.rgb(0, 0, 0), 'Text', inline='1', group=gr10)

textSizeI   = input.string('Tiny', '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 (USD)'    , text_color=textCol, text_size=textSize, bgcolor=tabCol)
table.cell(smartTable, 0, 3, 'Stoploss PIP/Percent' , text_color=textCol, text_size=textSize, bgcolor=tabCol)
if market == "crypto"
    table.cell(smartTable, 0, 4, 'Position Size'  , text_color=textCol, text_size=textSize, bgcolor=tabCol)
    table.cell(smartTable, 0, 5, 'Margin'  , text_color=textCol, text_size=textSize, bgcolor=tabCol)
if market == "forex" or market == "cfd" or market == "index"
    table.cell(smartTable, 0, 6, 'Lot Size (FX)'  , text_color=textCol, text_size=textSize, bgcolor=tabCol)

table.cell(smartTable, 1, 0, str.tostring(equity) , text_color=textCol, text_size=textSize, bgcolor=tabCol)
table.cell(smartTable, 1, 1, str.tostring(p_risk) , text_color=textCol, text_size=textSize, bgcolor=tabCol)
table.cell(smartTable, 1, 2, str.tostring(math.round(riskdv,2)), text_color=textCol, text_size=textSize, bgcolor=tabCol)
table.cell(smartTable, 1, 3, str.tostring(math.round(slp,2)), text_color=textCol, text_size=textSize, bgcolor=tabCol)
if market == "crypto"
    table.cell(smartTable, 1, 4, str.tostring(math.round(pz,2)), text_color=textCol, text_size=textSize, bgcolor=tabCol)
    table.cell(smartTable, 1, 5, str.tostring(math.round(margin,2)), text_color=textCol, text_size=textSize, bgcolor=tabCol)
if market == "forex" or market == "cfd" or market == "index"
    table.cell(smartTable, 1, 6, str.tostring(math.round(lotSize,2)), text_color=textCol, text_size=textSize, bgcolor=tabCol)