EPS  Table
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ARUN_SAXENA

//@version=5

indicator("EPS & revenue", overlay=false)

var table epsTable = table.new(position.top_left, 15, 15, border_width=2)

lightTransp = 90
avgTransp = 80
heavyTransp = 70

// === USER INPUTS ===
i_posColor = input(color.rgb(38, 166, 154), title='Positive Color')
i_negColor = input(color.rgb(240, 83, 80), title='Negative Color')
// i_volColor = input(color.new(#999999, 0), title='Neutral Color')

// === FUNCTIONS AND CALCULATIONS ===
// Current earnings per share
EPS = request.financial(syminfo.tickerid, "EARNINGS_PER_SHARE", "FQ")
SALES = request.financial(syminfo.tickerid, "TOTAL_REVENUE", "FQ")
// Function to define previous quarters' earnings per share 
f_eps(i) =>
    request.security(syminfo.tickerid, '1M', EPS[i])
f_sales(i) =>
    request.security(syminfo.tickerid, '1M', SALES[i])
// Year over year percentage change EPS  
EPSyoy = (EPS-f_eps(12))/math.abs(f_eps(12))*100
ChangeCurrent  = (EPS-f_eps(13))/math.abs(f_eps(13))*100
Change4  = (f_eps(4)-f_eps(16))/math.abs(f_eps(16))*100
Change7  = (f_eps(7)-f_eps(19))/math.abs(f_eps(19))*100
Change10  = (f_eps(10)-f_eps(22))/math.abs(f_eps(22))*100
Change13  = (f_eps(13)-f_eps(25))/math.abs(f_eps(25))*100
//for Sales data 

SalesCurrent1  = (SALES/10000000)
Sales41  = (f_sales(4)/10000000)
Sales71  = (f_sales(7)/10000000)
Sales101  = (f_sales(10)/10000000)
Sales131  = (f_sales(13)/10000000)
Sales151  = (f_sales(15)/10000000)


SalesCurrent  = (SALES-f_sales(13))/math.abs(f_sales(13))*100
Sales4  = (f_sales(4)-f_sales(16))/math.abs(f_sales(16))*100
Sales7  = (f_sales(7)-f_sales(19))/math.abs(f_sales(19))*100
Sales10  = (f_sales(10)-f_sales(22))/math.abs(f_sales(22))*100
Sales13  = (f_sales(13)-f_sales(25))/math.abs(f_sales(25))*100

// === TABLE FUNCTIONS ===
f_fillCell(_table, _column, _row, _value) =>
    _c_color = _value >= 0 ? i_posColor : i_negColor
    _transp = math.abs(_value) > 3 ? heavyTransp : math.abs(_value) > 1 ? avgTransp : lightTransp
    _cellText = str.tostring(_value, '0.0') 
    table.cell(_table, _column, _row, _cellText, bgcolor=color.new(_c_color, _transp), text_color=_c_color,text_size=size.small)
    
f_fillCellComp(_table, _column, _row, _value) =>
    _c_color = _value >= 0 ? i_posColor : i_negColor
    _transp = math.abs(_value) > 60 ? heavyTransp : math.abs(_value) > 20 ? avgTransp : lightTransp
    //_cellText = str.tostring(_value, '0') +  _text
    _cellText = str.tostring(_value, '0') + '%'
    table.cell(_table, _column, _row, _cellText, bgcolor=color.new(_c_color, _transp), text_color=_c_color,text_size=size.small)
    
    
    

//oneQperf = (EPS-f_eps(4))/math.abs(f_eps(4))*100
//twoQperf = (f_eps(4)-f_eps(7))/math.abs(f_eps(7))*100
//threeQperf = (f_eps(7)-f_eps(10))/math.abs(f_eps(10))*100
//avgPerf = math.avg(oneQperf, twoQperf, threeQperf)






