Kitti-Playbook request.earnings R0.0
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Kittimasak_S
// Date: Feb 5 2023
//Objective : 
//          Display Earning per share
//Calculation :
//          Get Earning  value by use  Function "request.earnings "
// Display :
//          Plot Earning Data 
//          Earning History Tab

//@version=5

indicator("Kitti-Playbook request.earnings R0.0")
GetEPS=request.earnings(ticker=syminfo.tickerid, field= earnings.actual, gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off, ignore_invalid_symbol=true, currency= syminfo.currency )
GetEPSColor= GetEPS>0 ?  color.new(#4c5eaf, 90)  :  GetEPS<=0? color.new(#864caf, 70) :color.new(#864caf, 70)
GetEPSColorbm= GetEPS>0 ?  color.new(#4c5eaf, 80)  :  GetEPS<=0? color.new(#864caf, 70) :color.new(#864caf, 70)

plot(GetEPS,"'Eraning per share",color=GetEPSColor,style=plot.style_area)
GetEPSbm = request.earnings(ticker=syminfo.tickerid, field=earnings.actual, gaps=barmerge.gaps_on, lookahead=barmerge.lookahead_on , ignore_invalid_symbol=true, currency= syminfo.currency)
plot(GetEPSbm,"'Eraning per share",color=GetEPSColorbm,style=plot.style_area)
plot(GetEPSbm)
//  Eaening when  last claculate 
s3 = GetEPS == GetEPSbm ? 1: 0
plot(s3*GetEPS," cal date ", color=color.new(color.yellow,80))
E0=ta.valuewhen(s3>0,GetEPS,0)   // earning / share T0
E1=ta.valuewhen(s3>0,GetEPS,1)   // earning / share T0
E2=ta.valuewhen(s3>0,GetEPS,2)   // earning / share T0
E3=ta.valuewhen(s3>0,GetEPS,3)   // earning / share T0

Eacc1 = E0+E1
Eacc2 = E0+E1 +E2
Eacc3 = E0+E1 +E2 +E3


//  check Last Bar
s4 = 1000
s4 := ta.barssince(s3>0)

// Value when last calcuation 
s5=ta.valuewhen(s3>0,close,0)

// Eraning %
s6=E0/s5 *100
Eacc1p = Eacc1/s5 *100
Eacc2p = Eacc2/s5 * 100
Eacc3p = Eacc3/s5 * 100
// Display Label

ShowInfoTab = input.bool(title='Show Infomation Bar ', defval=true, group='Display Setting', inline="line1")  // Show Tab

table_position2 = input.string(position.bottom_right, 'Information Bar position', options=
     [position.middle_left, 
     position.middle_center, 
     position.middle_right, 
     position.bottom_left, 
     position.bottom_center, 
     position.bottom_right, 
     position.top_center, 
     position.top_right], 
     group='Display Setting', inline="line2")

Tab_Width2 = input.int(30, "Size", minval=5, maxval=40, step=1, group='Display Setting', inline="line2")



var table Signal_table2 = table.new(position=table_position2, columns=1, rows=4, border_width=0)
tColor1 = s4 > 0 ? color.new(#888888, 0) : color.new(#ff9900, 90)
bColor0 = s4 > 300? color.new(#d1e43d, 80) : E0 > 0?  color.new(color.gray,90)  :E0 < 0?color.new(#e43dc0, 80) : color.new(#ff9900, 90)
bColor1 = s4 > 300? color.new(#d1e43d, 80) : E1 > 0?  color.new(color.gray,90)  :E1 < 0?color.new(#e43dc0, 80) : color.new(#ff9900, 90)
bColor2 = s4 > 300? color.new(#d1e43d, 80) : E2 > 0?  color.new(color.gray,90)  :E2 < 0?color.new(#e43dc0, 80) : color.new(#ff9900, 90)
bColor3 = s4 > 300? color.new(#d1e43d, 80) : E3 > 0?  color.new(color.gray,90)  :E3 < 0?color.new(#e43dc0, 80) : color.new(#ff9900, 90)


_TEXT_DisplayA20 =   " EPS (E0) = " + str.format("{0,number,#.###}",(E0))   + " " +syminfo.currency +" : " +  str.format("{0,number,#.###}",(s6)) + "%" +" ( Report@ =  - " + str.tostring(s4) + "  Bar : Price "
                     +  str.format("{0,number,#.###}",(s5)) + " " +  syminfo.currency + " )"

_TEXT_DisplayA21 =   " EPS (E1)  = " + str.format("{0,number,#.###}",(E1))   + " " +syminfo.currency +" :(Acc E0->E1  = " +  str.format("{0,number,#.###}",(Eacc1)) + " " +  syminfo.currency 
                     +  "  / " +str.format("{0,number,#.###}",(Eacc1p)) + "%"+ ")"
            
_TEXT_DisplayA22 =  " EPS (E2)  = " + str.format("{0,number,#.###}",(E2))   + " " +syminfo.currency +" : (Acc E0->E2 = " +  str.format("{0,number,#.###}",(Eacc2)) + " " +  syminfo.currency
                     +  " / " +str.format("{0,number,#.###}",(Eacc2p)) + "%"+ ")"
               
_TEXT_DisplayA23 =  " EPS (E3)  = " + str.format("{0,number,#.###}",(E3))   + " " +syminfo.currency +" : (Acc E0->E3 = " +  str.format("{0,number,#.###}",(Eacc3)) + " " +  syminfo.currency
                     +  " / " +str.format("{0,number,#.###}",(Eacc3p)) + "%" + ")"                


if ShowInfoTab == true 
    table.cell(Signal_table2, 0, 0, _TEXT_DisplayA20, bgcolor=bColor0, text_color=tColor1,text_halign=text.align_left, width=Tab_Width2)
    table.cell(Signal_table2, 0, 1, _TEXT_DisplayA21, bgcolor=bColor1, text_color=tColor1,text_halign=text.align_left, width=Tab_Width2)
    table.cell(Signal_table2, 0, 2, _TEXT_DisplayA22, bgcolor=bColor2, text_color=tColor1,text_halign=text.align_left, width=Tab_Width2)
    table.cell(Signal_table2, 0, 3, _TEXT_DisplayA23, bgcolor=bColor3, text_color=tColor1,text_halign=text.align_left, width=Tab_Width2)


//========================                 END            =================================================================================   
