Kitti-Playbook request.dividends 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 Devidends Amount per Share 
// Calculation :
//       Use function request.dividends(ticker, field, gaps, lookahead, ignore_invalid_symbol, currenc
// Display : The value of Dividends 

//@version=5
indicator("Kitti-Playbook request.dividends R0.0")
ReqDiv = request.dividends(ticker=syminfo.tickerid, field=dividends.gross, gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off , ignore_invalid_symbol=false, currency=syminfo.currency)
ReqDivBM = request.dividends(syminfo.tickerid, dividends.gross, gaps=barmerge.gaps_on, lookahead=barmerge.lookahead_on, ignore_invalid_symbol=false, currency=syminfo.currency)

ReqDiv2=ReqDivBM>0? ReqDiv:na
ReqDivGross1=ReqDivBM>0? na:ReqDiv

plot(ReqDivGross1,"dividends Area",color= color.new(color.green, 90),style=plot.style_area)
plot(ReqDiv2,"dividends Con",color=color.new(color.green,90),style=plot.style_area)

s3= ReqDiv>0 and ReqDivBM > 0  ? 1 : 0  // When XD

// Count Last XD date
s4=ta.barssince(s3>0)

// XD Percent calculate 

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

//plot(s5,"Value when XD" , color.fuchsia)

//  Devidents amount per share = ReqDiv
//  Devidents amount % per share = ReqDiv/s5

s6=ReqDiv/s5 * 100    //  % Divident / Share

//plot(s6," s6" , color.green)
// =========  
//  XD when  last claculate 

plot(s3*ReqDiv," cal date ", color=color.new(color.blue,50))
// Display Label

ShowInfoTab = input.bool(title='Show Infomation Bar ', defval=true, group='Display Setting')  // Show Tab
Tab_Width2 =  input.int(16, "Information Bar Size", minval=1, maxval=20, step=1,group='Display Setting')

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')


var table Signal_table2 = table.new(position=table_position2, columns=1, rows=1, border_width=2)
tColor1 = color.new(#888888, 0)

bColor0 = s4 < 300?  color.new(color.gray,90) :color.new(#e43dc0, 80)

_TEXT_DisplayA2 =  " \Since Last XD = " + str.tostring(s4) + "  Bar"
                         + " \n Value when XD   = " + str.format("{0,number,#.###}",(s5))   + " " +syminfo.currency
                         + " \n Div per share   = " + str.format("{0,number,#.###}",(ReqDiv))   + " " +syminfo.currency +" : " + str.format("{0,number,#.###}",(s6)) + " %"

if ShowInfoTab == true 
    table.cell(Signal_table2, 0, 0, _TEXT_DisplayA2, bgcolor=bColor0, text_color=tColor1,text_halign=text.align_left, width=Tab_Width2)
    
//========================                 END            =================================================================================    



