Financial Statement Indicator by zdmre
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © zdmre

//@version=5
indicator('Financial Statement Indicator by zdmre', format=format.volume)

period = input.string('FY', 'Financial Period', options=['FQ', 'FY'], tooltip='FQ: Financial Quarter\nFY: Financial Year')
plotdata = input.string('EPS', 'Plot Data', options=['Revenue', 'Net Margin', 'EPS', 'EPS-D', 'P/E'])
int datasize = input.int(8, 'Data size', minval=2)+2
opt_textsize = input.string(size.small, 'Text Size', options=[size.auto, size.tiny, size.small, size.normal, size.large, size.huge], group='table')
opt_textcolor = input(color.white, "Text Color", tooltip='Title')
opt_datatextcolor = input(color.black, "Text Color", tooltip='Data')
opt_panelbgcolor = input(color.blue, "Background Color", tooltip='Panel')
opt_cellbgcolor = input(color.orange, "Cell Background Color", tooltip='Non-filtered Cell')
is_opt_pos = input.string(position.middle_left, 'Table \nPosition\n #1', options= [position.top_left, position.top_center, position.top_right, position.middle_left, position.middle_center, position.middle_right, position.bottom_left, position.bottom_center, position.bottom_right], group='table')
t_1 = input.bool(true, title = "TABLE-1 Income Statement",  group = "table")
t_2 = input.bool(true, title = "TABLE-2 Balance Sheet",  group = "table")
t_3 = input.bool(true, title = "TABLE-3 Statistics & Cash Flow",  group = "table")
t_4 = input.bool(true, title = "TABLE-4 Statistics (Others)", group = "table")

//Filtering
colorGroup = 'Filtering Data by Color'
showValueFilter = input(true, title='Enabled', group = colorGroup , tooltip='if enabled, tables colored with below data. Otherwise, with periods.')
m_val = input.float(15, title = 'Net/Op Margin Filter', group=colorGroup, tooltip='Net Margin & Operating Margin - Income Statement - Table 1.\n\nIIf NM/OM > value, cell to green; else cell to red\ndefval=5')
//Table-1
epsval = input.float(0, title = 'EPS', group=colorGroup, tooltip='Earning per Share value & EPS Estimated - Income Statement - Table 1.\n\nIIf EPS > value, cell to green; else cell to red\ndefval=0')
peval = input.float(15, title = 'P/E', group=colorGroup, tooltip='Price to Earnings Ratio value (FY) - Income Statement - Table 1.\n\nIIf 0 < P/E < value, cell to green; else cell to red \ndefval=15')
//Table-2
deval = input.float(0.5, title = 'D/E', group=colorGroup, tooltip='Debt/Equity Ratio value - Balance Sheet - Table 2.\n\nIIf D/E < value, cell to green; else cell to red \ndefval=0.5')
pbval = input.float(3, title = 'P/B', group=colorGroup, tooltip='Price to Book Value - Balance Sheet -Table 2.\n\nIIf 0 < P/B < value, cell to green; else cell to red \ndefval=3 ')
//Table-3
roeval = input.float(10, title = 'ROE', group=colorGroup, tooltip='Return on Equity value - Statistics - Table 3.\n\nIIf ROE > value, cell to green; else cell to red \ndefval=10 ')
roepbval = input.float(3, title = 'ROE/PB', group=colorGroup, tooltip='Return on Equity / Price to Book Ratio - Balance Sheet -Table 2.\n\nIIf ROE/PB > value, cell to green; else cell to red \ndefval=3 ')
roaval = input.float(5, title = 'ROA', group=colorGroup, tooltip='Return on Asset value - Statistics - Table 3.\n\nIIf ROA > value, cell to green; else cell to red \ndefval=5')
roicval = input.float(7, title = 'ROIC', group=colorGroup, tooltip='Return on Investment Capital value - Statistics - Table 3.\n\nIIf ROIC > value, cell to green; else cell to red \ndefval=7')
qrval = input.float(1, title = 'QR', group=colorGroup, tooltip='Quick Ratio value - Statistics - Table 3.\n\nIIf QR > value, cell to green; else cell to red \ndefval=1')
//Table-4
dyieldval = input.float(2, title = 'Dividend Yield', group=colorGroup, tooltip='Dividend Yield - Statistics - Table 4.\n\nIf Dividend Yield < value, cell to red; else cell to green\ndefval=2')
pegval = input.float(1, title = 'PEG', group=colorGroup, tooltip='PEG - Statistics - Table 4.\n\nIf PEG Yield > value, cell to red; else cell to green\ndefval=1 ')
peforw = input.float(15, title = 'P/E Forward', group=colorGroup, tooltip='P/E Forward - Statistics - Table 4.\n\nIf P/E Forward < value, cell to red; else cell to green\ndefval=15')
sgr = input.float(5, title = 'SGR', group=colorGroup, tooltip='Sustainable Growth Rate - Statistics - Table 4.\n\nIf SGR < value, cell to red; else cell to green\ndefval=5')
altmanval = input.float(1.8, title = 'Altman Z-Score', group=colorGroup, tooltip='Altman Z-Score - Statistics - Table 4.\n\nIf Altman < value, cell to red; else cell to green\ndefval=1.8 ')
beneishval = input.float(-1.78, title = 'Beneish Model', group=colorGroup, tooltip='Beneish Model - Statistics - Table 4.\n\nIf Beneish < value, cell to green; else cell to red\ndefval=-1.78 ')
springateval = input.float(0.862, title = 'Springate Score', group=colorGroup, tooltip='Springate Score - Statistics - Table 4.\n\nIf Springate < value, cell to red; else cell to green\ndefval=0.862 ')
zmijval = input.float(0.5, title = 'Zmijewski Score', group=colorGroup, tooltip='Zmijewski Score - Statistics - Table 4.\n\nIf Zmijewski < value, cell to red; else cell to green\ndefval=0.5 ')

