EPS & revenue
// 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
//@version=5

indicator("EPS & Revnue ", overlay=true)

//table location input parmeter 
i_posTable = input.string(defval=position.top_left, title='Table Position', 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" ,inline="6")
var table epsTable = table.new(i_posTable , 17, 17, border_width=1,border_color=color.black)
// === USER INPUTS  colors for table data 
lightTransp = 0
avgTransp = 0
heavyTransp = 0
i_posColor = color.rgb(144,238,144)
i_negColor = color.rgb(255,157,157)
i_posColor0 = color.rgb(144,238,144)
i_posColor1 = color.rgb(144,238,144)
i_posColor2 = color.rgb(144,238,144)
i_negColor2 = color.rgb(255,157,157)
i_negColor1= color.rgb(255,157,157)
i_negColor0 = color.rgb(255,157,157)






// === FUNCTIONS AND CALCULATIONS ===

EPS = request.financial(syminfo.tickerid, "EARNINGS_PER_SHARE", "FQ")
SALES = request.financial(syminfo.tickerid, "TOTAL_REVENUE", "FQ")
Free_float = request.financial(syminfo.tickerid, "FLOAT_SHARES_OUTSTANDING","FY")
OP = request.financial(syminfo.tickerid, "OPER_INCOME", "FQ" )
ROE =request.financial(syminfo.tickerid, "RETURN_ON_EQUITY", "FQ")


f_eps(i) =>
    request.security(syminfo.tickerid, '1M', EPS[i])
f_sales(i) =>
    request.security(syminfo.tickerid, '1M', SALES[i])
f_op(i) =>
    request.security(syminfo.tickerid, '1M', OP[i])
f_roe(i) =>
    request.security(syminfo.tickerid, '1M', ROE[i])    
    
// EPS calculation.  
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
//Sales calculation . 

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)
ff = Free_float*ta.vwap/10000000

//for ROE data 
roe1  = ROE
roe4  = f_roe(4)//)/math.abs(f_sales(4))*100
roe7  = f_roe(7)//)/math.abs(f_sales(7))*100
roe10 = f_roe(10)//)/math.abs(f_sales(10))*100
roe13 = f_roe(13)//)/math.abs(f_sales(13))*100

//for OPM data  calculation 
opm1  = (OP)/math.abs(SALES)*100
opm4  = (f_op(4))/math.abs(f_sales(4))*100
opm7  = (f_op(7))/math.abs(f_sales(7))*100
opm10 = (f_op(10))/math.abs(f_sales(10))*100
opm13 = (f_op(13))/math.abs(f_sales(13))*100
//Change13  = (f_eps(13)-f_eps(25))/math.abs(f_eps(25))*100

//other calculation . 

TOVR = input(defval=50,title="Average Turnover Period", group= " Input Period  for Average Turnover  Calculation")
turnover50= ta.sma(volume,TOVR)*ta.sma(close,TOVR)*TOVR/10000000
turnover= (volume)*(close)/10000000  
AAA = input(defval=50,title="Relative Volume Lenght", group= " Input Lenght for Relative Volume Calculation")
Rvol=volume/ta.sma(volume,AAA )*100

RRR = str.tostring(Rvol, '0') + '%'



high_low_close = string(' ')

weekly_hh = request.security(syminfo.tickerid, 'W', ta.highest(high, 52), lookahead=barmerge.lookahead_on)
weekly_ll = request.security(syminfo.tickerid, 'W', ta.lowest(low, 52), lookahead=barmerge.lookahead_on)
weekly_hc = request.security(syminfo.tickerid, 'W', ta.highest(close, 52), lookahead=barmerge.lookahead_on)
weekly_lc = request.security(syminfo.tickerid, 'W', ta.lowest(close, 52), lookahead=barmerge.lookahead_on)

high_plot = high_low_close == ' ' ? weekly_hh : weekly_hc
low_plot = high_low_close == ' ' ? weekly_ll : weekly_lc
//Average daily range calculation . 
Length = input(20, title='ADR length', group = "input ADR lenght for ADR % calculation")
dhigh = request.security(syminfo.tickerid, 'D', high)
dlow = request.security(syminfo.tickerid, 'D', low)
ADR = 100 * (ta.sma(dhigh / dlow, Length) - 1)



//down from 52w high
down =(-high_plot+close)/high_plot*100
up   = (close-low_plot)/low_plot*100


//QoQ sales calculation.
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
    _c_color =if  _value > 3 
        i_posColor2
    else if _value <=3 and _value >=1
        i_posColor1
    else if _value <1 and _value >0
        i_posColor0
    else if _value < 0 and _value >= -1
        i_negColor0
    else if _value <= -1 and _value >= -3
        i_negColor1       
    else if _value < -3
        i_negColor2
    else
        color.white
    _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=color.black,text_size=size.small)
    
