OPEN-SOURCE SCRIPT

luusang

//version=5
indicator('Swing', shorttitle='GoodLuck', overlay=true, max_lines_count=500)

//-----Input-------
customTF = input.timeframe(defval="",title = "Show Other TimeFrame")
GroupGann = "Gann"
showGann = input.bool(false, 'Show Gann/Color/Width', group = GroupGann, inline = "Gann1")
colorGann = input.color(color.aqua, '', group = GroupGann,inline = "Gann1")
widthGann = input.int(defval=1,title = "",minval=1,step=1, group = GroupGann,inline = "Gann1")

GroupSGann = "Swing of Gann"

showSGann = input.bool(true, 'Show Swing/Color/Width', group = GroupSGann,inline = "Swing1")
colorSGann = input.color(color.blue, '', group = GroupSGann,inline = "Swing1")
widthSGann = input.int(defval=1,title = "",minval=1,step=1, group = GroupSGann,inline = "Swing1")

showChoCh = input.bool(false, 'Show ChoCh/Color/Width', group = GroupSGann,inline = "Choch")
colorChoch = input.color(color.red, '', group = GroupSGann,inline = "Choch")
widthChoch = input.int(defval=1,title = "",minval=1,step=1, group = GroupSGann,inline = "Choch")

show2ChoCh = input.bool(false, 'Show 2Choch/Color/Width', group = GroupSGann,inline = "2Choch")
color2Choch = input.color(color.purple, '', group = GroupSGann,inline = "2Choch")
width2Choch = input.int(defval=1,title = "",minval=1,step=1, group = GroupSGann,inline = "2Choch")

showtargetOTL = input.bool(true, 'Show Target OTL/Color/Width',group = GroupSGann, inline = "target")
colortargetOTL = input.color(color.purple, '', group = GroupSGann, inline = "target")
widthtargetOTL = input.int(defval=1,title = "",minval=1,step=1, group = GroupSGann,inline = "target")

showLabel = input.bool(false, 'Show Label TimeFrame',group = GroupSGann)

//showLinePrice = input.bool(true, 'Show LinePrice/Color/Width', group = GroupSGann,inline = "LinePrice")
//colorLinePrice = input.color(color.blue, '', group = GroupSGann,inline = "LinePrice")
//widthLinePrice = input.int(defval=1,title = "",minval=1,step=1, group = GroupSGann,inline = "LinePrice")

///tùy chọn line//
lineGann = input.string(title="",options=['(─)', '(╌)', '(┈)'],defval='(╌)', group = GroupGann, inline = "Gann1")
lineStyleGann = lineGann == "(┈)" ? line.style_dotted : lineGann == "(╌)" ? line.style_dashed : line.style_solid

lineSGann = input.string(title="",options=["(─)", "(╌)", "(┈)"],defval="(─)", group = GroupSGann, inline = "Swing1")
lineStyleSGann = lineSGann == "(┈)" ? line.style_dotted : lineSGann == "(╌)" ? line.style_dashed : line.style_solid

line2Choch = input.string(title="",options=["(─)", "(╌)", "(┈)"],defval="(╌)", group = GroupSGann, inline = "2Choch")
lineStyle2Choch = line2Choch == "(┈)" ? line.style_dotted : line2Choch == "(╌)" ? line.style_dashed : line.style_dashed


lineChoch = input.string(title="",options=["(─)", "(╌)", "(┈)","(←)", "(→)", "(↔)"],defval="(─)", group = GroupSGann, inline = "Choch")
lineStyleChoch = lineChoch == "(┈)" ? line.style_dotted : lineChoch == "(╌)" ? line.style_dashed : lineChoch == "(←)" ? line.style_arrow_left : lineChoch == "(→)" ? line.style_arrow_right : lineChoch == "(↔)" ? line.style_arrow_both : line.style_solid

linetargetOTL = input.string(title="",options=["(─)", "(╌)", "(┈)","(←)", "(→)", "(↔)"],defval="(→)", group = GroupSGann, inline = "target")
lineStyletargetOTL = linetargetOTL == "(┈)" ? line.style_dotted : linetargetOTL == "(╌)" ? line.style_dashed : linetargetOTL == "(←)" ? line.style_arrow_left : linetargetOTL == "(→)" ? line.style_arrow_right : linetargetOTL == "(↔)" ? line.style_arrow_both : line.style_arrow_both

//in out side bar//
OSB_up_down ="Ousidebar up down"

//////////////////////////Global//////////////////////////
var arrayLineTemp = array.new_line()
// Funtion
f_resInMinutes() =>
_resInMinutes = timeframe.multiplier * (
timeframe.isseconds ? 1. / 60. :
timeframe.isminutes ? 1. :
timeframe.isdaily ? 1440. :
timeframe.isweekly ? 10080. :
timeframe.ismonthly ? 43800. : na)
// Converts a resolution expressed in minutes into a string usable by "security()"
f_resFromMinutes(_minutes) =>
_minutes < 1 ? str.tostring(math.round(_minutes*60)) + "S" :
_minutes < 60 ? str.tostring(math.round(_minutes)) + "m" :
_minutes < 1440 ? str.tostring(math.round(_minutes/60)) + "H" :
_minutes < 10080 ? str.tostring(math.round(math.min(_minutes / 1440, 7))) + "D" :
_minutes < 43800 ? str.tostring(math.round(math.min(_minutes / 10080, 4))) + "W" :
str.tostring(math.round(math.min(_minutes / 43800, 12))) + "M"
f_tfRes(_res,_exp) =>
request.security(syminfo.tickerid,_res,_exp,lookahead=barmerge.lookahead_on)

var arrayLineChoCh = array.new_line()
var label labelTF = label.new(time, close, text = "",color = color.new(showSGann ? colorSGann : colorGann,95), textcolor = showSGann ? colorSGann : colorGann,xloc = xloc.bar_time, textalign = text.align_left)
var line lineChoChOTLTarget = line.new(x1= time , y1=close,x2=time, y2= close,color = showSGann ? colortargetOTL : colorGann,xloc = xloc.bar_time,style = lineStyletargetOTL,width = widthtargetOTL)
var line lineBosOTLTarget = line.new(x1= time , y1=close,x2=time, y2= close,color = showSGann ? colortargetOTL : colorGann,xloc = xloc.bar_time,style = lineStyletargetOTL, width = widthtargetOTL)
//var line line_Price = line.new(x1= time , y1=close,x2=time, y2= close,color = showSGann ? colorLinePrice : colorGann,xloc = xloc.bar_time,style = lineStyleLinePrice, width = widthLinePrice)


styleGann = showSGann ? line.style_dashed : line.style_solid
var arrayXGann = array.new_int(5,time)
var arrayYGann = array.new_float(5,close)
var arrayLineGann = array.new_line()
int drawLineGann = 0

_high = high
_low = low
_close = close
_open = open
if(customTF != timeframe.period)
_high := f_tfRes(customTF,high)
_low := f_tfRes(customTF,low)
_close := f_tfRes(customTF,close)
_open := f_tfRes(customTF,open)