//Timestamp
get_Fin_data(_finID, _period) =>
    request.financial(symbol=syminfo.tickerid, financial_id=_finID, period=_period,  gaps=barmerge.gaps_on, ignore_invalid_symbol=true)
symtemp ="ESD_FACTSET:" + syminfo.prefix + ";" + syminfo.ticker
earningsPublishTimeYear = request.security(symtemp + ";EARNINGS", "3M", time, lookahead=barmerge.lookahead_on)

// Income Statement
eps = get_Fin_data('EARNINGS_PER_SHARE_BASIC', period)
opmar = get_Fin_data('OPERATING_MARGIN', period)
netmar = get_Fin_data('NET_MARGIN',period)
pe = close/get_Fin_data('EARNINGS_PER_SHARE_BASIC', "TTM")
revenue = get_Fin_data('TOTAL_REVENUE', period)
epsest = get_Fin_data('EARNINGS_ESTIMATE', period)

// Balance Sheet
curasst = get_Fin_data('TOTAL_CURRENT_ASSETS', period)  // bs01
ttasst = get_Fin_data('TOTAL_ASSETS', period)  // bs02
tteq = get_Fin_data('TOTAL_EQUITY', period)  // bs03
pb = close / get_Fin_data('BOOK_VALUE_PER_SHARE', period)  // bs05
tdebt = get_Fin_data('TOTAL_DEBT', period)
de = tdebt/tteq

//  Statistics & Cash Flow
roe = get_Fin_data('RETURN_ON_EQUITY', period)
roepb = get_Fin_data('RETURN_ON_EQUITY_ADJUST_TO_BOOK', period)
roa = get_Fin_data('RETURN_ON_ASSETS', period)
qr = get_Fin_data('QUICK_RATIO', period)
roic = get_Fin_data('RETURN_ON_INVESTED_CAPITAL', period)
fcf = get_Fin_data('FREE_CASH_FLOW', period)

// Statistics (Others)
altman = get_Fin_data('ALTMAN_Z_SCORE', period)
beneish = get_Fin_data('BENEISH_M_SCORE', period)
dpayout = get_Fin_data('DIVIDEND_PAYOUT_RATIO', period)
dyield = get_Fin_data('DIVIDENDS_YIELD', period)
graham = get_Fin_data('GRAHAM_NUMBERS', period)
piotroski = get_Fin_data('PIOTROSKI_F_SCORE', period)
peg = get_Fin_data('PEG_RATIO', period)
peforward = get_Fin_data('PRICE_EARNINGS_FORWARD', period)
springate = get_Fin_data('SPRINGATE_SCORE', period)
sgrowthrate = get_Fin_data('SUSTAINABLE_GROWTH_RATE', period)
zmij = get_Fin_data('ZMIJEWSKI_SCORE', period)

datatoplot = plotdata == 'EPS' ? eps : plotdata == 'Revenue' ? revenue  : plotdata == 'Net Margin' ?  netmar : plotdata == 'P/E' ? pe   : na