f_fillCellComp(_table, _column, _row, _value) =>
    _c_color =if  _value > 3 
        i_posColor2
    else if _value <3 and _value >1
        i_posColor1
    else if _value <1 and _value >0
        i_posColor0
    else if _value < 0 and _value > -1
        i_negColor0
    else if _value < -1 and _value > -3
        i_negColor1       
    else if _value < -3
        i_negColor2
    else 
        color.white
    
    
    //_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=color.black,text_size=size.small)
    
f_fillCellComp2(_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=color.black,text_size=size.small)
    
f_fillCellComp1(_table, _column, _row, _value) =>
    _c_color = color.rgb(208,224,113)
    _transp = math.abs(_value) > 60 ? heavyTransp : math.abs(_value) > 20 ? avgTransp : lightTransp
    //_cellText = str.tostring(_value, '0') +  _text
    _cellText = str.tostring(_value, '0.0') 
    table.cell(_table, _column, _row, _cellText, bgcolor=color.new(_c_color, _transp), text_color=color.black,text_size=size.small)   
    
f_fillCellCompADR(_table, _column, _row, _value) =>
    //_c_color = color.rgb(208,224,113)
    _c_color = math.abs(_value) > 3 ? i_posColor0 : i_negColor0
    //_cellText = str.tostring(_value, '0') +  _text
    _cellText = str.tostring(ADR, '0.00')+ '%' 
    table.cell(_table, _column, _row, _cellText, bgcolor=color.new(_c_color, 20), text_color=color.black,text_size=size.normal)   
    
//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)
    f_fillCell(epsTable, 2, 13, ff)
    
    //52w high low 
    
    f_fillCellCompADR(epsTable, 5, 11, ADR)
    //f_fillCellComp1(epsTable, 5, 12, low_plot)
    
    
    
    
  // 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_fillCellComp1(epsTable, 2, 14, turnover)
    f_fillCellComp1(epsTable, 5, 14, turnover50)
    f_fillCellComp2(epsTable, 2, 12, up)
    f_fillCellComp2(epsTable, 2, 11, down)
    f_fillCellComp2(epsTable, 5, 13, Rvol)
    
    f_fillCellComp(epsTable, 5, 6, opm1)
    f_fillCellComp(epsTable, 5, 7, opm4)
    f_fillCellComp(epsTable, 5, 8, opm7)
    f_fillCellComp(epsTable, 5, 9, opm10)
    f_fillCellComp(epsTable, 5, 10, opm13)

    f_fillCellComp(epsTable, 6, 6, roe1)
    f_fillCellComp(epsTable, 6, 7, roe4)
    f_fillCellComp(epsTable, 6, 8, roe7)
    f_fillCellComp(epsTable, 6, 9, roe10)
    f_fillCellComp(epsTable, 6, 10, roe13)
    bColor=color.rgb(180, 177, 219)
    txt6 = "% CHG" 
    txt1111= "Earning Calendar (INR)"

    