// Display table 
if barstate.islast
    f_fillCell(epsTable, 1, 10, f_eps(13))
    f_fillCell(epsTable, 1, 9, f_eps(10))
    f_fillCell(epsTable, 1, 8, f_eps(7))
    f_fillCell(epsTable, 1, 7, f_eps(4))
    f_fillCell(epsTable, 1, 6, EPS)
  // QoQ change 
    f_fillCellComp(epsTable, 2, 6, ChangeCurrent)
    f_fillCellComp(epsTable, 2, 7, Change4)
    f_fillCellComp(epsTable, 2, 8, Change7)
    f_fillCellComp(epsTable, 2, 9, Change10)
    f_fillCellComp(epsTable, 2, 10, Change13)
    //f_fillCell(epsTable, 1, 1, EPS)
    
    
    //for sales 
    
    
    f_fillCell(epsTable, 3, 10, Sales131)
    f_fillCell(epsTable, 3, 9, Sales101)
    f_fillCell(epsTable, 3, 8, Sales71)
    f_fillCell(epsTable, 3, 7, Sales41)
    f_fillCell(epsTable, 3, 6, SalesCurrent1)
  // QoQ change 
    f_fillCellComp(epsTable, 4, 6, SalesCurrent)
    f_fillCellComp(epsTable, 4, 7, Sales4)
    f_fillCellComp(epsTable, 4, 8, Sales7)
    f_fillCellComp(epsTable, 4, 9, Sales10)
    f_fillCellComp(epsTable, 4, 10, Sales13)
    //f_fillCell(epsTable, 1, 1, SALES)
    
    
    txt5 = "MAR-21"
    txt4 = "JUN-21"
    txt3 = "SEP-21"
    txt2 = "DEC-21"
    txt1 = "MAR-22 "
    txt6 = "% CHANGE" 
    txt0 = " "
    
    
    //txt5 = "IY BACK"
    //txt4 = "3Q BACK"
    //txt3 = "2Q BACK"
    //txt2 = "1Q BACK"
    //txt1 = "CURRENT Q "
    //txt6 = "QoQ CHANGE" 
    //txt0 = " "
   
    //FOR HEADINGS 
    txt8 = "QTR"
    txt9 = "      EPS     " 
    
    txt10 = "SALES(IN Cr.)"
    txt11 = "% CHANGE" 
 
    
    
    //orignal 
    table.cell(epsTable,0,6, text=txt1, bgcolor=color.rgb(0,0,40,80), text_color=color.blue,text_size=size.small)
    table.cell(epsTable,0,7, text=txt2, bgcolor=color.rgb(0,0,40,80), text_color=color.blue,text_size=size.small)
    table.cell(epsTable,0,8, text=txt3, bgcolor=color.rgb(0,0,40,80), text_color=color.blue,text_size=size.small)
    table.cell(epsTable,0,9, text=txt4, bgcolor=color.rgb(0,0,40,80), text_color=color.blue,text_size=size.small)
    table.cell(epsTable,0,10, text=txt5, bgcolor=color.rgb(0,0,40,80), text_color=color.blue,text_size=size.small)
    table.cell(epsTable,2,5, text=txt6, bgcolor=color.rgb(0,0,40,80), text_color=color.blue,text_size=size.small)
    //table.cell(epsTable,0,7, text=txt7, bgcolor=color.rgb(0,0,40,80), text_color=color.blue,width=5,height=3)
    table.cell(epsTable,0,5, text=txt8, bgcolor=color.rgb(0,0,40,80), text_color=color.blue,text_size=size.small)
    table.cell(epsTable,1,5, text=txt9, bgcolor=color.rgb(0,0,40,80), text_color=color.blue,text_size=size.small)
    
    
    //SALES HEADING 
    table.cell(epsTable,3,5, text=txt10, bgcolor=color.rgb(0,0,40,80), text_color=color.blue,text_size=size.small)
    table.cell(epsTable,4,5, text=txt11, bgcolor=color.rgb(0,0,40,80), text_color=color.blue,text_size=size.small)
    
    
    table.cell(epsTable,0,0, text=txt0, bgcolor=color.rgb(0,0,0,100), text_color=color.blue,text_size=size.small)
    //table.cell(epsTable,0,1, text=txt0, bgcolor=color.rgb(0,0,0,100), text_color=color.blue,width=5,height=3)
    table.cell(epsTable,1,1, text=txt0, bgcolor=color.rgb(0,0,0,100), text_color=color.blue,text_size=size.small)
    //table.cell(epsTable,1,1, text=txt0, bgcolor=color.rgb(0,0,0,100), text_color=color.blue,width=5,height=3)
    table.cell(epsTable,2,2, text=txt0, bgcolor=color.rgb(0,0,0,100), text_color=color.blue,text_size=size.small)
    table.cell(epsTable,2,3, text=txt0, bgcolor=color.rgb(0,0,0,100), text_color=color.blue,text_size=size.small)
    //table.cell(epsTable,2,4, text=txt0, bgcolor=color.rgb(0,0,0,100), text_color=color.blue,text_size=size.small)
    table.cell(epsTable,2,0, text=txt0, bgcolor=color.rgb(0,0,0,100), text_color=color.blue,text_size=size.small)