//Table-1
var date = array.new_int(datasize)  // Date (year)
var datem_ = array.new_int(datasize)  // Date (month)
var is01 = array.new_float(datasize)  // Revenue
var is02 = array.new_float(datasize)  // Net margin
var is03 = array.new_float(datasize)  // EPS
var is04 = array.new_float(datasize) //Operating margin
var is05 = array.new_float(datasize) //P/E Ratio
var is06 = array.new_float(datasize) // EPS Estimate
//Table-2
var bs01 = array.new_float(datasize)  // Current Asset
var bs02 = array.new_float(datasize)  // Total Asset
var bs03 = array.new_float(datasize)  // Total Equity
var bs04 = array.new_float(datasize)  // Price to Book
var bs05 = array.new_float(datasize) // Total Debt
var bs06 = array.new_float(datasize) // D/E Ratio
var bs07 = array.new_float(datasize) // BETA
//Table-3
var cf01 = array.new_float(datasize)  // roe
var cf02 = array.new_float(datasize)  // roa
var cf03 = array.new_float(datasize)  // roic
var cf04 = array.new_float(datasize)  // qr
var cf05 = array.new_float(datasize)  // fcf
var cf06 = array.new_float(datasize) // roe/pb
//Table-4
var st01 = array.new_float(datasize)  // altman
var st02 = array.new_float(datasize)  // beneish
var st03 = array.new_float(datasize)  // dpayout
var st04 = array.new_float(datasize)  // dyield
var st06 = array.new_float(datasize) // graham
var st07 = array.new_float(datasize) // piotroski
var st08 = array.new_float(datasize) // peforward
var st09 = array.new_float(datasize) // springate
var st10 = array.new_float(datasize) // sgrowthrate
var st11 = array.new_float(datasize) // zmij
var st12 = array.new_float(datasize) //peg
    
// BETA Calc
symbolMarket = input.symbol("NDX", "Beta Benchmark")
lengthbeta = input(title = "Beta Period", defval=252)
symov = request.security(symbolMarket, 'D', close)
retass = ((close - close[1])/close)
retsym = ((symov - symov[1])/symov)
asstd = ta.stdev(retass, lengthbeta)
marktstd = ta.stdev(retsym, lengthbeta)
Beta = ta.correlation(retass, retsym, lengthbeta) * asstd / marktstd

if not na(datatoplot)
    label.new(bar_index, datatoplot,  text=str.tostring(plotdata == 'EPS' ? datatoplot : plotdata == 'Revenue' ? datatoplot / 1000000 : plotdata == 'Net Margin' ? datatoplot : datatoplot, plotdata == 'EPS' ? '#,##0.000' : plotdata == 'Revenue' ? '#,##0' + ' M' : plotdata == 'Net Income' ? '#,##0' : '#,##0.000'),size=opt_textsize, textcolor=color.white)
    array.push(date, earningsPublishTimeYear)
    array.push(datem_, time)
    array.push(is01, revenue)
    array.push(is02, netmar)
    array.push(is03, eps)
    array.push(is04, opmar)
    array.push(is05, pe)
    array.push(is06, epsest)
    array.shift(date)
    array.shift(datem_) 
    array.shift(is01) 
    array.shift(is02) 
    array.shift(is03) 
    array.shift(is04)
    array.shift(is05)
    array.shift(is06)
// Table-2 
    array.push(bs01, curasst)
    array.push(bs02, ttasst)
    array.push(bs03, tteq)
    array.push(bs04, pb)
    array.push(bs05, tdebt)
    array.push(bs06, de)
    array.push(bs07, Beta)
    array.shift(bs01)
    array.shift(bs02)
    array.shift(bs03)
    array.shift(bs04)
    array.shift(bs05)
    array.shift(bs06)
    array.shift(bs07)
// Table-3
    array.push(cf01, roe)
    array.push(cf02, roa)
    array.push(cf03, roic)
    array.push(cf04, qr)
    array.push(cf05, fcf)
    array.push(cf06, roepb)
    array.shift(cf01)
    array.shift(cf02)
    array.shift(cf03)
    array.shift(cf04)
    array.shift(cf05)
    array.shift(cf06)
// Table-4    
    array.push(st01, altman)
    array.push(st02, beneish)
    array.push(st03, dpayout)
    array.push(st04, dyield)
    array.push(st06, graham)
    array.push(st07, piotroski)
    array.push(st08, peforward)
    array.push(st09, springate)
    array.push(st10, sgrowthrate)
    array.push(st11, zmij)
    array.push(st12, peg)
    array.shift(st01)
    array.shift(st02)
    array.shift(st03)
    array.shift(st04)
    array.shift(st06)
    array.shift(st07)
    array.shift(st08)
    array.shift(st09)
    array.shift(st10)
    array.shift(st11)
    array.shift(st12)

    
