Day Open,High,Low Fib Levels
// 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('Day Open,High,Low Fib Levels', overlay=true,max_bars_back=1000,precision=2)

Openp= math.round_to_mintick(request.security(syminfo.tickerid ,"D",close[1],barmerge.gaps_off, barmerge.lookahead_on)) 
Nifnum= ta.change(Openp)
Intraday_Length=int(math.max(1, nz(ta.barssince(Nifnum)) + 1))
Clep= math.round_to_mintick(close) 
Levelop=plot(Openp, title="OpenPrice", color=color.fuchsia,linewidth=2)
Lp_Op=Clep-Openp

//Show Label Option
Show_LabelMA20=input.bool(defval=true,title="Show LabelMA20")
Show_LabelMA24=input.bool(defval=false,title="Show LabelMA24")
Show_LabelMA38=input.bool(defval=false,title="Show LabelMA38")
Show_LabelMA50=input.bool(defval=false,title="Show LabelMA50")
Show_LabelMA62=input.bool(defval=false,title="Show LabelMA62")
Show_LabelMA79=input.bool(defval=false,title="Show LabelMA79")
Show_LabelMA200=input.bool(defval=true,title="Show LabelMA200")
Regular=input.bool(defval=false,title="Regular")

//Oscillation Choice 
Sou = input(title='MA Source', defval=close)
MA20_sou = input.string(title='MA20', defval='SMA', options=['SMA', 'EMA', 'WMA', 'VWMA'])
MA24_sou = input.string(title='MA24', defval='SMA', options=['SMA', 'EMA', 'WMA', 'VWMA'])
MA38_sou = input.string(title='MA38', defval='SMA', options=['SMA', 'EMA', 'WMA', 'VWMA'])
MA50_sou = input.string(title='MA50 Osc', defval='SMA', options=['SMA', 'EMA', 'WMA', 'VWMA'])
MA62_sou = input.string(title='MA62 Osc', defval='SMA', options=['SMA', 'EMA', 'WMA', 'VWMA'])
MA79_sou = input.string(title='MA79 Osc', defval='SMA', options=['SMA', 'EMA', 'WMA', 'VWMA'])
MA200_sou = input.string(title='MA200', defval='SMA', options=['SMA', 'EMA', 'WMA', 'VWMA'])
Regular_Length=input.int(defval=14,title="Regular Length")

//Moving Average Calculbtions
MA20 = MA20_sou == 'SMA' ? ta.sma(Sou, 20) : MA20_sou == 'EMA' ? ta.ema(Sou, 20) : MA20_sou == 'WMA' ? ta.wma(Sou, 20) :  MA20_sou == 'VWMA' ? ta.vwma(Sou,20): na
MA24 = MA24_sou == 'SMA' ? ta.sma(Sou, 24) : MA24_sou == 'EMA' ? ta.ema(Sou, 24) : MA24_sou == 'WMA' ? ta.wma(Sou, 24) :  MA24_sou == 'VWMA' ? ta.vwma(Sou,24): na
MA38 = MA38_sou == 'SMA' ? ta.sma(Sou, 38) : MA38_sou == 'EMA' ? ta.ema(Sou, 38) : MA38_sou == 'WMA' ? ta.wma(Sou, 38) : MA38_sou == 'VWMA' ? ta.vwma(Sou, 38) :  na
MA50 = MA50_sou == 'SMA' ? ta.sma(Sou, 50) : MA50_sou == 'EMA' ? ta.ema(Sou, 50) : MA50_sou == 'WMA' ? ta.wma(Sou, 50) : MA50_sou == 'VWMA' ? ta.vwma(Sou, 50) :  na
MA62 = MA62_sou == 'SMA' ? ta.sma(Sou, 62) : MA62_sou == 'EMA' ? ta.ema(Sou, 62) : MA62_sou == 'WMA' ? ta.wma(Sou,62) : MA62_sou == 'VWMA' ? ta.vwma(Sou, 62) :  na
MA79 = MA79_sou == 'SMA' ? ta.sma(Sou, 79) : MA79_sou == 'EMA' ? ta.ema(Sou, 79) : MA79_sou == 'WMA' ? ta.wma(Sou, 79) : MA79_sou == 'VWMA' ? ta.vwma(Sou, 79) :  na
MA200 = MA200_sou == 'SMA' ? ta.sma(Sou, 200) : MA200_sou == 'EMA' ? ta.ema(Sou, 200) : MA200_sou == 'WMA' ? ta.wma(Sou, 200) :  MA200_sou == 'VWMA' ? ta.vwma(Sou,200): na

