BearMetrics
// 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("BearMetrics", 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 ᴛᴀʙʟᴇ   ═════════════════════')

showxD       = input.bool(defval = false, title = "🏦 Show Bankrupcy Potential Scored & Factors: ", group='Toggle Stats', inline='2')


showxA       = input.bool(defval = true, title = "🧮 Show Key Fundamental Metrics",
  group='Toggle Stats', inline='1')


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"],
  group='Toggle Stats', inline='3')
//
// ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
// —————————————————————————————————   Shares outstanding calcs   —————————————————————————————————————————————————————————
// ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
//
TSO         = request.financial(s, 'TOTAL_SHARES_OUTSTANDING', 'FQ', ignore_invalid_symbol=true)
MCap        = TSO
MCapCalc    = MCap
uM          = ''

if(MCap >= 1000 and MCap < 1000000)
    MCap := MCap/1000
    uM := 'K'
if(MCap >= 1000000 and MCap < 1000000000)
    MCap := MCap/1000000
    uM := 'M'
if(MCap >= 1000000000)
    MCap := MCap/1000000000
    uM := 'B'
//
// ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
// —————————————————————————————————   Float Calcs  ———————————————————————————————————————————————————————————————————————
// ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
//
FSO             = request.financial(s, 'FLOAT_SHARES_OUTSTANDING', 'FY', ignore_invalid_symbol=true)
FFMCap          = FSO * closeDa
floatPer        = FFMCap/MCapCalc*100

_sN             = syminfo.ticker
_alt            = request.financial(syminfo.tickerid, "ALTMAN_Z_SCORE", "FY")
_gram           = request.financial(syminfo.tickerid, "GRAHAM_NUMBERS", "FY")
_pk             = request.financial(syminfo.tickerid, "PIOTROSKI_F_SCORE", "FQ")
_ff             = request.financial(syminfo.tickerid, "FULMER_H_FACTOR", "FY")//
_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'

///
// ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
// —————————————————————————————————  Pull in Data   ——————————————————————————————————————————————————————————————————————
// ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
//
finra_fnra              = 'QUANDL:FINRA/FNRA_' + syminfo.ticker
finra_fnyx              = 'QUANDL:FINRA/FNYX_' + syminfo.ticker
finra_fnsq              = 'QUANDL:FINRA/FNSQ_' + syminfo.ticker
bats_bats               = 'QUANDL:BATS/BATS_'  + syminfo.ticker
bats_byxx               = 'QUANDL:BATS/BYXX_'  + syminfo.ticker
bats_edga               = 'QUANDL:BATS/EDGA_'  + syminfo.ticker
bats_edgx               = 'QUANDL:BATS/EDGX_'  + syminfo.ticker
sector                  = syminfo.sector
industry                = syminfo.industry

_TSO = request.financial(syminfo.tickerid, "TOTAL_SHARES_OUTSTANDING", "FQ")
MarketCap = _TSO * close
/////
total_short_interest    = request.security(finra_fnyx, 'D', 
  close, barmerge.gaps_on) + request.security(finra_fnsq, 'D', close, barmerge.gaps_on)

///
//To calculate the short interest ratio, divide the number of shorted shares by the average daily trading volume. 
//The number tells you how many days it would take investors to close out short positions on the open market. 
//When the short interest ratio is high, it suggests investors are bearish about a stock.
//
// ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
// —————————————————————————————————  Colors library     ——————————————————————————————————————————————————————————————————
// ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
//
import kaigouthro/hsvColor/15 as HSV

var colArr = array.new_color()

if barstate.isfirst
    for i = 0 to 5
        colX = HSV.hsv_gradient(i, 0, 5, color.lime, color.white)
        colArr.push(colX)

percent              = 1 - ((ta.accdist - math.sum(ta.accdist, 252) / 252) / ta.accdist)
col                  = switch ta.accdist > ta.sma(ta.accdist, 21)
    
    true =>   color.lime
    =>        color.red
