Gap
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ArthurBuyStock

//@version=5
indicator("Gap", overlay = true)
b = request.security(syminfo.tickerid, 'W', bar_index, lookahead = barmerge.lookahead_on)
h = request.security(syminfo.tickerid, 'D', high[1], lookahead = barmerge.lookahead_on)
l = request.security(syminfo.tickerid, 'D', low[1], lookahead = barmerge.lookahead_on)
string nintendo = input.string("True", "Type", options = ["True", "Fix", "Intuition"])
var box[] bricks = array.new_box()
switch nintendo
	"True" => 
	    if b != b[1]
            if open > high[1]
                array.unshift(bricks, box.new(bar_index - 1, open, bar_index, close[1], na, extend  = extend.right, bgcolor = na))
            else if open < low[1]
                array.unshift(bricks, box.new(bar_index - 1, close[1], bar_index, open, na, extend  = extend.right, bgcolor = na))
	"Fix" => 
	    if b != b[1]
            if open > h
                array.unshift(bricks, box.new(bar_index - 1, open, bar_index, close[1], na, extend  = extend.right, bgcolor = na))
            else if open < l
                array.unshift(bricks, box.new(bar_index - 1, close[1], bar_index, open, na, extend  = extend.right, bgcolor = na))
	"Intuition" =>
        if open > high[1]
            array.unshift(bricks, box.new(bar_index - 1, open, bar_index, high[1], na, extend  = extend.right, bgcolor = na))
        else if open < low[1]
            array.unshift(bricks, box.new(bar_index - 1, low[1], bar_index, open, na, extend  = extend.right, bgcolor = na))
if array.size(bricks) > 0
    if array.size(bricks) == 1
        if high >= box.get_top(array.get(bricks,0)) and low <= box.get_bottom(array.get(bricks,0))
            box.delete(array.get(bricks,0))
            array.remove(bricks,0)
        else
            if b == b[1]
                if low[1] <= box.get_bottom(array.get(bricks,0)) and high >= box.get_top(array.get(bricks,0)) or high[1] >= box.get_top(array.get(bricks,0)) and low <= box.get_bottom(array.get(bricks,0))
                    box.delete(array.get(bricks,0))
                    array.remove(bricks,0)
    else
        t = 0
        for i = 0 to array.size(bricks) - 1
            if high >= box.get_top(array.get(bricks,i - t)) and low <= box.get_bottom(array.get(bricks,i - t))
                box.delete(array.get(bricks,i - t))
                array.remove(bricks,i - t)
                t := t + 1
            else
                if i > 0
                    if low[1] <= box.get_bottom(array.get(bricks,i - t)) and high >= box.get_top(array.get(bricks,i - t)) or high[1] >= box.get_top(array.get(bricks,i - t)) and low <= box.get_bottom(array.get(bricks,i - t))
                        box.delete(array.get(bricks,i - t))
                        array.remove(bricks,i - t)
                        t := t + 1
if array.size(bricks) > 0
    r = 0
    g = 0
    for i = 0 to array.size(bricks) - 1
        if high < box.get_top(array.get(bricks,i)) and low > box.get_bottom(array.get(bricks,i))
            if high[1] <= box.get_bottom(array.get(bricks,i))
                box.set_bottom(array.get(bricks, i), high)
            if low[1] >= box.get_top(array.get(bricks,i))
                box.set_top(array.get(bricks, i), low)
        else
            if box.get_top(array.get(bricks,i)) > low and low > box.get_bottom(array.get(bricks,i))
                box.set_top(array.get(bricks, i), low)
            if box.get_top(array.get(bricks,i)) > high and high > box.get_bottom(array.get(bricks,i))
                box.set_bottom(array.get(bricks, i), high)
        if close >= box.get_top(array.get(bricks,i))
            box.set_bgcolor(array.get(bricks, i), color.new(#ff595e, 20 + g >= 100 ? 90 : 20 + g))
            g := g + 20
        else if close <= box.get_bottom(array.get(bricks,i))
            box.set_bgcolor(array.get(bricks, i), color.new(#8ac926, 20 + r >= 100 ? 90 : 20 + r))
            r := r + 20
