BBFIB 
// 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('Regular/Intraday BB/FIB ', overlay=true, max_bars_back=5000)

//Regular/Intraday inputs

var grp1="regular/Intraday"
var grp2="regular"
var grp3="Fibonacci/Bolloinger Band"
var grp4="Fibonacci"

Regular=input.bool(defval=true,title="Regular",group=grp1,inline=grp2)
Intraday = input.bool(defval=false,title="Intraday",group=grp1,inline=grp2)

Fib=input.bool(defval=true,title="Fibonacci",group=grp3,inline=grp4)
BB=input.bool(defval=false,title="Bollinger Bands",group=grp3,inline=grp4)

//Intraday Length


Open= request.security(syminfo.tickerid ,"D",close[1],barmerge.gaps_off, barmerge.lookahead_on) 
Nifnum= ta.change(Open)
Lb=int(math.max(1, nz(ta.barssince(Nifnum)) + 1))


//Length for moving average and Fib Levels

var gpr5=" Lenghths "
var gpr6="Len"

Len1= input.int(title="Len Reg BB ", defval=20,minval=2, maxval=1000,group=gpr5,inline=gpr6)
Len2=input.int(defval=20,title="Len Reg Fib",group=gpr5,inline=gpr6)
Len3=input.int(defval=20,title="Len for MA",group=gpr5,inline=gpr6)


//Oscillations 

var gpr7="Input for Oscillation"
var gpr8="S"

Regular_Sou=input.string(title="Regular Osc",defval='SMA', options=['SMA', 'EMA', 'WMA', 'VWMA'],group=gpr7,inline=gpr8)
Lb_sou=input.string(title="Intraday Osc",defval='SMA', options=['SMA', 'WMA', 'VWMA'],group=gpr7,inline=gpr8)
MA_sou=input.string(title="Moving Avg Osc",defval='SMA', options=['SMA', "EMA",'WMA', 'VWMA'],group=gpr7,inline=gpr8)

//Source

Src = input(title="Source", defval=close,group="Source")

//Moving average calculations for BB

Ma7=Regular_Sou == 'SMA' ? ta.sma(Src, Len1) : Regular_Sou == 'EMA' ? ta.ema(Src, Len1) : Regular_Sou == 'WMA' ? 
 ta.wma(Src,Len1) : Regular_Sou == 'VWMA' ? ta.vwma(Src, Len1) :  na

//Intra day moving average calculations

Ma8=Lb_sou == 'SMA' ? ta.sma(Src, Lb) :  Lb_sou == 'WMA' ? ta.wma(Src,Lb) :
 Lb_sou == 'VWMA' ? ta.vwma(Src, Lb) :  na

//General Moving average Calculations

MA=MA_sou == 'SMA' ? ta.sma(Src,Len3 ) : MA_sou == 'EMA' ? ta.ema(Src,Len3): MA_sou == 'WMA' ? ta.wma(Src,Len3) :
 MA_sou == 'VWMA' ? ta.vwma(Src, Len3) :  na



//BB multiplier 

Mul=input.float(defval=2.00,title="BB Multiplier",minval=2.00,maxval=100.00,group="Others")



//BB Calculations

Basisa=Intraday== true ?  ta.stdev(Src,Lb):  ta.stdev(Src,Len1)
Upb= Intraday and BB ==true ?Ma8+Basisa*Mul:Regular and BB ==true? Ma7+Basisa*2 :na
Dnb= Intraday and BB ==true ?Ma8-Basisa*Mul:Regular and BB ==true? Ma7-Basisa*2 :na

//Fibonacci Levels for BB

Dif=Upb-Dnb
Lev1=Upb-Dif*0.236
Lev2=Upb-Dif*0.382
Lev3=Upb-Dif*0.50
Lev4=Upb-Dif*0.618
Lev5=Upb-Dif*0.786



//Plots for BB

plot(MA,title="Moving Averegae",color=color.rgb(140, 8, 163))
U=plot(Upb,"Upb",color.green)
L=plot(Dnb,"Dnb",color.red)
U1=plot(Lev1,"23.6", color.blue)
U2=plot(Lev2,"38.2", color.blue)
M=plot(Lev3,"50", color.blue)
D1=plot(Lev4,"61.8", color.blue)
D2=plot(Lev5,"78.6", color.blue)

