Congestionautilus
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Rumost ...dedicato ai simpatici amici del gruppo di supporto ;-)
//@version=5
indicator("Congestionautilus", overlay = true, max_boxes_count = 500)

ZoneColor = input(defval = color.new(#ffde4b, 90), title = "Area")
BorderColor = input(defval = color.new(#795c1e, 35), title = "Border")
ExtendColor = input(defval = color.new(#795c1e, 35), title = "Extension")
Extend = input(defval = true, title = "Extend the last congestion")

var CHigh = array.new_float(0)
var CLow = array.new_float(0)

var ALine = line.new(1,0,1,0,color=na)
var BLine = line.new(1,0,1,0,color=na)
var E1Line = line.new(1,0,1,0,color=na)
var E2Line = line.new(1,0,1,0,color=na)
var xBox = box.new(1,0,1,0,bgcolor = na, border_color = na)

var count = 3

congestionCondtion() =>
    if ( array.size(CHigh) == 0 )
        close[1] >= low[2] and close[1] <= high[2] and open[1] >= low[2] and open[1] <= high[2]
    else
        close[1] >= array.get(CLow, 0) and close[1] <= array.get(CHigh, 0) and open[1] >= array.get(CLow, 0) and open[1] <= array.get(CHigh, 0)

reset() =>
    array.clear(CHigh)
    array.clear(CLow)
    box.delete(xBox)
    line.delete(ALine)
    line.delete(BLine)

counter() =>
    counter=array.size(CHigh)

if ( congestionCondtion()  )
    array.push(CHigh, high[2])
    array.push(CLow, low[2])

else if( counter() < count )
    reset()

if( counter() >= count and congestionCondtion() == false )
    float maxCZ = array.get(CHigh, 0)
    float minCZ = array.get(CLow, 0)
    box.new(bar_index - counter()-2, maxCZ, bar_index-2, minCZ, bgcolor = ZoneColor, border_color = BorderColor)
    reset()
else if( counter() >= count and congestionCondtion() == true )
    float maxCZ = array.get(CHigh, 0)
    float minCZ = array.get(CLow, 0)
    line.delete(ALine)
    line.delete(BLine)
    box.delete(xBox)
    xBox:=box.new(bar_index - counter()-1, maxCZ, bar_index+1, minCZ, bgcolor = ZoneColor, border_color = na)
    ALine:=line.new(bar_index - counter()-1, maxCZ, bar_index+1, maxCZ, color=BorderColor, style=line.style_solid, width=1, xloc=xloc.bar_index)
    BLine:=line.new(bar_index - counter()-1, minCZ, bar_index+1, minCZ, color=BorderColor, style=line.style_solid, width=1, xloc=xloc.bar_index)
    if (Extend)
        line.delete(E1Line)
        line.delete(E2Line)
        E1Line:=line.new(bar_index, maxCZ, bar_index+1, maxCZ, color=ExtendColor, style=line.style_dashed, width=1, xloc=xloc.bar_index,extend=extend.right)
        E2Line:=line.new(bar_index, minCZ, bar_index+1, minCZ, color=ExtendColor, style=line.style_dashed, width=1, xloc=xloc.bar_index,extend=extend.right)

