Fundamental Metrics v1.2
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © thebearfib
//
//@version=5

indicator("Fundamental Metrics v1.0",
  shorttitle = 'Funda Bear', 
  overlay = true)

//
// ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
// —————————————————————————————————  Time / Ticker / Source  —————————————————————————————————————————————————————————————
// ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
//
res         = timeframe.period
s           = syminfo.tickerid
closeDa     = request.security(s, res, close)
source      = close
//
// Day Closing range
[dayOpen, dayHigh, dayLow, dayClose] = request.security(syminfo.tickerid, 'D',[open,high,low,close], lookahead = barmerge.lookahead_on)
_dChg = (dayClose - dayLow) / (dayHigh - dayLow)

// week Closing range
[weekOpen, weekHigh, weekLow, weekClose] = request.security(syminfo.tickerid, 'W',[open,high,low,close], lookahead = barmerge.lookahead_on)
_wkChg  = (weekClose - weekLow) / (weekHigh - weekLow)
//
//
// ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
// —————————————————————————————————  sᴛᴀᴛs ᴛᴀʙʟᴇ Inputs     —————————————————————————————————————————————————————————————
// ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
//
_Xxd        = input(false,'═══════  sᴛᴀᴛs ᴛᴀʙʟᴇ   ═════════════════════')
showxE       = input.bool(defval = true, title = "🏦 Show Fundamentals: ")

_fontSize1  = input.string("Normal","🗚 - Font Size", options = [ "Auto", "Tiny", "Small", "Normal", "Large", "Huge"])

position3b   = input.string("Bottom Right","- Table Position",
  options  = ["Top Right", "Top Center", "Top Left", "Middle Right", "Middle Center", "Middle Left", "Bottom Right", "Bottom Center", "Bottom Left"])

//
// ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
// —————————————————————————————————   Shares outstanding calcs   —————————————————————————————————————————————————————————
// ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
//
TSO         = request.financial(s, 'TOTAL_SHARES_OUTSTANDING', 'FQ', ignore_invalid_symbol=true)
MCap        = TSO
MCapCalc    = MCap

//
// ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
// —————————————————————————————————   Float Calcs  ———————————————————————————————————————————————————————————————————————
// ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
//
FSO             = request.financial(s, 'FLOAT_SHARES_OUTSTANDING', 'FY', ignore_invalid_symbol=true)
FFMCap          = FSO * closeDa
floatPer        = FFMCap/MCapCalc*100

_sN             = syminfo.ticker
_fso            = request.financial(syminfo.tickerid, "FLOAT_SHARES_OUTSTANDING", "FY")

institutionData = request.security(syminfo.tickerid, 'D', close)
totalSharesOutstanding = _fso  // Replace with the actual total shares outstanding for your symbol
institutionPercent = ((institutionData / totalSharesOutstanding) * institutionData)

arp = 100 * (ta.sma(high / low, 21) - 1)
adrp = request.security(syminfo.tickerid, 'D', arp)

MCapz        = _fso*closeDa
MCapCalcz    = MCapz
uMz          = ''

if(MCapz >= 1000 and MCapz < 1000000)
    MCapz := MCapz/1000
    uMz := 'K'
if(MCapz >= 1000000 and MCapz < 1000000000)
    MCapz := MCapz/1000000
    uMz := 'M'
if(MCapz >= 1000000000)
    MCapz := MCapz/1000000000
    uMz := 'B'

//Long term debt excl. lease liabilities	FQ, FY	LONG_TERM_DEBT_EXCL_CAPITAL_LEASE
_LTD = request.financial(syminfo.tickerid, "LONG_TERM_DEBT_EXCL_CAPITAL_LEASE", "FQ")
MCapz0        = _LTD
MCapCalcz0    = MCapz0
uMz0          = ''

if(MCapz0 >= 1000 and MCapz0 < 1000000)
    MCapz0 := MCapz0/1000
    uMz0 := 'K'
if(MCapz0 >= 1000000 and MCapz0 < 1000000000)
    MCapz0 := MCapz0/1000000
    uMz0 := 'M'
if(MCapz0 >= 1000000000)
    MCapz0 := MCapz0/1000000000
    uMz0 := 'B'

//   Total assets	FQ, FY	TOTAL_ASSETS
_TR = request.financial(syminfo.tickerid, "TOTAL_REVENUE", "FQ")
MCapz1        = _TR
MCapCalcz1    = MCapz1
uMz1          = ''