MAup=math.max(MA24,MA38,MA50,MA62,MA79)
MAdn=math.min(MA24,MA38,MA50,MA62,MA79)
MAmid=(MAup+MAdn)/2

//Plots
plot(MA20, title='MA20', color=color.new(color.orange, 0), display=Show_LabelMA20?display.all:display.none)
plot(MA24, title='MA24', color=color.new(color.fuchsia, 0), display=Show_LabelMA24?display.all:display.none)
plot(MA38, title='MA38', color=color.new(color.orange, 0), display=Show_LabelMA38?display.all:display.none)
plot(MA50, title='MA50', color=color.new(color.red, 0), display=Show_LabelMA50?display.all:display.none)
plot(MA62, title='MA62', color=color.new(color.maroon, 0), display=Show_LabelMA62?display.all:display.none)
plot(MA79, title='MA79', color=color.new(color.yellow, 0), display=Show_LabelMA79?display.all:display.none)
plot(MA200, title='MA200', color=color.new(color.yellow, 0), display=Show_LabelMA200?display.all:display.none)
plot(MAmid,title="MAMid",color=color.fuchsia)


//Fill function
Level1=plot(MAup,title="MAUp")
Level2=plot(MAdn,title="MAdn")
fill(Level1,Level2,color=color.rgb(213, 213, 233))


//fill(levelop,levelcl,color=Openp>Clep ? color.rgb(226, 199, 199):color.rgb(202, 219, 203),title="OPCL fill")
Hid=not Regular?ta.highest(Intraday_Length):ta.highest(Regular_Length)
Hidp=plot(Hid,title="Highest",style=plot.style_circles,color=color.green)
Lid=not Regular?ta.lowest(Intraday_Length):ta.lowest(Regular_Length)
Lidp=plot(Lid,title="Lowest", color=color.red,style=plot.style_circles)


HiOp=close>Openp?Hid-Openp:na

Le5=close>Openp?Hid-HiOp*0.786:na
lev1=plot(Le5,title="Hi5",color=color.blue,style=plot.style_circles,join=true)

Le4=close>Openp?Hid-HiOp*0.618:na
lev2=plot(Le4,title="Hi4", color=color.blue,style=plot.style_circles,join=true)

Le3=close>Openp?Hid-HiOp*0.50:na
lev3=plot(Le3,title="Hi3",color=color.yellow,style=plot.style_circles,join=true)

Le2=close>Openp?Hid-HiOp*0.382:na
lev4=plot(Le2,title="Hi2", color=color.blue,style=plot.style_circles,join=true)

Le1=close>Openp?Hid-HiOp*0.236:na
lev5=plot(Le1,title="He1",color=color.blue,style=plot.style_circles,join=true)


LiOp= Openp>close? Lid -Openp:na

Lle1= Openp>close?Lid-LiOp*0.236:na
llev1=plot(Lle1,title="Li1",color=color.blue,style=plot.style_circles,join=true)

Lle2=Openp>close?Lid-LiOp*0.382:na
llev2=plot(Lle2,title="Li2", color=color.blue,style=plot.style_circles,join=true)