//Fill colors for BB

fill(U,U2,color.rgb(157, 225, 159),"UP zone",show_last=500)
fill(D1,L,color.rgb(228, 162, 162),"DN zone",show_last=500)
fill(U2,D1,color.rgb(216, 220, 233),"Gray Zone",show_last=500)

//Fibonacci High Low for Chart

//Fibonacci High and Low for Intra day

Hid=ta.highest(Src,Lb)
Lid=ta.lowest(Src,Lb)

//Fibonacci High and Low for Regular

Hid1=ta.highest(Src,Len2)
Lid1=ta.lowest(Src,Len2)

//Fibonacci for Intraday and Regular

Uph=Regular and Fib  ==true ? Hid1:Intraday and Fib ?Hid:na
Dnh= Regular and Fib  ==true ? Lid1:Intraday and Fib ?Lid:na

//Fibonacci Levels for Chart

Difh=Uph-Dnh
Le1=Uph-Difh*0.236
Le2=Uph-Difh*0.382
Le3=Uph-Difh*0.50
Le4=Uph-Difh*0.618
Le5=Uph-Difh*0.786

//Plots for Fibonacci levels for Chart

Uh=plot(Uph,"Upb",color.green)
Lh=plot(Dnh,"Dnb",color.red)
Uh1=plot(Le1,"23.6", color.blue)
Uh2=plot(Le2,"38.2", color.blue)
Mh=plot(Le3,"50", color.blue)
Dh1=plot(Le4,"61.8", color.blue)
Dh2=plot(Le5,"78.6", color.blue)

//Fill colors for Fibonacci Chart

fill(Uh,Uh2,color.rgb(157, 225, 159),"UP zone",show_last=500)
fill(Dh1,Lh,color.rgb(228, 162, 162),"DN zone",show_last=500)
fill(Uh2,Dh1,color.rgb(216, 220, 233),"Gray Zone",show_last=500)




//Labels for BB

Chper = time -time[1]
Chper := ta.change(Chper) > 0 ? Chper[1] : Chper

Lab1=label.new(x=time+Chper*30,y=Upb,xloc=xloc.bar_time,yloc=yloc.price,text="HID",textcolor=color.blue,
 size=size.small,style=label.style_none)
max_bars_back(Lab1,500)
label.delete(Lab1[1])

Lab2=label.new(x=time+Chper*30,y=Lev1,xloc=xloc.bar_time,yloc=yloc.price,text="78.6/23.6",textcolor=color.blue,
 size=size.small,style=label.style_none)
label.delete(Lab2[1])
max_bars_back(Lab2,500)

Lab3=label.new(x=time+Chper*30,y=Lev2,xloc=xloc.bar_time,yloc=yloc.price,text="61.8/38.2",textcolor=color.blue,
 size=size.small,style=label.style_none)
label.delete(Lab3[1])
max_bars_back(Lab3,500)

Lab4=label.new(x=time+Chper*30,y=Lev3,xloc=xloc.bar_time,yloc=yloc.price,text="50",textcolor=color.blue,
 size=size.small,style=label.style_none)
label.delete(Lab4[1])
max_bars_back(Lab4,500)

Lab5=label.new(x=time+Chper*30,y=Lev4,xloc=xloc.bar_time,yloc=yloc.price,text="38.2/61.8",textcolor=color.blue,
 size=size.small,style=label.style_none)
label.delete(Lab5[1])
max_bars_back(Lab5,500)

Lab6=label.new(x=time+Chper*30,y=Lev5,xloc=xloc.bar_time,yloc=yloc.price,text="23.6/78.6",textcolor=color.blue,
 size=size.small,style=label.style_none)
label.delete(Lab6[1])
max_bars_back(Lab6,500)

Lab7=label.new(x=time+Chper*30,y=Dnb,xloc=xloc.bar_time,yloc=yloc.price,text="LID",textcolor=color.white,
 size=size.small,style=label.style_none)
label.delete(Lab7[1])
max_bars_back(Lab7,500)


plotcandle(open,high,low,close,"Bcandle",open>close? color.red:color.green)

//Labels for Fibonacci Chart

Lab11=label.new(x=time+Chper*30,y=Uph,xloc=xloc.bar_time,yloc=yloc.price,text="HID",textcolor=color.blue,
 size=size.small,style=label.style_none)
