屏幕上应该只显示 10 个区域,但它显示了另外两个

Only 10 zones should be displayed on the screen, but it displays two more

首先,在 BINANCE:BTCUSDT 每日时间范围内测试代码。我用蓝色和黄色条形颜色标记了这些区域,这样您可以更清楚地看到它们。

问题是,当您 select 输入 10 个区域(默认)和“不收缩”选项(默认)时,代码会被喝醉并显示另外两个甚至不是下一个的区域一个,但在他们之后有两支蜡烛。我相信这是因为看涨和看跌区域的数组不同。它必须以某种方式重新编码以显示正确的区域。我在下图中标记了应该显示的区域。

编辑:

我认为它实际上显示了 5 个红色区域和 5 个绿色区域。我希望它显示 10 个区域,无论它们是看涨还是看跌。

当前

预计

https://www.tradingview.com/x/4DOGjqSs/

//@version=5
indicator("Imb", overlay = true, max_boxes_count = 500, max_bars_back = 500)

// ———————————————————— Constants {
// ————— Colors
var color GREEN         = color.green
var color RED           = color.red
var color BLUE          = color.blue
var color YELLOW        = color.yellow

// ————— Constants used in inputs
var string G1 = "Show"
var string G2 = "Hide"

var string IMB1 = "Shrink when mitigated"
var string IMB2 = "Do not shrink"
// }

// ———————————————————— Inputs {
// ————— Imbalance Zones
var GRP3 = "Imbalance Zones"
bool showImbalance = input.bool(true, "Show Imbalance/Fair Value Gaps", inline = "30", group = GRP3)
int numberOfZonesInput = input.int(10, "# Zones", inline = "31", group = GRP3)
bool updateZonesWithPriceMovement = input.string(IMB2, "", inline = "32", options = [IMB1, IMB2], group = GRP3) == IMB1
color bullishImbColorInput = input.color(GREEN, "", inline = "32", group = GRP3)
color bearishImbColorInput = input.color(RED, "", inline = "32", group = GRP3)
int imbTranspInput = input.int(80, "Transparency", inline = "32", group = GRP3)
// }

// ———————————————————— Imbalance/Fair Value Gaps {
var line bullishImbOpen = na
var line bullishImbLow = na

var line bearishImbOpen = na
var line bearishImbHigh = na

bullishImb = low > high[2]
bearishImb = high < low[2]

barcolor(bullishImb ? BLUE : bearishImb ? YELLOW : na, offset = -1, title = "Imbalance Bar Color")

plotshape(bullishImb ? bullishImb : na, style = shape.triangleup, location = location.belowbar, color = GREEN, offset = -1, size = size.tiny)
plotshape(bullishImb ? bearishImb : na, style = shape.triangledown, location = location.abovebar, color = RED, offset = -1, size = size.tiny)

var box[] bullishImbBox = array.new_box()
var box[] bearishImbBox = array.new_box()

var color bullColor = color.new(bullishImbColorInput, imbTranspInput)
var color bearColor = color.new(bearishImbColorInput, imbTranspInput)

if bullishImb and showImbalance
    array.push(bullishImbBox, box.new(left = time[1], top = low, right = time, bottom = high[2], extend = extend.none, xloc = xloc.bar_time, border_color = bullColor, border_style = line.style_dashed, bgcolor = bullColor))
    if array.size(bullishImbBox) + array.size(bearishImbBox) > numberOfZonesInput
        box.delete(array.shift(bullishImbBox))

if bearishImb and showImbalance
    array.push(bearishImbBox, box.new(left = time[1], top = low[2], right = time, bottom = high, extend = extend.none, xloc = xloc.bar_time, border_color = bearColor, border_style = line.style_dashed, bgcolor = bearColor))
    if array.size(bullishImbBox) + array.size(bearishImbBox) > numberOfZonesInput
        box.delete(array.shift(bearishImbBox))

if array.size(bullishImbBox) > 0
    for i = 0 to array.size(bullishImbBox) - 1
        if i <= array.size(bullishImbBox) - 1 and array.size(bullishImbBox) > 0
            t = box.get_top(array.get(bullishImbBox, i))
            b = box.get_bottom(array.get(bullishImbBox, i))
            
            // update right side of the box
            box.set_right(array.get(bullishImbBox, i), time)
            
            if updateZonesWithPriceMovement
                // delete box
                if b >= low
                    box.delete(array.remove(bullishImbBox, i))
                // update box
                else if t >= low
                    box.set_top(array.get(bullishImbBox, i), low)


if array.size(bearishImbBox) > 0
    for i = 0 to array.size(bearishImbBox) - 1
        if i <= array.size(bearishImbBox) - 1 and array.size(bearishImbBox) > 0
            t = box.get_top(array.get(bearishImbBox, i))
            b = box.get_bottom(array.get(bearishImbBox, i))
            
            // update right side of the box
            box.set_right(array.get(bearishImbBox, i), time)
            
            if updateZonesWithPriceMovement
                // delete box        
                if t <= high
                    box.delete(array.remove(bearishImbBox, i))
                // update box
                else if b <= high
                    box.set_bottom(array.get(bearishImbBox, i), high)