//Table with Datas
var tbis = table.new(is_opt_pos, 35, datasize, frame_color=color.black, frame_width=1, border_width=1, border_color=color.black)
headerColor = color.new(color.blue, 20)
if barstate.isfirst
    table.cell(tbis, 0, 0, text = "", bgcolor = headerColor)
    table.merge_cells(tbis, 0, 0, 1, 0)
    if t_1
        table.cell(tbis, 2, 0, text = "Income Statement", bgcolor = headerColor, text_color=color.white)
        table.merge_cells(tbis, 2, 0, 7, 0)
    if t_2
        table.cell(tbis, 9, 0, text = "Balance Sheet", bgcolor = headerColor, text_color=color.white)
        table.merge_cells(tbis, 9, 0, 15, 0)
    if t_3
        table.cell(tbis, 17, 0, text = "Statistics & Cash Flow", bgcolor = headerColor, text_color=color.white)
        table.merge_cells(tbis, 17, 0, 22, 0)
    if t_4
        table.cell(tbis, 24, 0, text = "Statistics (Others)", bgcolor = headerColor,  text_color=color.white)
        table.merge_cells(tbis, 24, 0, 34, 0)  
    

if barstate.islast and t_1 and ( t_2 or t_3 or t_4 )
    table.cell(tbis, 8, 0, '', bgcolor = color.black)
    table.merge_cells(tbis, 8, 0, 8, datasize-1)
if barstate.islast and  t_2 and (t_3 or t_4)
    table.cell(tbis, 16, 0, '', bgcolor = color.black)
    table.merge_cells(tbis, 16, 0, 16, datasize-1)
if barstate.islast and  t_3 and t_4
    table.cell(tbis, 23, 0, '', bgcolor = color.black)
    table.merge_cells(tbis, 23, 0, 23, datasize-1)

if barstate.islast and t_1
    table.cell(tbis, 0, 1, '', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor)
    table.cell(tbis, 1, 1, 'Period', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor)
    table.cell(tbis, 2, 1, 'Revenue', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor)
    table.cell(tbis, 3, 1, 'Net\nMargin %', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor)
    table.cell(tbis, 4, 1, 'Operating\nMargin %', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor)
    table.cell(tbis, 5, 1, 'EPS', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor, tooltip="Control Value: "+str.tostring(epsval))
    table.cell(tbis, 6, 1, 'EPS\nEst', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor, tooltip="Control Value: "+str.tostring(epsval))
    table.cell(tbis, 7, 1, 'P/E', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor, tooltip="Control Value: "+str.tostring(peval))
if barstate.islast and t_2
    table.cell(tbis, 0, 1, '', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor)
    table.cell(tbis, 1, 1, 'Period', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor)
    table.cell(tbis, 9, 1, 'Current \nAsset', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor)
    table.cell(tbis, 10, 1, 'Total \nAsset', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor)
    table.cell(tbis, 11, 1, 'Total \nEquity', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor)
    table.cell(tbis, 12, 1, 'P/B', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor, tooltip="Control Value: "+str.tostring(pbval))
    table.cell(tbis, 13, 1, 'Total\nDebt', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor)
    table.cell(tbis, 14, 1, 'D/E', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor, tooltip="Control Value: "+str.tostring(deval))
    table.cell(tbis, 15, 1, 'β', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor, tooltip="Control Value: 1")
if barstate.islast and t_3
    table.cell(tbis, 0, 1, '', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor)
    table.cell(tbis, 1, 1, 'Period', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor)
    table.cell(tbis, 17, 1, 'ROE', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor, tooltip="Control Value: "+str.tostring(roeval))
    table.cell(tbis, 18, 1, 'ROE/\nPB', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor, tooltip="Control Value: "+str.tostring(roepbval))
    table.cell(tbis, 19, 1, 'ROA', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor, tooltip="Control Value: "+str.tostring(roaval))
    table.cell(tbis, 20, 1, 'ROIC', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor, tooltip="Control Value: "+str.tostring(roicval))
    table.cell(tbis, 21, 1, 'Quick\nRatio', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor, tooltip="Control Value: "+str.tostring(qrval))
    table.cell(tbis, 22, 1, 'Free \nCash Flow', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor)