max_bars_back(Lab11,500)
label.delete(Lab11[1])

Lab12=label.new(x=time+Chper*30,y=Le1,xloc=xloc.bar_time,yloc=yloc.price,text="78.6/23.6",textcolor=color.blue,
 size=size.small,style=label.style_none)
label.delete(Lab12[1])
max_bars_back(Lab12,500)

Lab13=label.new(x=time+Chper*30,y=Le2,xloc=xloc.bar_time,yloc=yloc.price,text="61.8/38.2",textcolor=color.blue,
 size=size.small,style=label.style_none)
label.delete(Lab13[1])
max_bars_back(Lab13,500)

Lab14=label.new(x=time+Chper*30,y=Le3,xloc=xloc.bar_time,yloc=yloc.price,text="50",textcolor=color.blue,
 size=size.small,style=label.style_none)
label.delete(Lab14[1])
max_bars_back(Lab14,500)

Lab15=label.new(x=time+Chper*30,y=Le4,xloc=xloc.bar_time,yloc=yloc.price,text="38.2/61.8",textcolor=color.blue,
 size=size.small,style=label.style_none)
label.delete(Lab15[1])
max_bars_back(Lab15,500)

Lab16=label.new(x=time+Chper*30,y=Le5,xloc=xloc.bar_time,yloc=yloc.price,text="23.6/78.6",textcolor=color.blue,
 size=size.small,style=label.style_none)
label.delete(Lab16[1])
max_bars_back(Lab16,500)

Lab17=label.new(x=time+Chper*30,y=Dnh,xloc=xloc.bar_time,yloc=yloc.price,text="LID",textcolor=color.white,
 size=size.small,style=label.style_none)
label.delete(Lab17[1])
max_bars_back(Lab17,500)



//Messages for Intraday and Regular combinations on Table

Msg1=Intraday and BB ? "You are on Intraday/BB":na
Msg2=Intraday and Fib ? "You are on Intraday/Fibonacci":na
Msg3=Regular and BB ? "You are on Regular/BB":na
Msg4=Regular  and Fib ? "You are on Regular/Fibonacci":na
Msg5=(Intraday and BB and Intraday and Fib and Regular and BB and Regular  and Fib) ?" Choose only one Combination":
      (Intraday and BB and Intraday and Fib and Regular and BB)?" Choose only  one Combination": (Intraday and BB and Intraday and Fib) ?   
       " Choose only one Combination":(Regular and BB and Regular and Fib) ?" Choose only one Combination":(Regular and BB and Intraday and BB) ?
       " Choose only one Combination":(Intraday and Fib and Regular and Fib)?" Choose only one Combination":  na





//TABLE

BorderThickness = 1
TableWidth = 0
Tableheight=0



TableLocation = input.string("bottom_right", 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 top_ left")

TableTextSize = input.string("Auto", title='Select Table Text Size',options=["Auto","Huge","Large","Normal","Small",
  "Tiny"] , tooltip="Default Text Size is Auto")


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




var table t = table.new (position=TableLocation,columns=10,rows= 10, bgcolor=color.rgb(231, 231, 223),frame_color=color.black,frame_width=2,
 border_color=color.gray,border_width=BorderThickness)

if barstate.islast
    
   
    
    table.cell(t,1, 1, str.tostring(Msg1),text_size=celltextsize, width=TableWidth, text_color=color.blue,
     height=Tableheight, text_halign=text.align_left)
    table.cell(t,2, 1, str.tostring(Msg2),text_size=celltextsize, width=TableWidth, text_color= color.blue, 
     height=Tableheight, text_halign=text.align_left)
    table.cell(t,3, 1, str.tostring(Msg3),text_size=celltextsize, width=TableWidth, text_color=color.blue,
     height=Tableheight, text_halign=text.align_left)
    table.cell(t,4, 1, str.tostring(Msg4),text_size=celltextsize, width=TableWidth, text_color= color.blue, 
     height=Tableheight, text_halign=text.align_left)
    table.cell(t,5, 1, str.tostring(Msg5),text_size=celltextsize, width=TableWidth, text_color= color.red, 
     height=Tableheight, text_halign=text.align_left)
    
    