//
// ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
// —————————————————————————————————  Colors  calcs   —————————————————————————————————————————————————————————————————————
// ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
//
_colorAlt       = _alt > 3 ?            color.rgb(0, 255, 8) : color.rgb(255, 0, 0)
_colorgm        = close > _gram ?       color.rgb(0, 255, 8) : color.rgb(255, 0, 0)
_colorpk        = _pk >=1 ?             color.rgb(0, 255, 8) : color.rgb(255, 0, 0)
_textff         = _ff >0 ?              'ᴘᴀss' : 'ғᴀɪʟ'  
_colorff        = _ff >0 ?              color.rgb(0, 255, 8) : color.rgb(255, 0, 0)
_colorchgD      =_dChg > 0 ?            color.rgb(0, 255, 8) : color.rgb(255, 0, 0)
_colorchgW      =_wkChg > 0 ?           color.rgb(0, 255, 8) : color.rgb(255, 0, 0)

//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 ————————————————————————————————————————————————————————————————————————
// ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
//
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 showxD
        table.cell(tab, 1, 13, "  ",text_halign = text.align_left, text_color = color.white)
        table.cell(tab, 1, 19, "ᴀʟᴛᴍᴀɴ ᴢ-sᴄᴏʀᴇ: ",text_halign = text.align_left, text_color = color.white)
        table.cell(tab, 2, 19, str.tostring(_alt,'0.00'), text_color = _colorAlt,  text_halign = text.align_left)

        table.cell(tab, 1, 20, "ɢʀᴀʜᴀᴍs ɴᴏ: ",text_halign = text.align_left, text_color = color.white)
        table.cell(tab, 2, 20, str.tostring(_gram,'0.0'), text_color = _colorgm,  text_halign = text.align_left)

        table.cell(tab, 1, 21, "ᴘɪᴏᴛʀᴏsᴋɪ ғ-sᴄᴏʀᴇ: ",text_halign = text.align_left, text_color = color.white)
        table.cell(tab, 2, 21, str.tostring(_pk, '0.0'), text_color = _colorpk,  text_halign = text.align_left)

        table.cell(tab, 1, 22, "ғᴜʟᴍᴇʀ ғᴀᴄᴛᴏʀ: ",text_halign = text.align_left, text_color = color.white)
        table.cell(tab, 2, 22, _textff , text_color = _colorff, text_halign = text.align_left, text_size=size.normal)
        table.cell(tab, 1, 23, "  ",text_halign = text.align_left, text_color = color.white)

    if showxA
        table.cell(tab, 1, 24, "ʟᴏɴɢᴛᴇʀᴍ ᴅᴇʙᴛ \nᴇx-ᴄᴀᴘɪᴛᴀʟ ʟᴇᴀsᴇ ~ ғǫ: ",text_halign = text.align_left, text_color = color.white)
        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_center)

        table.cell(tab, 1, 25, "ᴛᴏᴛᴀʟ ʀᴇᴠᴇɴᴜᴇ ~ ғǫ: ",text_halign = text.align_left, text_color = color.white)
        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_center)

        table.cell(tab, 1, 26, "ᴛᴏᴛᴀʟ ᴇǫᴜɪᴛʏ ~ ғʏ: ",text_halign = text.align_left, text_color = color.white)
        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_center)

        table.cell(tab, 1, 27, "ᴄᴀsʜ & ᴇǫᴜɪᴠᴀʟᴇɴᴛs ~ ғʏ: ",text_halign = text.align_left, text_color = color.white)
        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_center)

        table.cell(tab, 1, 28, "ʀᴇᴠᴇɴᴜᴇ ᴇsᴛɪᴍᴀᴛᴇs ~ ғʏ: ",text_halign = text.align_left, text_color = color.white)
        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_center)

        table.cell(tab, 1, 29, "ғʀᴇᴇ ᴄᴀsʜ ғʟᴏᴡs ~ ғʏ: ",text_halign = text.align_left, text_color = color.white)
        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_center)

        table.cell(tab, 1, 30, "ᴇʙɪᴛᴅᴀ ~ ғʏ: ",text_halign = text.align_left, text_color = color.white)
        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_center)

        table.cell(tab, 1, 31, "ᴍᴀʀᴋᴇᴛᴄᴀᴘ: ",text_halign = text.align_left, text_color = color.white)
        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_center)