highPrev = _high
lowPrev = _low
// drawLineGann => 2:Tiếp tục 1:Đảo chiều; // Outsidebar 2:Tiếp tục 3:Tiếp tục và Đảo chiều 4 : Đảo chiều 2 lần
drawLineGann := 0
if(_high[0] > highPrev[1] and _low[0] > lowPrev[1])
if(array.get(arrayYGann,0) > array.get(arrayYGann,1))
if(_high[0] <= high)
array.set(arrayXGann, 0, time)
array.set(arrayYGann, 0, _high[0])
drawLineGann := 2
else
array.unshift(arrayXGann,time)
array.unshift(arrayYGann,_high[0])
drawLineGann := 1
else if(_high[0] < highPrev[1] and _low[0] < lowPrev[1])
if(array.get(arrayYGann,0) > array.get(arrayYGann,1))
array.unshift(arrayXGann,time)
array.unshift(arrayYGann,_low[0])
drawLineGann := 1
else
if(_low[0] >= low)
array.set(arrayXGann, 0, time)
array.set(arrayYGann, 0, _low[0])
drawLineGann := 2
else if(_high[0] >= highPrev[1] and _low[0] < lowPrev[1] or _high[0] > highPrev[1] and _low[0] <= lowPrev[1])
if(array.get(arrayYGann,0) > array.get(arrayYGann,1))
if(_high[0] >= array.get(arrayYGann,0) and array.get(arrayYGann,1) < _low[0])
if(_high[0] <= high)
array.set(arrayXGann, 0, time)
array.set(arrayYGann, 0, _high[0])
drawLineGann := 2
else if(_high[0] >= array.get(arrayYGann,0) and array.get(arrayYGann,1) > _low[0])
if(_close < _open)
if(_high[0] <= high)
array.set(arrayXGann, 0, time)
array.set(arrayYGann, 0, _high[0])
array.unshift(arrayXGann,time)
array.unshift(arrayYGann,_low[0])
drawLineGann := 3
else
array.unshift(arrayXGann,time)
array.unshift(arrayYGann,_low[0])
array.unshift(arrayXGann,time)
array.unshift(arrayYGann,_high[0])
drawLineGann := 4
else if(array.get(arrayYGann,0) < array.get(arrayYGann,1))
if(_low[0] <= array.get(arrayYGann,0) and _high[0] < array.get(arrayYGann,1))
if(_low[0] >= low)
array.set(arrayXGann, 0, time)
array.set(arrayYGann, 0, _low[0])
drawLineGann := 2
else if(_low[0] <= array.get(arrayYGann,0) and _high[0] > array.get(arrayYGann,1))
if(_close > _open)
if(_low[0] >= low)
array.set(arrayXGann, 0, time)
array.set(arrayYGann, 0, _low[0])
array.unshift(arrayXGann,time)
array.unshift(arrayYGann,_high[0])
drawLineGann := 3
else
array.unshift(arrayXGann,time)
array.unshift(arrayYGann,_high[0])
array.unshift(arrayXGann,time)
array.unshift(arrayYGann,_low[0])
drawLineGann := 4
else if((_high[0] <= highPrev[1] and _low[0] >= lowPrev[1]))
highPrev := highPrev[1]
lowPrev := lowPrev[1]
if(f_resInMinutes() < f_tfRes(customTF,f_resInMinutes()) and drawLineGann == 0)
if(array.get(arrayYGann,0) > array.get(arrayYGann,1))
if(array.get(arrayYGann,0) <= high)
array.set(arrayXGann, 0, time)
drawLineGann := 2
else
if(array.get(arrayYGann,0) >= low)
array.set(arrayXGann, 0, time)
drawLineGann := 2
if(showGann and f_resInMinutes() <= f_tfRes(customTF,f_resInMinutes()))
if(drawLineGann == 2)
if(array.size(arrayLineGann) >0)
line.set_xy2(array.get(arrayLineGann,0),array.get(arrayXGann,0),array.get(arrayYGann,0))
else
array.unshift(arrayLineGann,line.new(array.get(arrayXGann,1),array.get(arrayYGann,1),array.get(arrayXGann,0),array.get(arrayYGann,0), color = colorGann,xloc = xloc.bar_time, style = lineStyleGann,width = widthGann))
else if(drawLineGann == 1)
array.unshift(arrayLineGann,line.new(array.get(arrayXGann,1),array.get(arrayYGann,1),array.get(arrayXGann,0),array.get(arrayYGann,0), color = colorGann,xloc = xloc.bar_time, style = lineStyleGann,width = widthGann))
else if(drawLineGann == 3)
if(array.size(arrayLineGann) >0)
line.set_xy2(array.get(arrayLineGann,0),array.get(arrayXGann,1),array.get(arrayYGann,1))
else
array.unshift(arrayLineGann,line.new(array.get(arrayXGann,2),array.get(arrayYGann,2),array.get(arrayXGann,1),array.get(arrayYGann,1), color = colorGann,xloc = xloc.bar_time, style = lineStyleGann,width = widthGann))
array.unshift(arrayLineGann,line.new(array.get(arrayXGann,1),array.get(arrayYGann,1),array.get(arrayXGann,0),array.get(arrayYGann,0), color = colorGann,xloc = xloc.bar_time, style = lineStyleGann,width = widthGann))
else if(drawLineGann == 4)
array.unshift(arrayLineGann,line.new(array.get(arrayXGann,2),array.get(arrayYGann,2),array.get(arrayXGann,1),array.get(arrayYGann,1), color = colorGann,xloc = xloc.bar_time, style = lineStyleGann,width = widthGann))
array.unshift(arrayLineGann,line.new(array.get(arrayXGann,1),array.get(arrayYGann,1),array.get(arrayXGann,0),array.get(arrayYGann,0), color = colorGann,xloc = xloc.bar_time, style = lineStyleGann,width = widthGann))

//////////////////////////Swing Gann//////////////////////////
var arrayXSGann = array.new_int(5,time)
var arrayYSGann = array.new_float(5,close)
var arrayLineSGann = array.new_line()
int drawLineSGann = 0
int drawLineSGann1 = 0
bool runCheckChoChSGann = false
runCheckChoChSGann := runCheckChoChSGann[1]
if(showSGann)
if(math.max(array.get(arrayYSGann,0),array.get(arrayYSGann,1)) < math.min(array.get(arrayYGann,0),array.get(arrayYGann,1)) or math.min(array.get(arrayYSGann,0),array.get(arrayYSGann,1)) > math.max(array.get(arrayYGann,0),array.get(arrayYGann,1)))
//Khởi tạo bắt đầu
drawLineSGann1 := 5
array.set(arrayXSGann, 0, array.get(arrayXGann,1))
array.set(arrayYSGann, 0, array.get(arrayYGann,1))
array.unshift(arrayXSGann,array.get(arrayXGann,0))
array.unshift(arrayYSGann,array.get(arrayYGann,0))
// drawLineSGann kiểm tra điểm 1 => 13:Tiếp tục có sóng hồi // 12|19(reDraw):Tiếp tục không có sóng hồi // 14:Đảo chiều
if(array.get(arrayXGann,0) == array.get(arrayXGann,1))
if(array.get(arrayXSGann,0) >= array.get(arrayXGann,2) and array.get(arrayYSGann,0) != array.get(arrayYGann,1) and ((array.get(arrayYGann,1) > array.get(arrayYGann,2) and array.get(arrayYSGann,0) > array.get(arrayYSGann,1)) or (array.get(arrayYGann,1) < array.get(arrayYGann,2) and array.get(arrayYSGann,0) < array.get(arrayYSGann,1))))
drawLineSGann1 := 12
array.set(arrayXSGann, 0, array.get(arrayXGann,1))
array.set(arrayYSGann, 0, array.get(arrayYGann,1))
else if(array.get(arrayXSGann,0) <= array.get(arrayXGann,2))
if((array.get(arrayYSGann,0) > array.get(arrayYSGann,1) and array.get(arrayYGann,1) < array.get(arrayYSGann,1)) or (array.get(arrayYSGann,0) < array.get(arrayYSGann,1) and array.get(arrayYGann,1) > array.get(arrayYSGann,1)))
drawLineSGann1 := 14
runCheckChoChSGann := true
array.unshift(arrayXSGann,array.get(arrayXGann,1))
array.unshift(arrayYSGann,array.get(arrayYGann,1))
else if((array.get(arrayYSGann,0) > array.get(arrayYSGann,1) and array.get(arrayYGann,1) > array.get(arrayYSGann,0)) or (array.get(arrayYSGann,0) < array.get(arrayYSGann,1) and array.get(arrayYGann,1) < array.get(arrayYSGann,0)))
drawLineSGann1 := 13
_max = math.min(array.get(arrayYSGann,0),array.get(arrayYSGann,1))
_min = math.max(array.get(arrayYSGann,0),array.get(arrayYSGann,1))
_max_idx = 0
_min_idx = 0
for i = 2 to array.size(arrayXGann)
if(array.get(arrayXSGann,0) >= array.get(arrayXGann,i))
break
if(_min > array.get(arrayYGann,i))
_min := array.get(arrayYGann,i)
_min_idx := array.get(arrayXGann,i)
if(_max < array.get(arrayYGann,i))
_max := array.get(arrayYGann,i)
_max_idx := array.get(arrayXGann,i)
if(array.get(arrayYSGann,0) > array.get(arrayYSGann,1))
array.unshift(arrayXSGann,_min_idx)
array.unshift(arrayYSGann,_min)
else if(array.get(arrayYSGann,0) < array.get(arrayYSGann,1))
array.unshift(arrayXSGann,_max_idx)
array.unshift(arrayYSGann,_max)
array.unshift(arrayXSGann,array.get(arrayXGann,1))
array.unshift(arrayYSGann,array.get(arrayYGann,1))

if(f_resInMinutes() < f_tfRes(customTF,f_resInMinutes()))
if(array.get(arrayYSGann,0) == array.get(arrayYGann,1) and array.get(arrayXSGann,0) != array.get(arrayXGann,1))
array.set(arrayXSGann, 0, array.get(arrayXGann,1))
drawLineSGann1 := 19
if(f_resInMinutes() <= f_tfRes(customTF,f_resInMinutes()))
if(drawLineSGann1 == 12 or drawLineSGann1 == 19)
if(array.size(arrayLineSGann) >0)
line.set_xy2(array.get(arrayLineSGann,0),array.get(arrayXSGann,0),array.get(arrayYSGann,0))
else
array.unshift(arrayLineSGann,line.new(array.get(arrayXSGann,1),array.get(arrayYSGann,1),array.get(arrayXSGann,0),array.get(arrayYSGann,0), color = colorSGann,xloc = xloc.bar_time,width = widthSGann, style = lineStyleSGann))
else if(drawLineSGann1 == 14)
array.unshift(arrayLineSGann,line.new(array.get(arrayXSGann,1),array.get(arrayYSGann,1),array.get(arrayXSGann,0),array.get(arrayYSGann,0), color = colorSGann,xloc = xloc.bar_time,width = widthSGann,style = lineStyleSGann))
else if(drawLineSGann1 == 13)
array.unshift(arrayLineSGann,line.new(array.get(arrayXSGann,2),array.get(arrayYSGann,2),array.get(arrayXSGann,1),array.get(arrayYSGann,1), color = colorSGann,xloc = xloc.bar_time,width = widthSGann,style = lineStyleSGann))
array.unshift(arrayLineSGann,line.new(array.get(arrayXSGann,1),array.get(arrayYSGann,1),array.get(arrayXSGann,0),array.get(arrayYSGann,0), color = colorSGann,xloc = xloc.bar_time,width = widthSGann,style = lineStyleSGann))
else if(drawLineSGann1 == 15)
if(array.size(arrayLineSGann) >0)
line.set_xy2(array.get(arrayLineSGann,0),array.get(arrayXSGann,1),array.get(arrayYSGann,1))
else
array.unshift(arrayLineSGann,line.new(array.get(arrayXSGann,2),array.get(arrayYSGann,2),array.get(arrayXSGann,1),array.get(arrayYSGann,1), color = colorSGann,xloc = xloc.bar_time,width = widthSGann,style = lineStyleSGann))
array.unshift(arrayLineSGann,line.new(array.get(arrayXSGann,1),array.get(arrayYSGann,1),array.get(arrayXSGann,0),array.get(arrayYSGann,0), color = colorSGann,xloc = xloc.bar_time,width = widthSGann,style = lineStyleSGann))
if(runCheckChoChSGann)
runCheckChoChSGann := false
// ChoCh Trường hợp chữ N ngược, chữ N
if((array.get(arrayYSGann,3) > array.get(arrayYSGann,2) and array.get(arrayYSGann,3) < array.get(arrayYSGann,1) and array.get(arrayYSGann,0) < array.get(arrayYSGann,2)) or (array.get(arrayYSGann,3) < array.get(arrayYSGann,2) and array.get(arrayYSGann,3) > array.get(arrayYSGann,1) and array.get(arrayYSGann,0) > array.get(arrayYSGann,2)))
alert(syminfo.ticker + " : " + timeframe.period + " => Swing of Gann ChoCh" + (array.get(arrayYSGann,0) > array.get(arrayYSGann,1) ? "+ ⇑" : "- ⇓"))
if(showChoCh)
array.unshift(arrayLineChoCh,line.new(x1= array.get(arrayXSGann,2) , y1=array.get(arrayYSGann,2),x2=array.get(arrayXSGann,0), y2=array.get(arrayYSGann,2),color = colorChoch,xloc = xloc.bar_time,style = lineStyleChoch,width = widthChoch))
// ChoCh 2 Đầu Trường hợp chữ N ngược, chữ N
if(show2ChoCh and ((array.get(arrayYSGann,1) > array.get(arrayYSGann,3) and array.get(arrayYSGann,3) > array.get(arrayYSGann,4) and array.get(arrayYSGann,4) > array.get(arrayYSGann,2) and array.get(arrayYSGann,2) > array.get(arrayYSGann,0)) or (array.get(arrayYSGann,0) > array.get(arrayYSGann,2) and array.get(arrayYSGann,2) > array.get(arrayYSGann,4) and array.get(arrayYSGann,4) > array.get(arrayYSGann,3) and array.get(arrayYSGann,3) > array.get(arrayYSGann,1))))
line.set_width(array.get(arrayLineSGann,1),width2Choch)
line.set_style(array.get(arrayLineSGann,1), lineStyle2Choch)
line.set_color(array.get(arrayLineSGann,1),color2Choch)