else

    bColor=color.rgb(180, 177, 219)
    //txt6 = "% CHG" 
    txt1111= "Earning Calendar (INR)" 
    txt5 = "IY BACK"
    txt4 = "3Q BACK"
    txt3 = "2Q BACK"
    txt2 = "1Q BACK"
    txt1 =  "Current Q"//str.tostring(DDA) + "/" + str.tostring(MMA)  + "/" + str.tostring(YYA)
    txt6 = "% CHG" 
    txt0 = " "
   
    //FOR HEADINGS 
    txt8 = "REPORTED "
    txt9 = " EPS" 
    
    txt10 = "SALES(Cr)"
    txt11 = "% CHG" 
    txt111 = "OPM%"
    txt121 = "ROE%"
    txt12 = "TURNOVER D (Cr)"
    txt13 = "% UP FROM 52W LOW"
    txt14 = "% DOWN FROM 52W HIGH"
    txt15 = "FREE FLOAT (Cr)"
    txt16 = "Created By"
    txt17 = "ARUN_SAXENA"
    txt18 =  "TURNOVER # (IN Cr.)"
    
    
    
    txt61 = "ADR % #" 
    txt62 = "52W LOW" 
    txt63 = "R Vol" 



    table.cell(epsTable,0,6, text=txt1, bgcolor=bColor, text_color=color.black,text_size=size.small)
    table.cell(epsTable,0,7, text=txt2, bgcolor=bColor, text_color=color.black,text_size=size.small)
    table.cell(epsTable,0,8, text=txt3, bgcolor=bColor, text_color=color.black,text_size=size.small)
    table.cell(epsTable,0,9, text=txt4, bgcolor=bColor, text_color=color.black,text_size=size.small)
    table.cell(epsTable,0,10, text=txt5, bgcolor=bColor, text_color=color.black,text_size=size.small)
    table.cell(epsTable,2,5, text=txt6, bgcolor=bColor, text_color=color.black,text_size=size.small)
    table.cell(epsTable,3,11, text=txt61, bgcolor=bColor, text_color=color.black,text_size=size.normal)
    table.cell(epsTable,3,12, text=txt62, bgcolor=bColor, text_color=color.black,text_size=size.small)
    table.cell(epsTable,3,13, text=txt63, bgcolor=bColor, text_color=color.black,text_size=size.small)
    
    table.cell(epsTable,0,5, text=txt8, bgcolor=bColor, text_color=color.black,text_size=size.small)
    table.cell(epsTable,1,5, text=txt9, bgcolor=bColor, text_color=color.black,text_size=size.small)
    
    
    
    table.cell(epsTable,3,5, text=txt10, bgcolor=bColor, text_color=color.black,text_size=size.small)
    table.cell(epsTable,4,5, text=txt11, bgcolor=bColor, text_color=color.black,text_size=size.small)
    table.cell(epsTable,5,5, text=txt111, bgcolor=bColor, text_color=color.black,text_size=size.small)
    table.cell(epsTable,6,5, text=txt121, bgcolor=bColor, text_color=color.black,text_size=size.small)
    table.cell(epsTable,0,2, text=txt1111, bgcolor=color.rgb(129, 210, 251), text_color=color.black,text_size=size.normal, text_font_family = font.family_monospace)
    table.cell(epsTable,0,0, text=txt0, bgcolor=color.rgb(0,0,0,100), text_color=color.black,text_size=size.normal)
    table.cell(epsTable,0,1, text=txt0, bgcolor=color.rgb(0,0,0,100), text_color=color.black,text_size=size.small)
    table.cell(epsTable,0,14, text=txt12, bgcolor=bColor, text_color=color.black,text_size=size.small)
    table.cell(epsTable,3,14, text=txt18, bgcolor=bColor, text_color=color.black,text_size=size.small)
    table.cell(epsTable,0,12, text=txt13, bgcolor=bColor, text_color=color.black,text_size=size.small)
    table.cell(epsTable,0,11, text=txt14, bgcolor=bColor, text_color=color.black,text_size=size.small)
    table.cell(epsTable,0,13, text=txt15, bgcolor=bColor, text_color=color.black,text_size=size.small)
    table.merge_cells(epsTable,3,14,4,14)
    table.merge_cells(epsTable,3,11,4,12)
    table.merge_cells(epsTable,3,13,4,13)
    table.merge_cells(epsTable,5,11,6,12)

    table.merge_cells(epsTable,5,14,6,14)
   // table.merge_cells(epsTable,5,11,6,11)
    //table.merge_cells(epsTable,5,12,6,12)
    table.merge_cells(epsTable,5,13,6,13)  
    table.merge_cells(epsTable,0,14,1,14)
    table.merge_cells(epsTable,0,11,1,11)
    table.merge_cells(epsTable,0,12,1,12)
    table.merge_cells(epsTable,0,13,1,13)    
    table.merge_cells(epsTable,0,2,6,2)
    table.merge_cells(epsTable,0,0,6,1)

// Length = input(20, title='length')

// dhigh = request.security(syminfo.tickerid, 'D', high)
// dlow = request.security(syminfo.tickerid, 'D', low)
// // formula amended, thanks to GlinckEastwoot
// ADR = 100 * (ta.sma(dhigh / dlow, Length) - 1)




i_moreData     = false

rev = request.financial(syminfo.tickerid,'TOTAL_REVENUE','FQ',barmerge.gaps_on, ignore_invalid_symbol=true)
datasize = 7
blankUnderUp = i_moreData == false ? 3 : 6 // Because there is a blank between the top of the table and the second line but Tradingview doesn't display it.

// Function for Date
f_array(arrayId, val) => 
    array.unshift(arrayId, val) // append vale to an array
    array.pop(arrayId)

ftdate(_table, _column, _row, _value) => 
    myColor = color.red 
    table.cell(table_id = _table, column = _column, row = _row, text = _value, bgcolor = myColor, text_color = color.black)
// For Date
var date = array.new_int(datasize)
if rev
    f_array(date, time)


// Display table 
//scondRepeatSameValueAtLastLine = actualEPS==actualEPS1 and standardEPS==standardEPS1 and EPS_Estimate==EPS_Estimate[1] // here I use 'and' instead of 'or' because we want to avoid the display bug of TradingView when the 2 last lines repeat themselves
if barstate.islast
 
    // For Date MMM-yy
    for i = 0 to datasize-blankUnderUp
        if barstate.islast
            table.cell(epsTable, 0, (6+i), str.format('{0, date, MMM-yy}', array.get(date, i)),bgcolor=color.rgb(180, 177, 219), text_color=color.black,text_size=size.small)
//plot(Rvol , color=color.rgb(0,100,0,100))