if barstate.islast and t_4 
    table.cell(tbis, 0, 1, '', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor)
    table.cell(tbis, 1, 1, 'Period', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor)
    table.cell(tbis, 24, 1, 'Dividend\nPayout', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor)
    table.cell(tbis, 25, 1, 'Dividend\nYield', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor, tooltip="Control Value: "+str.tostring(dyieldval))
    table.cell(tbis, 26, 1, 'PEG', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor, tooltip="Control Value: "+str.tostring(pegval))
    table.cell(tbis, 27, 1, 'P/E\nForward', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor, tooltip="Control Value: "+str.tostring(peforw))
    table.cell(tbis, 28, 1, 'SGR', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor, tooltip="Control Value: "+str.tostring(sgr))
    table.cell(tbis, 29, 1, 'Altman', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor, tooltip="Control Value: "+str.tostring(altmanval))
    table.cell(tbis, 30, 1, 'Beneish', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor, tooltip="Control Value: "+str.tostring(beneishval))
    table.cell(tbis, 31, 1, 'Graham', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor)
    table.cell(tbis, 32, 1, 'Piotroski', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor)
    table.cell(tbis, 33, 1, 'Springate', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor, tooltip="Control Value: "+str.tostring(springateval))
    table.cell(tbis, 34, 1, 'Zmijewski', bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor, tooltip="Control Value: "+str.tostring(zmijval))
  