// drawLineSGann kiểm tra điểm 0 => 3:Tiếp tục có sóng hồi // 2|9(reDraw):Tiếp tục không có sóng hồi // 4:Đảo chiều
if(array.get(arrayXSGann,0) >= array.get(arrayXGann,1) and array.get(arrayYSGann,0) != array.get(arrayYGann,0) and ((array.get(arrayYGann,0) > array.get(arrayYGann,1) and array.get(arrayYSGann,0) > array.get(arrayYSGann,1)) or (array.get(arrayYGann,0) < array.get(arrayYGann,1) and array.get(arrayYSGann,0) < array.get(arrayYSGann,1))))
drawLineSGann := 2
array.set(arrayXSGann, 0, array.get(arrayXGann,0))
array.set(arrayYSGann, 0, array.get(arrayYGann,0))
else if(array.get(arrayXSGann,0) <= array.get(arrayXGann,1))
if((array.get(arrayYSGann,0) > array.get(arrayYSGann,1) and array.get(arrayYGann,0) < array.get(arrayYSGann,1)) or (array.get(arrayYSGann,0) < array.get(arrayYSGann,1) and array.get(arrayYGann,0) > array.get(arrayYSGann,1)))
drawLineSGann := 4
runCheckChoChSGann := true
array.unshift(arrayXSGann,array.get(arrayXGann,0))
array.unshift(arrayYSGann,array.get(arrayYGann,0))
else if((array.get(arrayYSGann,0) > array.get(arrayYSGann,1) and array.get(arrayYGann,0) > array.get(arrayYSGann,0)) or (array.get(arrayYSGann,0) < array.get(arrayYSGann,1) and array.get(arrayYGann,0) < array.get(arrayYSGann,0)))
drawLineSGann := 3
_max = math.min(array.get(arrayYSGann,0),array.get(arrayYSGann,1))
_min = math.max(array.get(arrayYSGann,0),array.get(arrayYSGann,1))
_max_idx = 0
_min_idx = 0
for i = 1 to array.size(arrayXGann)
if(array.get(arrayXSGann,0) >= array.get(arrayXGann,i))
break
if(_min > array.get(arrayYGann,i))
_min := array.get(arrayYGann,i)
_min_idx := array.get(arrayXGann,i)
if(_max < array.get(arrayYGann,i))
_max := array.get(arrayYGann,i)
_max_idx := array.get(arrayXGann,i)
if(array.get(arrayYSGann,0) > array.get(arrayYSGann,1))
array.unshift(arrayXSGann,_min_idx)
array.unshift(arrayYSGann,_min)
else if(array.get(arrayYSGann,0) < array.get(arrayYSGann,1))
array.unshift(arrayXSGann,_max_idx)
array.unshift(arrayYSGann,_max)
array.unshift(arrayXSGann,array.get(arrayXGann,0))
array.unshift(arrayYSGann,array.get(arrayYGann,0))

if(f_resInMinutes() < f_tfRes(customTF,f_resInMinutes()))
if(array.get(arrayYSGann,0) == array.get(arrayYGann,0) and array.get(arrayXSGann,0) != array.get(arrayXGann,0))
array.set(arrayXSGann, 0, array.get(arrayXGann,0))
drawLineSGann := 9
if(f_resInMinutes() <= f_tfRes(customTF,f_resInMinutes()))
if(drawLineSGann == 2 or drawLineSGann == 9)
if(array.size(arrayLineSGann) >0)
line.set_xy2(array.get(arrayLineSGann,0),array.get(arrayXSGann,0),array.get(arrayYSGann,0))
else
array.unshift(arrayLineSGann,line.new(array.get(arrayXSGann,1),array.get(arrayYSGann,1),array.get(arrayXSGann,0),array.get(arrayYSGann,0), color = colorSGann,xloc = xloc.bar_time,width = widthSGann,style = lineStyleSGann))
else if(drawLineSGann == 4)
array.unshift(arrayLineSGann,line.new(array.get(arrayXSGann,1),array.get(arrayYSGann,1),array.get(arrayXSGann,0),array.get(arrayYSGann,0), color = colorSGann,xloc = xloc.bar_time,width = widthSGann,style = lineStyleSGann))
else if(drawLineSGann == 3)
array.unshift(arrayLineSGann,line.new(array.get(arrayXSGann,2),array.get(arrayYSGann,2),array.get(arrayXSGann,1),array.get(arrayYSGann,1), color = colorSGann,xloc = xloc.bar_time,width = widthSGann,style = lineStyleSGann))
array.unshift(arrayLineSGann,line.new(array.get(arrayXSGann,1),array.get(arrayYSGann,1),array.get(arrayXSGann,0),array.get(arrayYSGann,0), color = colorSGann,xloc = xloc.bar_time,width = widthSGann,style = lineStyleSGann))
else if(drawLineSGann == 5)
if(array.size(arrayLineSGann) >0)
line.set_xy2(array.get(arrayLineSGann,0),array.get(arrayXSGann,1),array.get(arrayYSGann,1))
else
array.unshift(arrayLineSGann,line.new(array.get(arrayXSGann,2),array.get(arrayYSGann,2),array.get(arrayXSGann,1),array.get(arrayYSGann,1), color = colorSGann,xloc = xloc.bar_time,width = widthSGann,style = lineStyleSGann))
array.unshift(arrayLineSGann,line.new(array.get(arrayXSGann,1),array.get(arrayYSGann,1),array.get(arrayXSGann,0),array.get(arrayYSGann,0), color = colorSGann,xloc = xloc.bar_time,width = widthSGann,style = lineStyleSGann))

if(runCheckChoChSGann)
runCheckChoChSGann := false
// ChoCh Trường hợp chữ N ngược, chữ N
if((array.get(arrayYSGann,3) > array.get(arrayYSGann,2) and array.get(arrayYSGann,3) < array.get(arrayYSGann,1) and array.get(arrayYSGann,0) < array.get(arrayYSGann,2)) or (array.get(arrayYSGann,3) < array.get(arrayYSGann,2) and array.get(arrayYSGann,3) > array.get(arrayYSGann,1) and array.get(arrayYSGann,0) > array.get(arrayYSGann,2)))
alert(syminfo.ticker + " : " + timeframe.period + " => Swing of Gann ChoCh" + (array.get(arrayYSGann,0) > array.get(arrayYSGann,1) ? "+ ⇑" : "- ⇓"))
if(showChoCh)
array.unshift(arrayLineChoCh,line.new(x1= array.get(arrayXSGann,2) , y1=array.get(arrayYSGann,2),x2=array.get(arrayXSGann,0), y2=array.get(arrayYSGann,2),color = colorChoch,xloc = xloc.bar_time,style = lineStyleChoch,width = widthChoch))
// ChoCh 2 Đầu Trường hợp chữ N ngược, chữ N
if(show2ChoCh and ((array.get(arrayYSGann,1) > array.get(arrayYSGann,3) and array.get(arrayYSGann,3) > array.get(arrayYSGann,4) and array.get(arrayYSGann,4) > array.get(arrayYSGann,2) and array.get(arrayYSGann,2) > array.get(arrayYSGann,0)) or (array.get(arrayYSGann,0) > array.get(arrayYSGann,2) and array.get(arrayYSGann,2) > array.get(arrayYSGann,4) and array.get(arrayYSGann,4) > array.get(arrayYSGann,3) and array.get(arrayYSGann,3) > array.get(arrayYSGann,1))))
line.set_width(array.get(arrayLineSGann,1),width2Choch)
line.set_style(array.get(arrayLineSGann,1),lineStyle2Choch)
line.set_color(array.get(arrayLineSGann,1),color2Choch)

