intraday barcount
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © avsr90

//@version=5


indicator(title="intraday barcount ",overlay=true,max_bars_back=5000)

resSymtickerid=input.timeframe(defval="",title="ticker id resolution")
resSymtickeridOpen=input.timeframe(defval="D",title="open resolution")

openSymtickerid= request.security(syminfo.tickerid ,resSymtickeridOpen, close[1],barmerge.gaps_off, barmerge.lookahead_on)
closeSymtickerid= request.security(syminfo.tickerid ,resSymtickerid, close,barmerge.gaps_off, barmerge.lookahead_on)

condBarssince= ta.change(openSymtickerid)
LengthBars=int(math.max(1, nz(ta.barssince(condBarssince)) + 1))


Intra_bars_count() =>
    
    openSymtickerid= request.security(syminfo.tickerid ,resSymtickeridOpen, close[1],barmerge.gaps_off, barmerge.lookahead_on)
    closeSymtickerid= request.security(syminfo.tickerid ,resSymtickerid, close,barmerge.gaps_off, barmerge.lookahead_on)

    condBarssince= ta.change(openSymtickerid)
    LengthBars=int(math.max(1, nz(ta.barssince(condBarssince)) + 1))
    
    greenBars = 0.00
    redBars = 0.00
    
    for i=1 to LengthBars
        if  ( close[i]>open[i])
            greenBars:= greenBars+1
        if not (close[i] > open[i])
            redBars := redBars +1
    
    [greenBars,redBars] 
[greenB, redB] = Intra_bars_count()


difBars=greenB-redB
difPer=math.round_to_mintick((difBars/LengthBars)*100)
Op_Cl=math.round_to_mintick(closeSymtickerid-openSymtickerid)
plot(openSymtickerid,title="Open Intra day")


//Table

BT = 0
TW = 0
TH=0


TL = input.string("bottom_center", title='Select Table Position',options=["top_left","top_center","top_right",
  "middle_left","middle_center","middle_right","bottom_left","bottom_center","bottom_right"] , 
  tooltip="Default location is at bottom_center")
//RoundingPrecision = input.int(title='Indicator Rounding Precision', defval=2, minval=0, maxval=5)
TS = input.string("Auto", title='Select Table Text Size',options=["Auto","Huge","Large","Normal","Small",
 "Tiny"] , tooltip="Default Text Size is Auto")


assignedposition = switch TL
    "top_left" => position.top_left
    "top_center" => position.top_center
    "top_right" => position.top_right
    "middle_left" => position.middle_left
    "middle_center" => position.middle_center
    "middle_right" => position.middle_right
    "bottom_left" => position.bottom_left
    "bottom_center" => position.bottom_center
    "bottom_right" => position.bottom_right

celltextsize = switch TS
    "Auto" => size.auto
    "Huge" => size.huge
    "Large" => size.large
    "Normal" => size.normal
    "Small" => size.small
    "Tiny" => size.tiny
    
    

var table i = table.new (assignedposition,15, 9, color.yellow,border_width=BT)

if barstate.islast

    table.cell(i, 1, 1, "INTRA DAY ",text_size=celltextsize,width=TW,height=TH,bgcolor=color.green)
    table.cell(i, 2, 1, "NUMBER OF",text_size=celltextsize,width=TW,height=TH,bgcolor=color.green)
    table.cell(i, 3, 1, "BARS AFTER",text_size=celltextsize,width=TW,height=TH,bgcolor=color.green)
    table.cell(i, 4, 1, "DAY OPEN",text_size=celltextsize,width=TW,height=TH,bgcolor=color.green)
    table.cell(i, 5, 1, "  ",text_size=celltextsize,width=TW,height=TH,bgcolor=color.green)
    table.cell(i, 6, 1, "  ",text_size=celltextsize,width=TW,height=TH,bgcolor=color.green)
    
    
    
    table.cell(i, 1, 2, "TotalBars",text_size=celltextsize,width=TW,height=TH,bgcolor=color.red)
    table.cell(i, 2, 2, "GreenBars",text_size=celltextsize, width=TW,height=TH,bgcolor=color.red)
    table.cell(i, 3, 2, "RedBars",text_size=celltextsize, width=TW,height=TH,bgcolor=color.red)
    table.cell(i, 4, 2, "Difference",text_size=celltextsize, width=TW,height=TH,bgcolor=color.red)
    table.cell(i, 5, 2, "%DifTotBars",text_size=celltextsize, width=TW,height=TH,bgcolor=color.red)
    table.cell(i, 6, 2, "Open-Close",text_size=celltextsize, width=TW,height=TH,bgcolor=color.red)
    
    
    table.cell(i, 1, 3, str.tostring(LengthBars) ,text_size=celltextsize, width=TW,height=TH)
    table.cell(i, 2, 3, str.tostring(greenB) ,text_size=celltextsize, width=TW,text_color= color.green,height=TH)
    table.cell(i, 3, 3, str.tostring(redB),text_size=celltextsize, width=TW,text_color=color.red,height=TH)
    table.cell(i, 4, 3, str.tostring(difBars),text_size=celltextsize, width=TW,text_color=greenB>redB ? 
     color.green:color.red,height=TH)
    table.cell(i, 5, 3, str.tostring(difPer),text_size=celltextsize, width=TW,text_color=greenB>redB ?
     color.green:color.red,height=TH)
    table.cell(i, 6, 3, str.tostring(Op_Cl),text_size=celltextsize, width=TW,text_color=Op_Cl>0 ?
     color.green:color.red,height=TH)
    