Lle3=Openp>close?Lid-LiOp*0.50:na
llev3=plot(Lle3,title="Li3",color=color.yellow,style=plot.style_circles,join=true)

Lle4=Openp>close?Lid-LiOp*0.618:na
llev4=plot(Lle4,title="Li4", color=color.blue,style=plot.style_circles,join=true)

Lle5=Openp>close?Lid-LiOp*0.786:na
llev5=plot(Lle5,title="Li5",color=color.blue,style=plot.style_circles,join=true)


//BOXES

topleft1=chart.point.now(Hid)
bottomright1=chart.point.from_index(bar_index ,Hid)
box2=box.new(topleft1,bottomright1,bgcolor=color.gray,text="HID",text_color=color.green,text_size=size.normal)
box.delete(box2[1])

topleft2=chart.point.now(Le1)
bottomright2=chart.point.from_index(bar_index ,Le1)
box3=box.new(topleft2,bottomright2,bgcolor=color.gray,text="78.6",text_color=color.blue,text_size=size.normal)
box.delete(box3[1])

topleft3=chart.point.now(Le2)
bottomright3=chart.point.from_index(bar_index ,Le2)
box4=box.new(topleft3,bottomright3,bgcolor=color.gray,text="61.8",text_color=color.blue,text_size=size.normal)
box.delete(box4[1])

topleft4=chart.point.now(Le3)
bottomright4=chart.point.from_index(bar_index ,Le3)
box5=box.new(topleft4,bottomright4,bgcolor=color.gray,text="50.0",text_color=color.blue,text_size=size.normal)
box.delete(box5[1])

topleft5=chart.point.now(Le4)
bottomright5=chart.point.from_index(bar_index ,Le4)
box6=box.new(topleft5,bottomright5,bgcolor=color.gray,text="38.2",text_color=color.blue,text_size=size.normal)
box.delete(box6[1])


topleft6=chart.point.now(Le5)
bottomright6=chart.point.from_index(bar_index ,Le5)
box7=box.new(topleft6,bottomright6,bgcolor=color.gray,text="23.6",text_color=color.blue,text_size=size.normal)
box.delete(box7[1])

topleft7=chart.point.now(Lid)
bottomright7=chart.point.from_index(bar_index ,Lid)
box8=box.new(topleft7,bottomright7,bgcolor=color.gray,text="LID",text_color=color.red,text_size=size.normal)
box.delete(box8[1])

topleft8=chart.point.now(Openp)
bottomright8=chart.point.from_index(bar_index ,Openp)
box9=box.new(topleft8,bottomright8,bgcolor=color.gray,text="Open Price",text_color=color.fuchsia,text_size=size.normal)
box.delete(box9[1])


topleft9=chart.point.now(Lle1)
bottomright9=chart.point.from_index(bar_index ,Lle1)
box10=box.new(topleft9,bottomright9,bgcolor=color.gray,text="78.6",text_color=color.blue,text_size=size.normal)
box.delete(box10[1])

topleft10=chart.point.now(Lle2)
bottomright10=chart.point.from_index(bar_index ,Lle2)
box11=box.new(topleft10,bottomright10,bgcolor=color.gray,text="61.8",text_color=color.blue,text_size=size.normal)
box.delete(box11[1])

topleft11=chart.point.now(Lle3)
bottomright11=chart.point.from_index(bar_index ,Lle3)
box12=box.new(topleft11,bottomright11,bgcolor=color.gray,text="50.0",text_color=color.blue,text_size=size.normal)
box.delete(box12[1])

topleft12=chart.point.now(Lle4)
bottomright12=chart.point.from_index(bar_index ,Lle4)
box13=box.new(topleft12,bottomright12,bgcolor=color.gray,text="38.2",text_color=color.blue,text_size=size.normal)
box.delete(box13[1])


topleft13=chart.point.now(Lle5)
bottomright13=chart.point.from_index(bar_index ,Lle5)
box14=box.new(topleft13,bottomright13,bgcolor=color.gray,text="23.6",text_color=color.blue,text_size=size.normal)
box.delete(box14[1])