///////////////////////Other//////////////////////////////////
if(f_resInMinutes() <= f_tfRes(customTF,f_resInMinutes()))
if(showSGann)
if(showtargetOTL)
if(drawLineSGann1 == 14)
line.set_xy1(lineChoChOTLTarget,array.get(arrayXSGann,3),array.get(arrayYSGann,3))
line.set_xy2(lineChoChOTLTarget,array.get(arrayXSGann,3),array.get(arrayYSGann,3)*2 - array.get(arrayYSGann,2))

else if(drawLineSGann1 == 13)
line.set_xy1(lineBosOTLTarget,array.get(arrayXSGann,3),array.get(arrayYSGann,3))
line.set_xy2(lineBosOTLTarget,array.get(arrayXSGann,3),array.get(arrayYSGann,3)*2 - array.get(arrayYSGann,2))

if(drawLineSGann == 4)
line.set_xy1(lineChoChOTLTarget,array.get(arrayXSGann,2),array.get(arrayYSGann,2))
line.set_xy2(lineChoChOTLTarget,array.get(arrayXSGann,2),array.get(arrayYSGann,2)*2 - array.get(arrayYSGann,1))

else if(drawLineSGann == 3)
line.set_xy1(lineBosOTLTarget,array.get(arrayXSGann,2),array.get(arrayYSGann,2))
line.set_xy2(lineBosOTLTarget,array.get(arrayXSGann,2),array.get(arrayYSGann,2)*2 - array.get(arrayYSGann,1))


if(showLabel and (barstate.islast or barstate.islastconfirmedhistory))
texLabel = f_resInMinutes() == f_tfRes(customTF,f_resInMinutes()) ? f_resFromMinutes(f_resInMinutes()) : f_resFromMinutes(f_tfRes(customTF,f_resInMinutes()))
label.set_xy(labelTF,array.get(arrayXSGann,0),array.get(arrayYSGann,0))
label.set_text(labelTF,texLabel)
label.set_style(labelTF,array.get(arrayYSGann,0) < array.get(arrayYSGann,1) ? label.style_label_upper_right : label.style_label_lower_right)
else if(showGann)
if(showLabel and (barstate.islast or barstate.islastconfirmedhistory))
texLabel = f_resInMinutes() == f_tfRes(customTF,f_resInMinutes()) ? f_resFromMinutes(f_resInMinutes()) : f_resFromMinutes(f_tfRes(customTF,f_resInMinutes()))
label.set_xy(labelTF,array.get(arrayXGann,0),array.get(arrayYGann,0))
label.set_text(labelTF,texLabel)
label.set_style(labelTF,array.get(arrayYGann,0) < array.get(arrayYGann,1) ? label.style_label_upper_right : label.style_label_lower_right)

