BB Support & Resistance
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © grnghost

//@version=5
indicator("BB Support & Resitance", "BB S&R", overlay = true)

//Input
grp1 = "Bollinger Band Settings"
bb_len = input.int(20, "Bollinger Band Length =", minval = 1, group = grp1)
multi = input.int(2, "Multipler =", minval = 1, group = grp1)

grp2 = "Time Frame Selection"
TF1 = input.timeframe("60", "Time Frame 1", inline = "row1", group = grp2) // options=['15', '30','60', "120", "240", 'D', 'W', "M"])
show_tf1 = input.bool(true, "Show Time Frame 1", inline = "row1" , group = grp2)
TF2 = input.timeframe("D", "Time Frame 2", inline = "row2" , group = grp2) //, options=['15', '30', '60', "120","240", 'D', 'W',"M"])
show_tf2 = input.bool(true, "Show Time Frame 2", inline = "row2" , group = grp2)
TF3 = input.timeframe("W", "Time Frame 3", inline = "row3", group = grp2) // options=['30','60', "120", "240", 'D', 'W', "M"])
show_tf3  = input.bool(false, "Show Time Frame 3", inline = "row3" , group = grp2) 

grp3 = "Color Selection"
cbtf = input.bool(false, "Color By Time Frame", group = grp3)
tf1_col = input.color(color.new(color.yellow, 40), "Time Frame 1 Color", group = grp3)
tf2_col = input.color(color.new(color.yellow, 20), "Time Frame 2 Color", group = grp3)
tf3_col = input.color(color.new(color.yellow, 0), "Time Frame 3 Color", group = grp3)

//Default Color Definition
upper = color.new(color.red,50)//close < trend1 ? color.new(color.red,50) : color.new(color.green,50) //input.color(color.new(color.yellow,60), title =  "Time Frame 1 Line Color")
basis = color.new(color.yellow, 50)  // close < trend1 ? color.new(color.red,50) : color.new(color.green,50)//input.color(color.new(color.yellow,60), title =  "Time Frame 2 Line Color")
lower = color.new(color.green, 50)// close < trend1 ? color.new(color.red,50) : color.new(color.green,50) //input.color(color.new(color.yellow,60), title =  "Time Frame 3 Line Color")

//Calculate Bollinger Bands 
[mid, up, lo] = ta.bb(close[1], bb_len, multi)
[mid_tf1, up_tf1, lo_tf1] = request.security(syminfo.tickerid, TF1, [mid, up, lo] )  
[mid_tf2, up_tf2, lo_tf2] = request.security(syminfo.tickerid, TF2, [mid, up, lo] ) 
[mid_tf3, up_tf3, lo_tf3] = request.security(syminfo.tickerid, TF3, [mid, up, lo] ) 

//Plot Support Resist Lines
mid_ft1_ln = show_tf1 ? line.new(bar_index -1, mid_tf1, bar_index, mid_tf1, extend = extend.both, color = cbtf ? tf1_col : basis, width = 2, style = line.style_dotted) : na// color.new(color.yellow,50))
line.delete(mid_ft1_ln[1])
up_ft1_ln = show_tf1 ? line.new(bar_index -1, up_tf1, bar_index, up_tf1, extend = extend.both, color = cbtf ? tf1_col : upper, width = 2, style = line.style_dotted) : na// color.new(color.yellow,50))
line.delete(up_ft1_ln[1])
lo_ft1_ln = show_tf1 ? line.new(bar_index -1, lo_tf1, bar_index, lo_tf1, extend = extend.both, color = cbtf ? tf1_col : lower, width = 2, style = line.style_dotted) : na //color.new(color.yellow,50))
line.delete(lo_ft1_ln[1])

if show_tf2
    mid_ft2_ln = show_tf2 ? line.new(bar_index -1, mid_tf2, bar_index, mid_tf2, extend = extend.both, color = cbtf ? tf2_col : basis, width = 2, style = line.style_dashed) :na// color.yellow)
    line.delete(mid_ft2_ln[1])
    up_ft2_ln = show_tf2 ? line.new(bar_index -1, up_tf2, bar_index, up_tf2, extend = extend.both, color = cbtf ? tf2_col : upper, width = 2, style = line.style_dashed) : na// color.yellow)
    line.delete(up_ft2_ln[1])
    lo_ft2_ln = show_tf2 ? line.new(bar_index -1, lo_tf2, bar_index, lo_tf2, extend = extend.both, color = cbtf ? tf2_col : lower, width = 2, style = line.style_dashed) : na// color.yellow)
    line.delete(lo_ft2_ln[1])

if show_tf3
    mid_ft3_ln = show_tf3 ? line.new(bar_index -1, mid_tf3, bar_index, mid_tf3, extend = extend.both, color = cbtf ? tf3_col : basis, width = 3, style = line.style_solid) : na
    line.delete(mid_ft3_ln[1])
    up_ft3_ln = show_tf3 ? line.new(bar_index -1, up_tf3, bar_index, up_tf3, extend = extend.both, color = cbtf ? tf3_col : upper, width = 3, style = line.style_solid) : na
    line.delete(up_ft3_ln[1])
    lo_ft3_ln = show_tf3 ? line.new(bar_index -1, lo_tf3, bar_index, lo_tf3, extend = extend.both, color = cbtf ? tf3_col : lower, width = 3, style = line.style_solid) : na
    line.delete(lo_ft3_ln[1])