for i = 2 to datasize-1 by 1
    if barstate.islast and (t_1 or t_2 or t_3 or t_4)
        table.cell(tbis, 0, i, str.tostring(i), bgcolor = opt_panelbgcolor, text_size=opt_textsize)
        table.cell(tbis, 1, i, (str.format('{0, date, Y}',  array.get(date, i))+str.format('{0, date, MMM}',  array.get(datem_, i))), bgcolor = opt_panelbgcolor, text_size=opt_textsize, text_color=opt_textcolor)
    if barstate.islast and t_1
        table.cell(tbis, 2, i, str.tostring(array.get(is01, i) / 1000000, '#,##0' + 'M'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(is01, i)) ?  opt_datatextcolor : array.get(is01, i) <= array.get(is01, i - 1) ? #FF6961 : color.lime) // Revenue
        table.cell(tbis, 3, i, str.tostring(array.get(is02, i) / 1, '#,##0')+'%', text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(is02, i)) ?  opt_datatextcolor : array.get(is02, i) <= array.get(is02, i - 1) ? #FF6961 : color.lime) // Net Margin
        table.cell(tbis, 4, i, str.tostring(array.get(is04, i) / 1, '#,##0')+'%', text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(is04, i)) ?  opt_datatextcolor : array.get(is04, i) <= array.get(is04, i - 1) ? #FF6961 : color.lime) // Operating Margin
        table.cell(tbis, 5, i, str.tostring(array.get(is03, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(is03, i)) ?  opt_datatextcolor : array.get(is03, i) <= array.get(is03, i - 1) ? #FF6961 : color.lime) // EPS
        table.cell(tbis, 6, i, str.tostring(array.get(is06, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(is06, i)) ?  opt_datatextcolor : array.get(is06, i) <= array.get(is06, i - 1) ? #FF6961 : color.lime) // EPS est
        table.cell(tbis, 7, i, str.tostring(array.get(is05, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(is05, i)) ?  opt_datatextcolor : array.get(is05, i) >= array.get(is05, i - 1) ?  #FF6961 : array.get(is05, i) <= 0 ? #FF6961 : color.lime) //P/E Ratio
    if barstate.islast and showValueFilter and t_1
        table.cell(tbis, 3, i, str.tostring(array.get(is02, i) / 1, '#,##0')+'%', text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(is02, i)) ?  opt_datatextcolor : array.get(is02, i) <= m_val ? opt_cellbgcolor : color.lime) // Net Margin
        table.cell(tbis, 4, i, str.tostring(array.get(is04, i) / 1, '#,##0')+'%', text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(is04, i)) ?  opt_datatextcolor : array.get(is04, i) <= m_val ? opt_cellbgcolor : color.lime) // Operating Margin
        table.cell(tbis, 5, i, str.tostring(array.get(is03, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(is03, i)) ?  opt_datatextcolor : array.get(is03, i) <= epsval ?  #FF6961 : color.lime) // EPS
        table.cell(tbis, 6, i, str.tostring(array.get(is06, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(is06, i)) ?  opt_datatextcolor : array.get(is06, i) <= epsval ?  #FF6961 : color.lime) // EPS est
        table.cell(tbis, 7, i, str.tostring(array.get(is05, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(is05, i)) ?  opt_datatextcolor : array.get(is05, i) >= peval ?  #FF6961 : array.get(is05, i) <= 0 ?  #FF6961 : color.lime) //P/E Ratio
        //Non-filtered
        table.cell(tbis, 2, i, str.tostring(array.get(is01, i) / 1000000, '#,##0' + 'M'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(is01, i)) ?  opt_datatextcolor : array.get(is01, i) <= array.get(is01, i - 1) ? opt_cellbgcolor : color.lime) // Revenue || NON-FILTERED
    if barstate.islast and t_2
        table.cell(tbis, 9, i, str.tostring(array.get(bs01, i) / 1000000, '#,##0' + 'M'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(bs01, i)) ?  opt_datatextcolor : array.get(bs01, i) <= array.get(bs01, i - 1) ? #FF6961 : color.lime)  // Current Asset
        table.cell(tbis, 10, i, str.tostring(array.get(bs02, i) / 1000000, '#,##0' + 'M'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(bs02, i)) ?  opt_datatextcolor : array.get(bs02, i) <= array.get(bs02, i - 1) ? #FF6961 : color.lime) // Total Asset
        table.cell(tbis, 11, i, str.tostring(array.get(bs03, i)/ 1000000, '#,##0' + 'M'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(bs03, i)) ?  opt_datatextcolor : array.get(bs03, i) <= array.get(bs03, i - 1) ? #FF6961 : color.lime) // Total Equity
        table.cell(tbis, 12, i, str.tostring(array.get(bs04, i), '#,##0.0'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(bs04, i)) ?  opt_datatextcolor : array.get(bs04, i) >= array.get(bs04, i - 1) ? #FF6961 : array.get(bs04, i) <= 0 ? #FF6961 : color.lime) // Price to Book
        table.cell(tbis, 13, i, str.tostring(array.get(bs05, i)/ 1000000, '#,##0' + 'M'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(bs05, i)) ?  opt_datatextcolor : array.get(bs05, i) >= array.get(bs05, i - 1) ? #FF6961  : color.lime) // Total Debt
        table.cell(tbis, 14, i, str.tostring(array.get(bs06, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(bs06, i)) ?  opt_datatextcolor : array.get(bs06, i) >= array.get(bs06, i - 1) ? #FF6961 : array.get(bs06, i) < 0 ? #FF6961 : color.lime) // D/E Ratio
        table.cell(tbis, 15, i, str.tostring(array.get(bs07, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(bs07, i)) ?  opt_datatextcolor : math.abs(array.get(bs07, i)) >= math.abs(array.get(bs07, i - 1)) ? #d14dff :  #00ffff ) // BETA
    if barstate.islast and showValueFilter and t_2
        table.cell(tbis, 12, i, str.tostring(array.get(bs04, i), '#,##0.0'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(bs04, i)) ?  opt_datatextcolor : array.get(bs04, i) >= pbval ? #FF6961 : array.get(bs04, i) <= 0 ? #FF6961 : color.lime) // Price to Book
        table.cell(tbis, 14, i, str.tostring(array.get(bs06, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(bs06, i)) ?  opt_datatextcolor : array.get(bs06, i) >= deval ? #FF6961 : array.get(bs06, i) < 0 ? #FF6961 : color.lime) // D/E Ratio
        table.cell(tbis, 15, i, str.tostring(array.get(bs07, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(bs07, i)) ?  opt_datatextcolor : math.abs(array.get(bs07, i)) >= 1 ? #d14dff : #00ffff ) // Beta
        //Non-filtered
        table.cell(tbis, 9, i, str.tostring(array.get(bs01, i) / 1000000, '#,##0' + 'M'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=array.get(bs01, i) <= array.get(bs01, i - 1) ? opt_cellbgcolor : color.lime)  // Current Asset || NON-FILTERED
        table.cell(tbis, 10, i, str.tostring(array.get(bs02, i) / 1000000, '#,##0' + 'M'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=array.get(bs02, i) <= array.get(bs02, i - 1) ? opt_cellbgcolor : color.lime) // Total Asset || NON-FILTERED
        table.cell(tbis, 11, i, str.tostring(array.get(bs03, i)/ 1000000, '#,##0' + 'M'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=array.get(bs03, i) <= array.get(bs03, i - 1) ? opt_cellbgcolor : color.lime) // Total Equity || NON-FILTERED
        table.cell(tbis, 13, i, str.tostring(array.get(bs05, i)/ 1000000, '#,##0' + 'M'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=array.get(bs05, i) >= array.get(bs05, i - 1) ? opt_cellbgcolor  : color.lime) // Total Debt || NON-FILTERED
    if barstate.islast and t_3
        table.cell(tbis, 17, i, str.tostring(array.get(cf01, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(cf01, i)) ?  opt_datatextcolor : array.get(cf01, i) <= array.get(cf01, i - 1) ? #FF6961 : color.lime) // roe
        table.cell(tbis, 18, i, str.tostring(array.get(cf06, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(cf06, i)) ?  opt_datatextcolor : array.get(cf06, i) <= array.get(cf06, i - 1) ? #FF6961 : color.lime) // roe/pb
        table.cell(tbis, 19, i, str.tostring(array.get(cf02, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(cf02, i)) ?  opt_datatextcolor : array.get(cf02, i) <= array.get(cf02, i - 1) ? #FF6961 : color.lime) // roa
        table.cell(tbis, 20, i, str.tostring(array.get(cf03, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(cf03, i)) ?  opt_datatextcolor : array.get(cf03, i) <= array.get(cf03, i - 1) ? #FF6961 : color.lime) // roic
        table.cell(tbis, 21, i, str.tostring(array.get(cf04, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(cf04, i)) ?  opt_datatextcolor : array.get(cf04, i) <= array.get(cf04, i - 1) ? #FF6961 : color.lime) // qr
        table.cell(tbis, 22, i, str.tostring(array.get(cf05, i) / 1000000, '#,##0' + 'M'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(cf05, i)) ?  opt_datatextcolor : array.get(cf05, i) <= array.get(cf05, i - 1) ? #FF6961 : color.lime) // fcf
    if barstate.islast and showValueFilter and t_3
        table.cell(tbis, 17, i, str.tostring(array.get(cf01, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(cf01, i)) ?  opt_datatextcolor : array.get(cf01, i) <= roeval ? #FF6961 : color.lime) // roe
        table.cell(tbis, 18, i, str.tostring(array.get(cf06, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(cf06, i)) ?  opt_datatextcolor : array.get(cf06, i) <= roepbval ? #FF6961 : color.lime) // roe/pb
        table.cell(tbis, 19, i, str.tostring(array.get(cf02, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(cf02, i)) ?  opt_datatextcolor : array.get(cf02, i) <= roaval ? #FF6961 : color.lime) // roa
        table.cell(tbis, 20, i, str.tostring(array.get(cf03, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(cf03, i)) ?  opt_datatextcolor : array.get(cf03, i) <= roicval ? #FF6961 : color.lime) // roic
        table.cell(tbis, 21, i, str.tostring(array.get(cf04, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(cf04, i)) ?  opt_datatextcolor : array.get(cf04, i) <= qrval ? #FF6961 : color.lime) // qr
        //Non-filtered
        table.cell(tbis, 22, i, str.tostring(array.get(cf05, i) / 1000000, '#,##0' + 'M'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(cf05, i)) ?  opt_datatextcolor : array.get(cf05, i) <= array.get(cf05, i - 1) ? opt_cellbgcolor : color.lime) // fcf  || NON-FILTERED
    if barstate.islast and t_4
        table.cell(tbis, 24, i, str.tostring(array.get(st03, i), '#,##0.00')+'%', text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(st03, i)) ?  opt_datatextcolor : array.get(st03, i) <= array.get(st03, i - 1) ? #FF6961 : color.lime) // dpayout
        table.cell(tbis, 25, i, str.tostring(array.get(st04, i), '#,##0.00')+'%', text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(st04, i)) ?  opt_datatextcolor : array.get(st04, i) <= array.get(st04, i - 1) ? #FF6961 : color.lime) // dyield
        table.cell(tbis, 26, i, str.tostring(array.get(st12, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(st12, i)) ?  opt_datatextcolor : array.get(st12, i) >= array.get(st12, i - 1) or array.get(st12, i) > 1 ? #FF6961 : color.lime) // peg
        table.cell(tbis, 27, i, str.tostring(array.get(st08, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(st08, i)) ?  opt_datatextcolor : array.get(st08, i) >= array.get(st08, i - 1) ? #FF6961 : color.lime) // peforward
        table.cell(tbis, 28, i, str.tostring(array.get(st10, i), '#,##0.00')+'%', text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(st10, i)) ?  opt_datatextcolor : array.get(st10, i) <= array.get(st10, i - 1) ? #FF6961 : color.lime) // sgrowthrate
        table.cell(tbis, 29, i, str.tostring(array.get(st01, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(st01, i)) ?  opt_datatextcolor : array.get(st01, i) >= array.get(st01, i - 1) ? #FF6961 : color.lime) // altman
        table.cell(tbis, 30, i, str.tostring(array.get(st02, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(st02, i)) ?  opt_datatextcolor : array.get(st02, i) >= array.get(st02, i - 1) ? #FF6961 : color.lime) // beneish
        table.cell(tbis, 31, i, str.tostring(array.get(st06, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(st06, i)) ?  opt_datatextcolor :  #a9cce3) // graham
        table.cell(tbis, 32, i, str.tostring(array.get(st07, i), '#,##0'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(st07, i)) ?  opt_datatextcolor : array.get(st07, i) <= array.get(st07, i - 1) ? #FF6961 : color.lime) // piotroski
        table.cell(tbis, 33, i, str.tostring(array.get(st09, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(st09, i)) ?  opt_datatextcolor : array.get(st09, i) <= array.get(st09, i - 1) ? #FF6961 : color.lime) // springate
        table.cell(tbis, 34, i, str.tostring(array.get(st11, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(st11, i)) ?  opt_datatextcolor : array.get(st11, i) >= array.get(st11, i - 1) ? #FF6961 : color.lime) // zmij
    if barstate.islast and showValueFilter and t_4
        table.cell(tbis, 24, i, str.tostring(array.get(st03, i), '#,##0.00')+'%', text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(st03, i)) ?  opt_datatextcolor : array.get(st03, i) >= 70 ? opt_cellbgcolor : color.lime) // dpayout
        table.cell(tbis, 25, i, str.tostring(array.get(st04, i), '#,##0.00')+'%', text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(st04, i)) ?  opt_datatextcolor : array.get(st04, i) <= dyieldval ? #FF6961 : color.lime) // dyield
        table.cell(tbis, 26, i, str.tostring(array.get(st12, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(st12, i)) ?  opt_datatextcolor : array.get(st12, i) >= pegval or array.get(st12, i) < 0 ? #FF6961 : color.lime) // peg
        table.cell(tbis, 27, i, str.tostring(array.get(st08, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(st08, i)) ?  opt_datatextcolor : array.get(st08, i) >= peforw ? #FF6961 : color.lime) // peforward
        table.cell(tbis, 28, i, str.tostring(array.get(st10, i), '#,##0.00')+'%', text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(st10, i)) ?  opt_datatextcolor : array.get(st10, i) <= sgr ? #FF6961 : color.lime) // sgrowthrate
        table.cell(tbis, 29, i, str.tostring(array.get(st01, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(st01, i)) ?  opt_datatextcolor : array.get(st01, i) <= altmanval ? #FF6961 : color.lime) // altman//
        table.cell(tbis, 30, i, str.tostring(array.get(st02, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(st02, i)) ?  opt_datatextcolor : array.get(st02, i) >= beneishval ? #FF6961 : color.lime) // beneish 
        table.cell(tbis, 32, i, str.tostring(array.get(st07, i), '#,##0'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(st07, i)) ?  opt_datatextcolor : array.get(st07, i) <= 0 ? #c0392b : array.get(st07, i) <= 1 ? #ec7063 : array.get(st07, i) <= 2  ? #e67e22  : array.get(st07, i) <= 3  ?  #fad7a0 : array.get(st07, i) <= 4  ?  #85c1e9  : array.get(st07, i) <= 5  ? #a9cce3  : array.get(st07, i) <= 6  ? #a9dfbf  : array.get(st07, i) <= 7  ? #7dcea0  : array.get(st07, i) <= 8  ?  #52be80 : color.lime) // piotroski
        table.cell(tbis, 33, i, str.tostring(array.get(st09, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(st09, i)) ?  opt_datatextcolor : array.get(st09, i) <= springateval ? #FF6961 : color.lime) // springate
        table.cell(tbis, 34, i, str.tostring(array.get(st11, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(st11, i)) ?  opt_datatextcolor : array.get(st11, i) <= zmijval and array.get(st11, i) >= 0 ? #FF6961 : color.lime) // zmij
        //Non-filtered
        table.cell(tbis, 31, i, str.tostring(array.get(st06, i), '#,##0.00'), text_size=opt_textsize, text_color=opt_datatextcolor, bgcolor=na(array.get(st06, i)) ?  opt_datatextcolor :  #a9cce3) // graham || NON-FILTERED

plot(datatoplot, style=plot.style_circles, linewidth=4, join=true)