if(MCapz1 >= 1000 and MCapz1 < 1000000)
    MCapz1 := MCapz1/1000
    uMz1 := 'K'
if(MCapz1 >= 1000000 and MCapz1 < 1000000000)
    MCapz1 := MCapz1/1000000
    uMz1 := 'M'
if(MCapz1 >= 1000000000)
    MCapz1 := MCapz1/1000000000
    uMz1 := 'B'

//   Total equity	FQ, FY	TOTAL_EQUITY
_TE = request.financial(syminfo.tickerid, "TOTAL_EQUITY", "FY")//
MCapz2        = _TE
MCapCalcz2    = MCapz2
uMz2          = ''

if(MCapz2 >= 1000 and MCapz2 < 1000000)
    MCapz2 := MCapz2/1000
    uMz2 := 'K'
if(MCapz2 >= 1000000 and MCapz2 < 1000000000)
    MCapz2 := MCapz2/1000000
    uMz2 := 'M'
if(MCapz2 >= 1000000000)
    MCapz2 := MCapz2/1000000000
    uMz2 := 'B'

//   Cash & equivalents	FQ, FY	CASH_N_EQUIVALENTS
_CNE = request.financial(syminfo.tickerid, "CASH_N_EQUIVALENTS", "FY")
MCapz3        = _CNE
MCapCalcz3    = MCapz3
uMz3          = ''

if(MCapz3 >= 1000 and MCapz3 < 1000000)
    MCapz3 := MCapz3/1000
    uMz3 := 'K'
if(MCapz3 >= 1000000 and MCapz3 < 1000000000)
    MCapz3 := MCapz3/1000000
    uMz3 := 'M'
if(MCapz3 >= 1000000000)
    MCapz3 := MCapz3/1000000000
    uMz3 := 'B'

//   Revenue estimates	FQ, FY	SALES_ESTIMATES
_RE = request.financial(syminfo.tickerid, "SALES_ESTIMATES", "FY")
MCapz4        = _RE
MCapCalcz4    = MCapz4
uMz4          = ''

if(MCapz4 >= 1000 and MCapz4 < 1000000)
    MCapz4 := MCapz4/1000
    uMz4 := 'K'
if(MCapz4 >= 1000000 and MCapz4 < 1000000000)
    MCapz4 := MCapz4/1000000
    uMz4 := 'M'
if(MCapz4 >= 1000000000)
    MCapz4 := MCapz4/1000000000
    uMz4 := 'B'

//   Free cash flow	FQ, FY, TTM	FREE_CASH_FLOW
_FCF = request.financial(syminfo.tickerid, "FREE_CASH_FLOW", "FY")
MCapz5        = _FCF
MCapCalcz5    = MCapz5
uMz5          = ''

if(MCapz5 >= 1000 and MCapz5 < 1000000)
    MCapz5 := MCapz5/1000
    uMz5 := 'K'
if(MCapz5 >= 1000000 and MCapz5 < 1000000000)
    MCapz5 := MCapz5/1000000
    uMz5 := 'M'
if(MCapz5 >= 1000000000)
    MCapz5 := MCapz5/1000000000
    uMz5 := 'B'

//   EBITDA	FQ, FY, TTM	EBITDA
_EB = request.financial(syminfo.tickerid, "EBITDA", "FY")
MCapz6        = _EB
MCapCalcz6    = MCapz6
uMz6          = ''

if(MCapz6 >= 1000 and MCapz6 < 1000000)
    MCapz6 := MCapz6/1000
    uMz6 := 'K'
if(MCapz6 >= 1000000 and MCapz6 < 1000000000)
    MCapz6 := MCapz6/1000000
    uMz6 := 'M'
if(MCapz6 >= 1000000000)
    MCapz6 := MCapz6/1000000000
    uMz6 := 'B'

//   MarketCa
//
// ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
// ————————————————————————————————— Table Layouts ————————————————————————————————————————————————————————————————————————
// ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
//

  
_fontSize   = switch _fontSize1
    
    "Normal"    => size.normal
    "Auto"      => size.auto
    "Tiny"      => size.tiny
    "Small"     => size.small
    "Large"     => size.large
    "Huge"      => size.huge


tableposGo2 = switch position3b
    
    "Top Right"     => position.top_right
    "Top Center"    => position.top_center
    "Top Left"      => position.top_left
    "Middle Right"  => position.middle_right
    "Middle Center" => position.middle_center
    "Middle Left"   => position.middle_left
    "Bottom Right"  => position.bottom_right
    "Bottom Center" => position.bottom_center
    "Bottom Left"   => position.bottom_left