topleft14=chart.point.now(MA20)
bottomright14=chart.point.from_index(bar_index ,MA20)
box15=box.new(topleft14,bottomright14,bgcolor=color.gray,text="MA20",text_color=color.orange,text_size=size.normal)
box.delete(box15[1])

topleft15=chart.point.now(MA200)
bottomright15=chart.point.from_index(bar_index ,MA200)
box16=box.new(topleft15,bottomright15,bgcolor=color.gray,text="MA200",text_color=color.yellow,text_size=size.normal)
box.delete(box16[1])

topleft16=chart.point.now(MAmid)
bottomright16=chart.point.from_index(bar_index ,MAmid)
box17=box.new(topleft16,bottomright16,bgcolor=color.gray,text="Mid",text_color=color.fuchsia,text_size=size.normal)
box.delete(box17[1])



//Number of Bars above Gray Zone,in the Gray zone and below Gray zone
Intra_bars_count1() =>
    
    greB = 0.00
    redB = 0.00
    greB1=0.00
    redB1=0.00
    greB2=0.00
    redB2=0.00

    for i=1 to Intraday_Length
              
        if close[i]>open[i] and close[i]>MAup[i]
            greB:= greB+1
        if close[i]<open[i] and close[i]>MAup[i]
            redB := redB+1

        if close[i]>open[i] and close[i]<MAdn[i]
            greB1:= greB1+1
        if close[i]<open[i] and close[i]<MAdn[i]
            redB1 := redB1+1

        if close[i]>open[i] and close[i]<MAup[i] and close[i]>MAdn[i]
            greB2:= greB2+1
        if close[i]<open[i] and close[i]<MAup[i] and close[i]>MAdn[i]
            redB2 := redB2+1

    
    [greB,redB,greB1,redB1,greB2,redB2] 
[Gba, Rba,Gba1,Rba1,Gba2,Rba2] =  Intra_bars_count1()

Gray_Zone_Bars=Gba2+Rba2 - (Gba+Rba+Gba1+Rba1)

//TABLE

BorderThickness = 1,TableWidth = 0,Tableheight=0
var table t = table.new (position=position.bottom_right,columns=30,rows= 50, frame_color=color.black,frame_width=2,border_color=color.gray,border_width=BorderThickness)


if barstate.islast
    table.cell(t, 1, 1, str.tostring(Lp_Op)+"\n\LP-OP",text_size=size.normal, width=TableWidth,height=Tableheight,text_color=color.black,bgcolor=Lp_Op>0 ? color.rgb(178,209,175):
     color.rgb(180,151,151),text_halign=text.align_left)
    table.cell(t, 2, 1, "Green Zone"+"\n\Close>Day Open",text_size=size.normal, width=TableWidth,height=Tableheight,text_color=color.black,bgcolor=Clep>Openp?color.rgb(175, 209, 175):na, text_halign=text.align_left)
    table.cell(t, 3, 1, "Gray Zone"+"\n\Close In Gray Zone",text_size=size.normal, width=TableWidth,text_color=color.black,height=Tableheight,text_halign=text.align_left,bgcolor=(Clep>MAdn) and (Clep<MAup )?color.rgb(236, 236, 238):na)
    table.cell(t, 4, 1, "Red Zone"+"\n\Close<Day Open",text_size=size.normal, width=TableWidth,text_color=color.black, height=Tableheight,text_halign=text.align_left,bgcolor=Clep<Openp?color.rgb(180,151,151):na)
    table.cell(t, 5, 1, "Range Bound Market"+"\n\GZn Bars>Non GZn Bars",text_size=size.normal, width=TableWidth,text_color=color.black,height=Tableheight,text_halign=text.align_left,
     bgcolor=Gray_Zone_Bars>0 ? color.gray:na)
    