///ema///
EMA = "EMA Setting"
len = input.int(21, minval=1, title="EMA Length")
len1= input.int(34, minval=1, title="EMA Length")
len2 = input.int(89, minval=1, title="EMA Length")
len3 = input.int(200, minval=1, title="EMA Length")
src = close
ema = ta.ema(src, len)
ema1 = ta.ema(close,len1) //34
ema2 = ta.ema(close,len2) //89
ema3 = ta.ema (close,len3)//200
up = ema > ema[1]
down = ema < ema[1]
mycolor = up ? #00a7bc : down ? #b91002 : color.blue
plot(ema, title="EMA", color=mycolor, linewidth=1)
//plot(ema1, title="EMA", color=mycolor, linewidth=1,editable = false)
//plot(ema2, title="EMA", color=mycolor, linewidth=1,editable = false)
//plot(ema3, title="EMA", color=mycolor, linewidth=1,editable = false)
table_color_up = color.new(#00a7bc,70)
table_color_down = color.new(#b91002,70)
//vẽ bảng ema + rsi//
t = table.new(position = position.bottom_right, columns = 5, rows = 9, bgcolor = #fde9c9)
table.cell(t, 0, 0, "Time",text_size = size.small)
table.cell(t, 1, 0, "EMA",text_size = size.small)
table.cell(t, 0, 1, "5m",text_size = size.small)
table.cell(t, 0, 2, "15m",text_size = size.small)
table.cell(t, 0, 3, "4h",text_size = size.small)
table.cell(t, 0, 4, "1d",text_size = size.small)



// tính toán giá EMA của các khung thời gian khác nhau
ema5 = request.security(syminfo.tickerid, "5", ta.ema(src, len))
ema15 = request.security(syminfo.tickerid, "15", ta.ema(src, len))
ema4h = request.security(syminfo.tickerid, "240", ta.ema(src, len))
ema1d = request.security(syminfo.tickerid, "D", ta.ema(src, len))

//tô màu cho bảng chứa EMA
ema5_color = ema5 < close ? table_color_up : table_color_down
ema15_color = ema15 < close ? table_color_up :table_color_down
ema4h_color = ema4h < close ? table_color_up : table_color_down
ema1d_color = ema1d < close ? table_color_up : table_color_down
//tính toán ema 123_5
//ema5_1 = request.security(syminfo.tickerid, "5", ta.ema(close, len1))
//ema5_2 = request.security(syminfo.tickerid, "5", ta.ema(close, len2))
//ema5_3 = request.security(syminfo.tickerid, "5", ta.ema(close, len3))
//trend_ema5_color = (ema5_1 > ema5_2 and ema5_2 > ema5_3) ? color.green : (ema5_1 > ema5_2 and ema5_2 < ema5_3)? color.new(color.green,70) : (ema5_1 < ema5_2 and ema5_2 < ema5_3) ? color.red : (ema5_1 < ema5_2 and ema5_2 > ema5_3) ? color.new(color.red,70) : color.orange
//tính toán ema 123_15
ema15_1 = request.security(syminfo.tickerid, "15", ta.ema(close, len1))
ema15_2 = request.security(syminfo.tickerid, "15", ta.ema(close, len2))
ema15_3 = request.security(syminfo.tickerid, "15", ta.ema(close, len3))
trend_ema15_color = (ema15_1 > ema15_2 and ema15_2 > ema15_3) ? color.green : (ema15_1 > ema15_2 and ema15_2 < ema15_3)? color.new(color.green,70) : (ema15_1 < ema15_2 and ema15_2 < ema15_3) ? color.red : (ema15_1 < ema15_2 and ema15_2 > ema15_3) ? color.new(color.red,70) : color.orange
//tính toán ema 123_4h
ema4h_1 = request.security(syminfo.tickerid, "240", ta.ema(close, len1))
ema4h_2 = request.security(syminfo.tickerid, "240", ta.ema(close, len2))
ema4h_3 = request.security(syminfo.tickerid, "240", ta.ema(close, len3))
trend_ema4h_color = (ema4h_1 > ema4h_2 and ema4h_2 > ema4h_3) ? color.green : (ema4h_1 > ema4h_2 and ema4h_2 < ema4h_3)? color.new(color.green,70) : (ema4h_1 < ema4h_2 and ema4h_2 < ema4h_3) ? color.red : (ema4h_1 < ema4h_2 and ema4h_2 > ema4h_3) ? color.new(color.red,70) : color.orange
// tính toán RSI của các khung thời gian khác nhau
rsi5 = request.security(syminfo.tickerid, "5", ta.rsi(src, 14))
rsi15 = request.security(syminfo.tickerid, "15", ta.rsi(src, 14))
rsi4h = request.security(syminfo.tickerid, "240", ta.rsi(src, 14))
rsi1d = request.security(syminfo.tickerid, "D", ta.rsi(src, 14))

//tô màu cho bảng chứa RSI
rsi5_color = rsi5 > 70 ? table_color_down : rsi5 < 30 ? table_color_up : color.orange
rsi15_color = rsi15 > 70 ? table_color_down : rsi15 < 30 ? table_color_up : color.orange
rsi4h_color = rsi4h > 70 ? table_color_down : rsi4h < 30 ? table_color_up : color.orange
rsi1d_color = rsi1d > 70 ? table_color_down : rsi1d < 30 ? table_color_up : color.orange

//tính toán % thay đổi 1d, 7d, 30d
// Lấy giá trị giá đóng cửa của các ngày trước đó
yesterday_close = request.security(syminfo.tickerid, "D", close[1])
last_week_close = request.security(syminfo.tickerid, "W", close[1])
last_month_close = request.security(syminfo.tickerid, "M", close[1])
// Tính toán phần trăm thay đổi giá so với các ngày trước đó
day_change = math.round((close / yesterday_close - 1) * 100,1)
week_change = math.round((close / last_week_close - 1) * 100,1)
month_change = math.round((close / last_month_close - 1) * 100,1)
//tô màu cho bảng chứa % thay đổi
day_change_color = day_change < 0 ? table_color_down : table_color_up
weak_change_color = week_change < 0 ? table_color_down : table_color_up
month_change_color = month_change < 0 ? table_color_down : table_color_up
// tính toán btc.d usdt.d dxy
btc_d = request.security("CRYPTOCAP:BTC.D", timeframe.period, close)
usdt_d = request.security("CRYPTOCAP:USDT.D", timeframe.period, close)
dxy = request.security("TVC:DXY", timeframe.period, close)
yesterday_btc_d = request.security("CRYPTOCAP:BTC.D", timeframe.period, close[1])
yesterday_usdt_d = request.security("CRYPTOCAP:USDT.D", timeframe.period, close[1])
yesterday_dxy = request.security("TVC:DXY", timeframe.period, close[1])
btc_d_color = btc_d < yesterday_btc_d ? table_color_down : table_color_up //too mau
usdt_d_color = usdt_d < yesterday_usdt_d ? table_color_down : table_color_up
dxy_color = dxy < yesterday_dxy ? table_color_down : table_color_up
// điền giá EMA vào bảng
table.cell(t, 1, 1, str.tostring(math.round(ema5, 3)), bgcolor = ema5_color,text_size = size.small)
table.cell(t, 1, 2, str.tostring(math.round(ema15, 3)), bgcolor = ema15_color,text_size = size.small)
table.cell(t, 1, 3, str.tostring(math.round(ema4h, 3)), bgcolor = ema4h_color,text_size = size.small)
table.cell(t, 1, 4, str.tostring(math.round(ema1d, 3)), bgcolor = ema1d_color,text_size = size.small)
// điền rsi vào bảng
show_info_2 = input.bool(title = "Show % RSI",defval = true,group = "Table Setting")
if show_info_2
table.cell(t, 2, 0, "RSI",text_size = size.small)
table.cell(t, 2, 1, str.tostring(math.round(rsi5,0)), bgcolor = rsi5_color,text_size = size.small)
table.cell(t, 2, 2, str.tostring(math.round(rsi15,0)), bgcolor = rsi15_color,text_size = size.small)
table.cell(t, 2, 3, str.tostring(math.round(rsi4h,0)), bgcolor = rsi4h_color,text_size = size.small)
table.cell(t, 2, 4, str.tostring(math.round(rsi1d,0)), bgcolor = rsi1d_color,text_size = size.small)
// điển % change vào bảng
show_info_1 = input.bool(title = "Show % Change 1d, 7d, 30d",defval = true,group = "Table Setting")
if show_info_1
table.cell(t,0,5,"%",text_size = size.small)
table.cell(t,1,5,"1d",text_size = size.small)
table.cell(t,2,5,"7d",text_size = size.small)
table.cell(t,3,5,"30d",text_size = size.small)
table.cell(t, 1, 6, str.tostring(day_change, "#.##") + "%",bgcolor= day_change_color,text_size = size.small)
table.cell(t, 2, 6, str.tostring(week_change, "#.##") + "%",bgcolor = weak_change_color,text_size = size.small)
table.cell(t, 3, 6, str.tostring(month_change,"#.##") + "%", bgcolor = month_change_color,text_size = size.small)
//điện btc.d usdt.d dxy vào bảng
Table_Info = "Table Setting"
show_info = input.bool(title = "Show BTC.D, USSDT, DXY",defval = true,group = "Table Setting")
if show_info
table.cell(t,0,7,"#",text_size = size.small)
table.cell(t,1,7,"BTC.D",text_size = size.small)
table.cell(t,2,7,"USDT.D",text_size = size.small)
table.cell(t,3,7,"DXY",text_size = size.small)
table.cell(t, 1, 8, str.tostring(btc_d, "#.##") + "%",bgcolor= btc_d_color,text_size = size.small)
table.cell(t, 2, 8, str.tostring(usdt_d, "#.##") + "%",bgcolor = usdt_d_color,text_size = size.small)
table.cell(t, 3, 8, str.tostring(dxy,"#.##") + "%", bgcolor = dxy_color,text_size = size.small)
//điền trend ema vào bảng
show_info_3 = input.bool(title = "Show Trend",defval = true,group = "Table Setting")
if show_info_3
table.cell(t,3,0,"Trend",text_size = size.small)
//table.cell(t,3,1,"",bgcolor = trend_ema5_color)
table.cell(t,3,2,"",bgcolor = trend_ema15_color)
table.cell(t,3,3,"",bgcolor = trend_ema4h_color)
//đánh dấu volume với các osb + nến bulish bearish
//indicator("Color Ultra Volume Background", shorttitle = "UV",overlay=true, max_boxes_count=500, max_labels_count=500, max_lines_count=500, max_bars_back=1000)
UltraVol = "Ultra volume background"
colorultravol = input.color(color.new(color.purple,50),"Color UltraVol",group = UltraVol)

//Công thức//
lengthVolumeMA = input.int(20,minval=1,title="Length of MA applied on Volume",group = UltraVol)
ratioUltraVolume = input.float(2.2,minval=1,title="Ultra High Volume Ratio", group = UltraVol)
float volumeMA = 0
volumeMA := nz(volumeMA[1]) + (volume-nz(volumeMA[1])) / lengthVolumeMA
ultraHighVolumeMin = volumeMA * ratioUltraVolume
volUltraHigh = volume >= ultraHighVolumeMin? true : false
// Màu volume
color_cande_up_down = input.color(color.new(color.purple,100),"Color UltraVol",group = OSB_up_down)

Candle_body = input.float(0.5, 'Height of Engulfing candle body (%)', group = OSB_up_down)
//scan nến osb-eng - mera mera//
osb_up = high > high[1] and low < low[1] and close > close[1]
osb_down = high > high[1] and low < low[1] and close < close[1]
eng_up = open >= close[1] and close > high[1] and (close - open) >= Candle_body*(high - low)
eng_down = open <= close[1] and close < low[1] and (open - close) >= Candle_body*(high - low)
mm_up = osb_up and close[2] > close[3] and close[1] > close[2]
mm_down = osb_down and close[2] < close[3] and close[1] < close[2]
cande_up = close > close[1]
cande_down = close < close[1]

//tô màu nến//
colorOSB_up = input.color(#0ba4f9,'colorOSB_up',inline = "Up Outside Bar",group = OSB_up_down)
colorOSB_down = input.color(#fd1700,'colorOSB_dowwn',inline = "Down Outside Bar",group = OSB_up_down)
barcolor(osb_up ? colorOSB_up : na, title="Up Outside Bar")
barcolor(osb_down ? colorOSB_down : na, title="Down Outside Bar")
coloreng_up = input.color(#00a7bc,'coloreng_up',inline = "Bullish Bar",group = OSB_up_down)
coloreng_down = input.color(#b91002,'coloreng_dowwn',inline = "Bearlish Bar",group = OSB_up_down)
barcolor(eng_up ? coloreng_up : na, title="Bullish Bar")
barcolor(eng_down ? coloreng_down : na, title="Bearlish Bar")

//đánh dấu vol - mera mera
plotshape(volUltraHigh and cande_up,"Candle_UltraVol_up_shape",style = shape.circle,location = location.belowbar,color = coloreng_up,size = size.tiny)
plotshape(volUltraHigh and cande_down,"Candle_UltraVol_down_shape",style = shape.circle,location = location.abovebar,color = coloreng_down,size = size.tiny)
plotshape(volUltraHigh and osb_up,"Up Outside Bar+ Vol",style = shape.circle,location = location.belowbar,color = color_cande_up_down,size = size.tiny)
plotshape(volUltraHigh and osb_down,"Down Outside Bar+ Vol",style = shape.circle,location = location.abovebar,color = color_cande_up_down,size = size.tiny)
plotshape(volUltraHigh and eng_up,"Bullish Bar + Vol",style = shape.circle,location = location.belowbar,color = color_cande_up_down,size = size.tiny)
plotshape(volUltraHigh and eng_down,"Bearlish + Vol Bar",style = shape.circle,location = location.abovebar,color = color_cande_up_down,size = size.tiny)
plotshape(mm_up ? high[1] : na, style=shape.circle, location=location.belowbar, color=#0ba4f9,title = "Mera UP",size = size.tiny)
plotshape(mm_down ? low[1] : na, style=shape.circle, location=location.abovebar, color=#fd1700, title = "Mera Down", size = size.tiny)

//cảnh báo khi xuất hiện nến/
close_osb_up = close
close_osb_down = close
close_eng_up = close
close_eng_down = close
close_mm_up = close
close_mm_down = close
time_h4 = request.security(syminfo.tickerid, "240",close)
time_D1 = request.security(syminfo.tickerid, "D",close)

if (osb_up and barstate.isconfirmed)
if (close_osb_up > ema)
alert("OSB_UP: Above Ema: " + str.tostring(close_osb_up))
if volUltraHigh
alert("OSB_UP + Volume: Above Ema: " + str.tostring(close_osb_up))
else if (close_osb_up < ema)
alert("OSB_UP: Below Ema: " + str.tostring(close_osb_up))
if volUltraHigh
alert("OSB_UP + Volume: Below Ema: " + str.tostring(close_osb_up))

if (osb_down and barstate.isconfirmed)
if (close_osb_down > ema)
alert("OSB_DOWN: Above Ema: " + str.tostring(close_osb_down))
if volUltraHigh
alert("OSB_DOWN + Volume: Above Ema: " + str.tostring(close_osb_down))
else if (close_osb_down < ema)
alert("OSB_DOWN: Below Ema: " + str.tostring(close_osb_down))
if volUltraHigh
alert("OSB_DOWN + Volume: Below Ema: " + str.tostring(close_osb_down))


if (eng_up and barstate.isconfirmed)
if (close_eng_up > ema)
alert("Bar_UP: Above Ema: " + str.tostring(close_eng_up))
if volUltraHigh
alert("Bar_UP + Volume: Above Ema: " + str.tostring(close_eng_up))
else if (close_eng_up < ema)
alert("Bar_UP: Below Ema: " + str.tostring(close_eng_up))
if volUltraHigh
alert("Bar_UP + Volume: Below Ema: " + str.tostring(close_eng_up))


if (eng_down and barstate.isconfirmed)
if (close_eng_down > ema)
alert("Bar_DOWN: Above Ema: " + str.tostring(close_eng_down))
if volUltraHigh
alert("Bar_DOWN + Volume: Above Ema: " + str.tostring(close_eng_down))
else if (close_eng_down < ema)
alert("Bar_DOWN: Below Ema: " + str.tostring(close_eng_down))
if volUltraHigh
alert("Bar_DOWN + Volume: Below Ema: " + str.tostring(close_eng_down))

if (mm_up and barstate.isconfirmed)
if (close_mm_up > ema)
alert("Mera_UP: Above Ema: " + str.tostring(close_mm_up))
if volUltraHigh
alert("Mera_UP + Volume: Above Ema: " + str.tostring(close_mm_up))
else if (close_mm_up < ema)
alert("Mera_UP: Below Ema: " + str.tostring(close_mm_up))
if volUltraHigh
alert("Mera_UP + Volume: Below Ema: " + str.tostring(close_mm_up))

if (mm_down and barstate.isconfirmed)
if (close_mm_down > ema)
alert("Mera_DOWN: Above Ema: " + str.tostring(close_mm_down))
if volUltraHigh
alert("Mera_DOWN + Volume: Above Ema: " + str.tostring(close_mm_down))
else if (close_mm_down < ema)
alert("Mera_DOWN: Below Ema: " + str.tostring(close_mm_down))
if volUltraHigh
alert("Mera_DOWN + Volume: Below Ema: " + str.tostring(close_mm_down))

//indicator("Initial Balance", shorttitle="Initial Balance", overlay=true, max_bars_back=5000)
ib_session = input.session("2300-0000", title="Initial Balance", group="Calculation period")
show_extra_levels = input.bool(false, "Show extra levels (IBH x2 & IBL x2)", group="Information")
show_intermediate_levels = input.bool(false, "Show intermediate levels (50%)", group="Information")
show_ib_calculation_area = input.bool(false, "Initial balance calculation period coloration", group="Information")
show_labels = input.bool(false, "Show labels", group="Information")
fill_ib_areas = input.bool(false, "Colour IB areas", group="Information")
only_current_levels = input.bool(true, "Only display the current IB Levels", group="Information")
only_current_zone = input.bool(false, "Only display the current IB calculation area", group="Information")

label_size = input.string("Small", title="Label Size", options=["Auto", "Huge", "Large", "Normal", "Small", "Tiny"], group="Drawings")
lvl_width = input.int(1, "Daily price level width", group="Drawings")
high_col = input.color(color.black, "Initial balance high levels color", group="Drawings")
low_col = input.color(color.black, "Initial balance low levels color", group="Drawings")
middle_col = input.color(#ffa726, "50% initial balance color", group="Drawings")
extend_level = input.string("Right", title="Extend current levels", options=["Right", "Left", "Both", "None"], group="Drawings")

main_levels_style = input.string("Solid" , "Main levels line style", options=["Solid", "Dashed", "Dotted"], group="Drawings")
ext_levels_style = input.string("Dashed" , "Extended levels line style", options=["Solid", "Dashed", "Dotted"], group="Drawings")
int_levels_style = input.string("Dotted" , "Intermediate levels line style", options=["Solid", "Dashed", "Dotted"], group="Drawings")

fill_ib_color= input.color(#b8851faa, "IB area background color", group="Drawings")
ext = extend_level == "Right" ? extend.right : extend_level == "Left" ? extend.left : extend_level == "Both" ? extend.both : extend.none

var delta_history = array.new_float(20)

inSession(sess) => na(time(timeframe.period, sess)) == false

get_line_style(s) =>
s == "Solid" ? line.style_solid : s == "Dotted" ? line.style_dotted : line.style_dashed

get_levels(n) =>
h = high[1]
l = low[1]
for i=1 to n
if low < l
l := low
if high > h
h := high
[h, l, (h+l)/2]

var line ibh = na
var line ibl = na
var line ibm = na
var line ib_plus = na
var line ib_minus = na
var line ib_plus2 = na
var line ib_minus2 = na
var line ibm_plus = na
var line ibm_minus = na

var label labelh = na
var label labell = na
var label labelm = na
var label label_plus = na
var label label_minus = na
var label label_plus2 = na
var label label_minus2 = na
var label labelm_plus = na
var label labelm_minus = na
var box ib_area = na

labelSize = (label_size == "Huge") ? size.huge :
(label_size == "Large") ? size.large :
(label_size == "Small") ? size.small :
(label_size == "Tiny") ? size.tiny :
(label_size == "Auto") ? size.auto : size.normal

var offset = 0

ins = inSession(ib_session)

bgcolor(show_ib_calculation_area and ins ? #673ab730 : na, title="IB calculation zone")
var float ib_delta = na

if ins
offset += 1
if ins[1] and not ins
[h, l, m] = get_levels(offset)
ib_delta := h - l
if array.size(delta_history) >= 20
array.shift(delta_history)
array.push(delta_history, ib_delta)

line.set_extend(ibh, extend.none)
line.set_extend(ibl, extend.none)
if show_intermediate_levels
line.set_extend(ibm, extend.none)
if show_extra_levels
line.set_extend(ib_plus, extend.none)
line.set_extend(ib_minus, extend.none)
line.set_extend(ib_plus2, extend.none)
line.set_extend(ib_minus2, extend.none)
if show_intermediate_levels
line.set_extend(ibm_plus, extend.none)
line.set_extend(ibm_minus, extend.none)
if show_labels
if only_current_levels
label.delete(labelh)
label.delete(labell)
label.delete(labelm)
label.delete(label_plus)
label.delete(label_minus)
label.delete(label_plus2)
label.delete(label_minus2)
label.delete(labelm_plus)
label.delete(labelm_minus)
labelh := label.new(bar_index[offset], h, text="IBH 100%: "+str.tostring(h), style=label.style_none, textcolor=high_col, size=labelSize)
labell := label.new(bar_index[offset], l, text="IBL 0%: "+str.tostring(l), style=label.style_none, textcolor=low_col, size=labelSize)
if show_intermediate_levels
labelm := label.new(bar_index[offset], m, text="IBM 50%: "+str.tostring(m)+"\nIBΔ: "+str.tostring(h - l), style=label.style_none, textcolor=middle_col, size=labelSize)
if show_extra_levels
label_plus := label.new(bar_index[offset], h + ib_delta, text="IBH x2 - "+str.tostring(h + ib_delta), style=label.style_none, textcolor=high_col, size=labelSize)
label_minus := label.new(bar_index[offset], l - ib_delta, text="IBL x2: "+str.tostring(l - ib_delta), style=label.style_none, textcolor=low_col, size=labelSize)
label_plus2 := label.new(bar_index[offset], h + (ib_delta*2), text="IBH x3 - "+str.tostring(h + (ib_delta*2)), style=label.style_none, textcolor=high_col, size=labelSize)
label_minus2 := label.new(bar_index[offset], l - (ib_delta*2), text="IBL x3: "+str.tostring(l - (ib_delta*2)), style=label.style_none, textcolor=low_col, size=labelSize)
if fill_ib_areas
if only_current_zone
box.delete(ib_area)
ib_area := box.new(bar_index[offset], h, bar_index, l, bgcolor=fill_ib_color, border_color=#00000000)//, extend=ext)
if only_current_levels
line.delete(ibh)
line.delete(ibl)
line.delete(ibm)
line.delete(ib_plus)
line.delete(ib_minus)
line.delete(ib_plus2)
line.delete(ib_minus2)
line.delete(ibm_plus)
line.delete(ibm_minus)

ibh := line.new(bar_index[offset], h, bar_index, h, color=high_col, extend=ext, width=lvl_width, style=get_line_style(main_levels_style))
ibl := line.new(bar_index[offset], l, bar_index, l, color=low_col, extend=ext, width=lvl_width, style=get_line_style(main_levels_style))
if show_intermediate_levels
ibm := line.new(bar_index[offset], m, bar_index, m, color=middle_col, style=get_line_style(int_levels_style), extend=ext, width=lvl_width)
if show_extra_levels
ib_plus := line.new(bar_index[offset], h + ib_delta, bar_index, h + ib_delta, color=high_col, style=get_line_style(ext_levels_style), extend=ext, width=lvl_width)
ib_minus := line.new(bar_index[offset], l - ib_delta, bar_index, l - ib_delta, color=low_col, style=get_line_style(ext_levels_style), extend=ext, width=lvl_width)
ib_plus2 := line.new(bar_index[offset], h + (ib_delta*2), bar_index, h + (ib_delta *2), color=high_col, style=get_line_style(ext_levels_style), extend=ext, width=lvl_width)
ib_minus2 := line.new(bar_index[offset], l - (ib_delta*2), bar_index, l - (ib_delta*2), color=low_col, style=get_line_style(ext_levels_style), extend=ext, width=lvl_width)
if show_intermediate_levels
ibm_plus := line.new(bar_index[offset], h + (ib_delta/2), bar_index, h + (ib_delta/2), color=middle_col, style=get_line_style(int_levels_style), extend=ext, width=lvl_width)
ibm_minus := line.new(bar_index[offset], l - (ib_delta/2), bar_index, l - (ib_delta/2), color=middle_col, style=get_line_style(int_levels_style), extend=ext, width=lvl_width)
offset := 0

if (not ins) and (not ins[1])
line.set_x2(ibh, bar_index)
line.set_x2(ibl, bar_index)
if show_intermediate_levels
line.set_x2(ibm, bar_index)
if show_extra_levels
line.set_x2(ib_plus, bar_index)
line.set_x2(ib_minus, bar_index)
line.set_x2(ib_plus2, bar_index)
line.set_x2(ib_minus2, bar_index)
if show_intermediate_levels
line.set_x2(ibm_plus, bar_index)
line.set_x2(ibm_minus, bar_index)

var table ib_analytics = table.new(position.bottom_left, 2, 6)

ib_sentiment() =>
h = array.max(delta_history)
l = array.min(delta_history)
a = array.avg(delta_history)

h_comp = ib_delta > h ? ib_delta - h : (ib_delta - h) * -1
l_comp = ib_delta > l ? ib_delta - l : (ib_delta - l) * -1
a_comp = ib_delta > a ? ib_delta - a : (ib_delta - a) * -1
(h_comp < l_comp and h_comp < a_comp) ? "Huge" : (l_comp < h_comp and l_comp < a_comp) ? "Small" : "Medium"


//version=5
//indicator('Key Levels', overlay=true)
group = 'KEYLVL_SETTINGS'
displaystyle = input.string('Standard', ' Display Style', group = group, inline='onest', options=['Standard', 'Right', 'Both', 'Left'])
linestyle = input.string('Solid', ' Line Style ', group = group, inline='twost', options=['Solid', 'Dashed', 'Dotted'])
fontstyle = input.string('Monospace', ' Font Type ', group = group, inline='trest', options=['Monospace', 'Default'])
labelpos = input.string('Middle', ' Label Pos ', group = group, inline=' ', options=['Middle', 'Top'])
bar_offset = input.int (30, ' Offset  ', group = group, inline='onest', minval=0, maxval=450, step = 5)
threshold = input.float (0.1, ' Threshold %', group = group, inline='twost', minval=0, step=0.05, tooltip='Threshold - the distance between the levels after which the labels merge.')
index = input.int (0, ' Index   ', group = group, inline='trest', minval=0, step=1, tooltip='Index - Default 0 allows you to see current and past price levels. Changing to 1 will shift the current levels to the previous levels, and the previous levels to the previous levels, etc.')
color_cD = input.color (#2a89c9, ' cD', group = group, inline='cols1')
color_cW = input.color (#b7d123, ' cW', group = group, inline='cols1')
color_cM = input.color (#8056b8, ' cM', group = group, inline='cols1')
color_cQ = input.color (#c23eb0, ' cQ', group = group, inline='cols1')
color_cY = input.color (#d62e28, ' cY', group = group, inline='cols1')
color_pD = input.color (#2a89c9, ' pD', group = group, inline='cols2')
color_pW = input.color (#b7d123, ' pW', group = group, inline='cols2')
color_pM = input.color (#8056b8, ' pM', group = group, inline='cols2')
color_pQ = input.color (#c23eb0, ' pQ', group = group, inline='cols2')
color_pY = input.color (#d62e28, ' pY', group = group, inline='cols2', tooltip="Prefix 'C' = Current/today. \nPrefix 'P' = Previous/yesterday. \nPrefix 'D' = Day. \nPrefix 'W' = Week. \nPrefix 'M' = Mounth. \nPrefix 'Q' = Quarterly. \nPrefix 'Y' = Year.'" )
i_tG = input.bool (true, ' Global Color Text', group = group, inline='colsg')
color_tG = input.color (color.black, ' ', group = group, inline='colsg')
i_lG = input.bool (true, ' Global Color Line', group = group, inline='colsg')
color_lG = input.color (color.black, ' ', group = group, inline='colsg')

fontstyles = switch fontstyle
"Default" => font.family_default
"Monospace" => font.family_monospace
linestyles = switch linestyle
"Solid" => line.style_solid
"Dashed" => line.style_dashed
"Dotted" => line.style_dotted
displaystyles = switch displaystyle
"Standard" => extend.none
"Right" => extend.right
"Both" => extend.both
"Left" => extend.left

getprice(TimeFrame, Index)=> // Get Open, High, Low, Close, Time data
i = Index
tf = TimeFrame
[O, H, L, C, T] = request.security(syminfo.tickerid, tf, [open, high, low, close, time], lookahead=barmerge.lookahead_on)
[O, H, L, C, T]

[cDopen, cDhigh, cDlow, cDclose, cDtime] = getprice('D', 0+index) // Get Current Day Open, High, Low, Close, Time
[cWopen, cWhigh, cWlow, cWclose, cWtime] = getprice('W', 0+index) // Get Current Weekly Open, High, Low, Close, Time
[cMopen, cMhigh, cMlow, cMclose, cMtime] = getprice('M', 0+index) // Get Current Mounth Open, High, Low, Close, Time
[cQopen, cQhigh, cQlow, cQclose, cQtime] = getprice('3M', 0+index) // Get Current Quarter Open, High, Low, Close, Time
[cYopen, cYhigh, cYlow, cYclose, cYtime] = getprice('12M', 0+index) // Get Current Year Open, High, Low, Close, Time
[pDopen, pDhigh, pDlow, pDclose, pDtime] = getprice('D', 1+index) // Get Previous Day Open, High, Low, Close, Time
[pWopen, pWhigh, pWlow, pWclose, pWtime] = getprice('W', 1+index) // Get Previous Weekly Open, High, Low, Close, Time
[pMopen, pMhigh, pMlow, pMclose, pMtime] = getprice('M', 1+index) // Get Previous Mounth Open, High, Low, Close, Time
[pQopen, pQhigh, pQlow, pQclose, pQtime] = getprice('3M', 1+index) // Get Previous Quarter Open, High, Low, Close, Time
[pYopen, pYhigh, pYlow, pYclose, pYtime] = getprice('12M', 1+index) // Get Previous Year Open, High, Low, Close, Time

extend(bars) => // Extends the current time by the product of the time difference between elements and the number of bars.
timenow + (time - time[1]) * bars

getlevel(time, level, txtcolor, linecolor, txtlabel)=> // Get draws a line and a label on the chart
if barstate.islast // The following code doesn't need to be processed on every candle
xindex = displaystyle == 'Standard' ? time : bar_index+bar_offset
xloc = displaystyle == 'Standard' ? xloc.bar_time : xloc.bar_index
xid = displaystyle == 'Standard' ? extend(bar_offset) : bar_index+bar_offset+1
lblpos = labelpos == 'Middle' ? label.style_label_center : label.style_none
lines = line.new (x1=xindex, x2=xid, y1=level, y2=level, xloc=xloc, style=linestyles, extend=displaystyles, color=linecolor)
labels = label.new(x=xid, y=level, text=txtlabel, style=lblpos, xloc=xloc, text_font_family=fontstyles, textcolor=txtcolor, color = #ffffff00)
line.delete(lines[1]) // remove the previous line when new bar appears
label.delete(labels[1]) // remove the previous label when new bar appears
[lines, labels]

// Get Inputs, Lines, Labels
i_cDO = input(false, 'DO ', group = group, inline='D'), [cDayOpen , cDOlabel] = getlevel(cDtime, i_cDO ? cDopen : na, i_tG ? color_tG : color_cD, i_lG ? color_lG : color_cD, 'DO' )
i_cDH = input(false, 'DH ', group = group, inline='D'), [cDayHigh , cDHlabel] = getlevel(cDtime, i_cDH ? cDhigh : na, i_tG ? color_tG : color_cD, i_lG ? color_lG : color_cD, 'DH' )
i_cDL = input(false, 'DL ', group = group, inline='D'), [cDayLow , cDLlabel] = getlevel(cDtime, i_cDL ? cDlow : na, i_tG ? color_tG : color_cD, i_lG ? color_lG : color_cD, 'DL' )
i_pDO = input(false, 'pDO', group = group, inline='D'), [pDayOpen , pDOlabel] = getlevel(pDtime, i_pDO ? pDopen : na, i_tG ? color_tG : color_pD, i_lG ? color_lG : color_pD, 'pDO' )
i_pDH = input(true, 'pDH', group = group, inline='D'), [pDayHigh , pDHlabel] = getlevel(pDtime, i_pDH ? pDhigh : na, i_tG ? color_tG : color_pD, i_lG ? color_lG : color_pD, 'pDH' )
i_pDL = input(true, 'pDL', group = group, inline='D'), [pDayLow , pDLlabel] = getlevel(pDtime, i_pDL ? pDlow : na, i_tG ? color_tG : color_pD, i_lG ? color_lG : color_pD, 'pDL' )

i_cWO = input(false, 'WO ', group = group, inline='W'), [cWekOpen , cWOlabel] = getlevel(cWtime, i_cWO ? cWopen : na, i_tG ? color_tG : color_cW, i_lG ? color_lG : color_cW, 'WO' )
i_cWH = input(false, 'WH ', group = group, inline='W'), [cWekHigh , cWHlabel] = getlevel(cWtime, i_cWH ? cWhigh : na, i_tG ? color_tG : color_cW, i_lG ? color_lG : color_cW, 'WH' )
i_cWL = input(false, 'WL ', group = group, inline='W'), [cWekLow , cWLlabel] = getlevel(cWtime, i_cWL ? cWlow : na, i_tG ? color_tG : color_cW, i_lG ? color_lG : color_cW, 'WL' )
i_pWO = input(false, 'pWO', group = group, inline='W'), [pWekOpen , pWOlabel] = getlevel(pWtime, i_pWO ? pWopen : na, i_tG ? color_tG : color_pW, i_lG ? color_lG : color_pW, 'pWO' )
i_pWH = input(false, 'pWH', group = group, inline='W'), [pWekHigh , pWHlabel] = getlevel(pWtime, i_pWH ? pWhigh : na, i_tG ? color_tG : color_pW, i_lG ? color_lG : color_pW, 'pWH' )
i_pWL = input(false, 'pWL', group = group, inline='W'), [pWekLow , pWLlabel] = getlevel(pWtime, i_pWL ? pWlow : na, i_tG ? color_tG : color_pW, i_lG ? color_lG : color_pW, 'pWL' )

i_cMO = input(false, 'MO ', group = group, inline='M'), [cMonOpen , cMOlabel] = getlevel(cMtime, i_cMO ? cMopen : na, i_tG ? color_tG : color_cM, i_lG ? color_lG : color_cM, 'MO' )
i_cMH = input(false, 'MH ', group = group, inline='M'), [cMonHigh , cMHlabel] = getlevel(cMtime, i_cMH ? cMhigh : na, i_tG ? color_tG : color_cM, i_lG ? color_lG : color_cM, 'MH' )
i_cML = input(false, 'ML ', group = group, inline='M'), [cMonLow , cMLlabel] = getlevel(cMtime, i_cML ? cMlow : na, i_tG ? color_tG : color_cM, i_lG ? color_lG : color_cM, 'ML' )
i_pMO = input(false, 'pMO', group = group, inline='M'), [pMonOpen , pMOlabel] = getlevel(pMtime, i_pMO ? pMopen : na, i_tG ? color_tG : color_pM, i_lG ? color_lG : color_pM, 'pMO' )
i_pMH = input(false, 'pMH', group = group, inline='M'), [pMonHigh , pMHlabel] = getlevel(pMtime, i_pMH ? pMhigh : na, i_tG ? color_tG : color_pM, i_lG ? color_lG : color_pM, 'pMH' )
i_pML = input(false, 'pML', group = group, inline='M'), [pMonLow , pMLlabel] = getlevel(pMtime, i_pML ? pMlow : na, i_tG ? color_tG : color_pM, i_lG ? color_lG : color_pM, 'pML' )

i_cQO = input(false, 'QO ', group = group, inline='Q'), [cQonOpen , cQOlabel] = getlevel(cQtime, i_cQO ? cQopen : na, i_tG ? color_tG : color_cQ, i_lG ? color_lG : color_cQ, 'QO' )
i_cQH = input(false, 'QH ', group = group, inline='Q'), [cQonHigh , cQHlabel] = getlevel(cQtime, i_cQH ? cQhigh : na, i_tG ? color_tG : color_cQ, i_lG ? color_lG : color_cQ, 'QH' )
i_cQL = input(false, 'QL ', group = group, inline='Q'), [cQonLow , cQLlabel] = getlevel(cQtime, i_cQL ? cQlow : na, i_tG ? color_tG : color_cQ, i_lG ? color_lG : color_cQ, 'QL' )
i_pQO = input(false, 'pQO', group = group, inline='Q'), [pQonOpen , pQOlabel] = getlevel(pQtime, i_pQO ? pQopen : na, i_tG ? color_tG : color_pQ, i_lG ? color_lG : color_pQ, 'pQO' )
i_pQH = input(false, 'pQH', group = group, inline='Q'), [pQonHigh , pQHlabel] = getlevel(pQtime, i_pQH ? pQhigh : na, i_tG ? color_tG : color_pQ, i_lG ? color_lG : color_pQ, 'pQH' )
i_pQL = input(false, 'pQL', group = group, inline='Q'), [pQonLow , pQLlabel] = getlevel(pQtime, i_pQL ? pQlow : na, i_tG ? color_tG : color_pQ, i_lG ? color_lG : color_pQ, 'pQL' )

i_cYO = input(false, 'YO ', group = group, inline='Y'), [cYonOpen , cYOlabel] = getlevel(cYtime, i_cYO ? cYopen : na, i_tG ? color_tG : color_cY, i_lG ? color_lG : color_cY, 'YO' )
i_cYH = input(false, 'YH ', group = group, inline='Y'), [cYonHigh , cYHlabel] = getlevel(cYtime, i_cYH ? cYhigh : na, i_tG ? color_tG : color_cY, i_lG ? color_lG : color_cY, 'YH' )
i_cYL = input(false, 'YL ', group = group, inline='Y'), [cYonLow , cYLlabel] = getlevel(cYtime, i_cYL ? cYlow : na, i_tG ? color_tG : color_cY, i_lG ? color_lG : color_cY, 'YL' )
i_pYO = input(false, 'pYO', group = group, inline='Y'), [pYonOpen , pYOlabel] = getlevel(pYtime, i_pYO ? pYopen : na, i_tG ? color_tG : color_pY, i_lG ? color_lG : color_pY, 'pYO' )
i_pYH = input(false, 'pYH', group = group, inline='Y'), [pYonHigh , pYHlabel] = getlevel(pYtime, i_pYH ? pYhigh : na, i_tG ? color_tG : color_pY, i_lG ? color_lG : color_pY, 'pYH' )
i_pYL = input(false, 'pYL', group = group, inline='Y'), [pYonLow , pYLlabel] = getlevel(pYtime, i_pYL ? pYlow : na, i_tG ? color_tG : color_pY, i_lG ? color_lG : color_pY, 'pYL' )

// Сollecting labels into an array for normalization
labels = array.from(cDOlabel,cDHlabel,cDLlabel,pDOlabel,pDHlabel,pDLlabel,cWOlabel,cWHlabel,cWLlabel,pWOlabel,pWHlabel,pWLlabel,cMOlabel,cMHlabel,cMLlabel,pMOlabel,pMHlabel,pMLlabel,cQOlabel,cQHlabel,cQLlabel,pQOlabel,pQHlabel,pQLlabel,cYOlabel,cYHlabel,cYLlabel,pYOlabel,pYHlabel,pYLlabel)

// This loop merge items in arrays "labels" into a single label if their difference is less than a certain threshold, and updates the label text.
for i = 0 to array.size(labels) - 2 // Loop through all the labels
txt = label.get_text(array.get(labels, i)) // Get the text of the current label
price_i = label.get_y(array.get(labels, i)) // Get the y coordinate of the current label
label_i = array.get(labels, i) // Store the current label
for j = i + 1 to array.size(labels) - 1 // Loop through the remaining labels
price_j = label.get_y(array.get(labels, j)) // Get the y coordinate of the current label
if math.abs(price_i - price_j) < (threshold/100) * price_i // Compare the prices
txt := txt + ' / ' + label.get_text(array.get(labels, j)) // Add the text of the second label
label.delete(array.get(labels, j)) // Delete the second label
label.set_text(label_i, txt) // Set the text of the current label

repeat(str, num) => // Returns a string that is created by repeating the input str num times
res = ""
len = str.length(str)
if num > 0 and len * num <= 4096
for r = 1 to num
res := res + str
res
// Loop to change the position of the Label
if labelpos == 'Middle'
for z = 0 to array.size(labels) - 1
label_z = array.get(labels, z) // Get the label at position z
texts = label.get_text(label_z) // Get the text of the label
spaces = fontstyle=='Default' ? repeat(' ', math.ceil(str.length(texts))*2) : repeat(' ', math.ceil(str.length(texts))) // Calculate the number of spaces needed
label.set_text(label_z, displaystyle=='Right'? texts + spaces + ' ' : spaces+' ' + texts) // Set the label text with the calculated spaces
//

//version=5
//indicator("Congestion Zone", overlay=true)

Congestion_Zone = "Congestio_Zone"
Show_CZ = input.bool(true, title="On/off Congestion Zone", group="Congestion_Zone")
minbar = input(title="Minimum Bars in Zone", defval=5, group="Congestion_Zone", inline="Box",tooltip = "Số lượng nến tối thiểu để có 1 vùng CZ")
border_width = input.int(title="CZ boder width", defval=1, inline="Box1", group="Congestion_Zone", step=1,tooltip = "Viền CZ")
max_boxes = input.int(title="Max CZ", defval=50, group="Congestion_Zone", minval=1,tooltip = "Số vùng box tối đa muốn hiển thị để tránh rối mắt")

boxcolor_basic = input.color(title="CZ Color Basic", defval=color.new(color.orange, 80), group="Congestion_Zone",tooltip = "Nến thoát khỏi box không có xu hướng rõ ràng")
boxcolor_up = input.color(title="CZ Color Up", defval=color.new(color.green, 80), group="Congestion_Zone",tooltip = "Nến thoát khỏi box có giá đóng cửa > vùng CZ")
boxcolor_down = input.color(title="CZ Color Dowwn", defval=color.new(color.red, 80), group="Congestion_Zone",tooltip = "Nến thoát khỏi box có đóng cửa < vùng CZ")

var boxopen = array.new_float()
var boxclose = array.new_float()
var boxes = array.new_box()

congestion_condition = close >= low[1] and close <= high[1] and open >= low[1] and open <= high[1]

if congestion_condition and Show_CZ
array.push(boxopen, open > close ? open : close)
array.push(boxclose, open > close ? close : open)
else if array.size(boxopen) < minbar
array.clear(boxopen)
array.clear(boxclose)

if not congestion_condition and array.size(boxopen) >= minbar and Show_CZ
box_max = array.max(boxopen)
box_min = array.min(boxclose)
box_color = close >= box_max ? boxcolor_up : close <= box_min ? boxcolor_down : boxcolor_basic
if box_color == boxcolor_basic
alert("CZ basic")
else if box_color == boxcolor_up
alert("CZ UP")
else if box_color == boxcolor_down
alert("CZ DOWN")
// Determine box color based on last candle
new_box = box.new(bar_index - array.size(boxopen), box_max, bar_index - 1, box_min, bgcolor=box_color, border_width=border_width,border_color = box_color)
array.push(boxes, new_box)
array.clear(boxopen)
array.clear(boxclose)

//Remove old boxes if limit is reached
while array.size(boxes) > max_boxes
box.delete(array.shift(boxes))

Bands and Channels

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?

Disclaimer