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

//@version=5
indicator("EPS", overlay=false)

var table epsTable = table.new(position.middle_left, 7, 1, border_width=3)

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")
   
// Function to define previous quarters' earnings per share 
f_eps(i) =>
    request.security(syminfo.tickerid, '1M', EPS[i])

// Year over year percentage change EPS  
EPSyoy = (EPS-f_eps(12))/math.abs(f_eps(12))*100
    
// === TABLE FUNCTIONS ===
f_fillCell(_table, _column, _row, _value, _quarter) =>
    _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.000') + '\n' + _quarter
    table.cell(_table, _column, _row, _cellText, bgcolor=color.new(_c_color, _transp), text_color=_c_color, width=7)
    
f_fillCellComp(_table, _column, _row, _value, _text) =>
    _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.00') + '%\n' + _text
    table.cell(_table, _column, _row, _cellText, bgcolor=color.new(_c_color, _transp), text_color=_c_color, width=10)
    
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, 0, 0, f_eps(13), '1Y BACK')
    f_fillCell(epsTable, 1, 0, f_eps(10), '3Q BACK')
    f_fillCell(epsTable, 2, 0, f_eps(7), '2Q BACK')
    f_fillCell(epsTable, 3, 0, f_eps(4), '1Q BACK')
    f_fillCell(epsTable, 4, 0, EPS, 'CURRENT')
    f_fillCellComp(epsTable, 5, 0, avgPerf, 'QTRLY AVERAGE')
    f_fillCellComp(epsTable, 6, 0, (EPS-f_eps(13))/math.abs(f_eps(13))*100, 'YOY CHANGE')