if barstate.islast or barstate.islastconfirmedhistory
    var table tab = table.new(tableposGo2, 99, 99,
      bgcolor = color.rgb(255, 255, 255, 100),
      frame_color=color.rgb(255, 255, 255, 100),
      frame_width = 1,  
      border_color =color.rgb(255, 255, 255, 100),
      border_width = 1)

    if showxE
        table.cell(tab, 1, 24, "ʟᴏɴɢᴛᴇʀᴍ ᴅᴇʙᴛ \nᴇx-ᴄᴀᴘɪᴛᴀʟ ʟᴇᴀsᴇ ~ ғǫ: ",text_halign = text.align_left, text_color = color.white, text_size = _fontSize)
        table.cell(tab, 2, 24, str.tostring(math.round(MCapz0,2),'0.000')+uMz0, text_color = color.rgb(255, 0, 0), bgcolor =  color.rgb(24, 24, 24,100),text_halign = text.align_right,text_size = _fontSize)

        table.cell(tab, 1, 25, "ᴛᴏᴛᴀʟ ʀᴇᴠᴇɴᴜᴇ ~ ғǫ: ",text_halign = text.align_left, text_color = color.white,text_size = _fontSize)
        table.cell(tab, 2, 25, str.tostring(math.round(MCapz1,2),'0.000')+uMz1, text_color = color.rgb(9, 255, 0), bgcolor =  color.rgb(24, 24, 24,100),text_halign = text.align_right,text_size = _fontSize)

        table.cell(tab, 1, 26, "ᴛᴏᴛᴀʟ ᴇǫᴜɪᴛʏ ~ ғʏ: ",text_halign = text.align_left, text_color = color.white,text_size = _fontSize)
        table.cell(tab, 2, 26, str.tostring(math.round(MCapz2,2),'00.000')+uMz2, text_color = color.rgb(9, 255, 0), bgcolor =  color.rgb(24, 24, 24,100),text_halign = text.align_right,text_size = _fontSize)

        table.cell(tab, 1, 27, "ᴄᴀsʜ & ᴇǫᴜɪᴠᴀʟᴇɴᴛs ~ ғʏ: ",text_halign = text.align_left, text_color = color.white,text_size = _fontSize)
        table.cell(tab, 2, 27, str.tostring(math.round(MCapz3,2),'0.000')+uMz3, text_color = color.rgb(9, 255, 0), bgcolor =  color.rgb(24, 24, 24,100),text_halign = text.align_right,text_size = _fontSize)

        table.cell(tab, 1, 28, "ʀᴇᴠᴇɴᴜᴇ ᴇsᴛɪᴍᴀᴛᴇs ~ ғʏ: ",text_halign = text.align_left, text_color = color.white,text_size = _fontSize)
        table.cell(tab, 2, 28, str.tostring(math.round(MCapz4,2),'00.000')+uMz4, text_color = color.rgb(9, 255, 0), bgcolor =  color.rgb(24, 24, 24,100),text_halign = text.align_right,text_size = _fontSize)

        table.cell(tab, 1, 29, "ғʀᴇᴇ ᴄᴀsʜ ғʟᴏᴡs ~ ғʏ: ",text_halign = text.align_left, text_color = color.white,text_size = _fontSize)
        table.cell(tab, 2, 29, str.tostring(math.round(MCapz5,2),'0.000')+uMz5, text_color = color.rgb(9, 255, 0), bgcolor =  color.rgb(24, 24, 24,100),text_halign = text.align_right,text_size = _fontSize)

        table.cell(tab, 1, 30, "ᴇʙɪᴛᴅᴀ ~ ғʏ: ",text_halign = text.align_left, text_color = color.white,text_size = _fontSize)
        table.cell(tab, 2, 30, str.tostring(math.round(MCapz6,3),'0.000')+uMz6, text_color = color.rgb(255, 145, 0), bgcolor =  color.rgb(24, 24, 24,100),text_halign = text.align_right,text_size = _fontSize)

        table.cell(tab, 1, 31, "ᴍᴀʀᴋᴇᴛᴄᴀᴘ: ",text_halign = text.align_left, text_color = color.white,text_size = _fontSize)
        table.cell(tab, 2, 31, str.tostring(math.round(MCapz,2),'00.000')+uMz, text_color = color.rgb(255, 145, 0), bgcolor =  color.rgb(24, 24, 24,100),text_halign = text.align_right,text_size = _fontSize)