// }

当您通过检查并创建新框时,如果您的尺寸超过允许的10个框数,您将删除最早创建的同类型框,而在图表上它不一定是最先创建的两种类型,第一个创建的可以是不同类型的框,您将从中间删除框。您的所有框都被扩展到系列结束,在您的屏幕截图中,您可以看到框 9 和 6 被删除,而不是相反类型的框。

您需要从两种类型的公共集合中删除第一个创建的框

if bullishImb and showImbalance
    array.push(bullishImbBox, box.new(left = time[1], top = low, right = time, bottom = high[2], extend = extend.none, xloc = xloc.bar_time, border_color = bullColor, border_style = line.style_dashed, bgcolor = bullColor))
    if array.size(bullishImbBox) + array.size(bearishImbBox) > numberOfZonesInput
        box.delete(array.shift(box.all))

if bearishImb and showImbalance
    array.push(bearishImbBox, box.new(left = time[1], top = low[2], right = time, bottom = high, extend = extend.none, xloc = xloc.bar_time, border_color = bearColor, border_style = line.style_dashed, bgcolor = bearColor))
    if array.size(bullishImbBox) + array.size(bearishImbBox) > numberOfZonesInput
        box.delete(array.shift(box.all))
//@version=5
indicator("Imb", overlay = true, max_boxes_count = 500, max_bars_back = 500)

// ———————————————————— Constants {
// ————— Colors
var color GREEN         = color.green
var color RED           = color.red
var color BLUE          = color.blue
var color YELLOW        = color.yellow

// ————— Constants used in inputs
var string G1 = "Show"
var string G2 = "Hide"

var string IMB1 = "Shrink when mitigated"
var string IMB2 = "Do not shrink"
// }

// ———————————————————— Inputs {
// ————— Imbalance Zones
var GRP3 = "Imbalance Zones"
bool showImbalance = input.bool(true, "Show Imbalance/Fair Value Gaps", inline = "30", group = GRP3)
int numberOfZonesInput = input.int(10, "# Zones", inline = "31", group = GRP3)
bool updateZonesWithPriceMovement = input.string(IMB2, "", inline = "32", options = [IMB1, IMB2], group = GRP3) == IMB1
color bullishImbColorInput = input.color(GREEN, "", inline = "32", group = GRP3)
color bearishImbColorInput = input.color(RED, "", inline = "32", group = GRP3)
int imbTranspInput = input.int(80, "Transparency", inline = "32", group = GRP3)
// }

// ———————————————————— Imbalance/Fair Value Gaps {
var line bullishImbOpen = na
var line bullishImbLow = na

var line bearishImbOpen = na
var line bearishImbHigh = na

bullishImb = low > high[2]
bearishImb = high < low[2]

barcolor(bullishImb ? BLUE : bearishImb ? YELLOW : na, offset = -1, title = "Imbalance Bar Color")

plotshape(bullishImb ? bullishImb : na, style = shape.triangleup, location = location.belowbar, color = GREEN, offset = -1, size = size.tiny)
plotshape(bullishImb ? bearishImb : na, style = shape.triangledown, location = location.abovebar, color = RED, offset = -1, size = size.tiny)

var box[] ImbBoxes = array.new_box()
var bool[] ImbType = array.new_bool()

var color bullColor = color.new(bullishImbColorInput, imbTranspInput)
var color bearColor = color.new(bearishImbColorInput, imbTranspInput)

if bullishImb and showImbalance
    array.push(ImbBoxes, box.new(left = time[1], top = low, right = time, bottom = high[2], extend = extend.none, xloc = xloc.bar_time, border_color = bullColor, border_style = line.style_dashed, bgcolor = bullColor))
    array.push(ImbType, true)

if bearishImb and showImbalance
    array.push(ImbBoxes, box.new(left = time[1], top = low[2], right = time, bottom = high, extend = extend.none, xloc = xloc.bar_time, border_color = bearColor, border_style = line.style_dashed, bgcolor = bearColor))
    array.push(ImbType, false)

if array.size(ImbBoxes) > 0
    for i = array.size(ImbBoxes) - 1 to 0
        typeIsBull = array.get(ImbType, i)
        t = box.get_top(array.get(ImbBoxes, i))
        b = box.get_bottom(array.get(ImbBoxes, i))
        box.set_right(array.get(ImbBoxes, i), time)
        if updateZonesWithPriceMovement
            if typeIsBull
                if b >= low
                    box.delete(array.remove(ImbBoxes, i))
                    array.remove(ImbType, i)
                else if t >= low
                    box.set_top(array.get(ImbBoxes,i), low)
            else
                if t <= high
                    box.delete(array.remove(ImbBoxes, i))
                    array.remove(ImbType, i)
                else if b <= high
                    box.set_bottom(array.get(ImbBoxes, i), high)

if array.size(ImbBoxes) > numberOfZonesInput
    box.delete(array.shift(ImbBoxes))
    array.shift(ImbType)