无法更新框的右侧参数以指向当前柱

Unable to update the box's right parameter to point the current bar

我希望区域被绘制到当前柱线,就像下面的图片一样。让我解释一下这个问题。我正在将新框添加到数组中,此时当前柱是创建框后的下一个柱,每当出现新柱时,它不会更新框的 right 方向。

我试图通过添加以下内容来更新循环中的框:box.set_bottom(array.get(array.get(bearishImbBox, i), time),但实际上什么也没发生。实际上一切都消失了,这意味着它是不正确的。

当前行为

预期行为

片段

if bearishImb
    array.push(bearishImbBox, box.new(left = time[1], top = low[2], right = time, bottom = high, extend = extend.none,
     xloc = xloc.bar_time, border_color = color.new(bearishImbColorInput, imbTranspInput), border_style = line.style_dashed, bgcolor = color.new(bearishImbColorInput, imbTranspInput)))
    if array.size(bearishImbBox) > numberOfZonesInput
        box.delete(array.shift(bearishImbBox))
   
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))
            // 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)

您正在将蜡烛当前时间右侧的方框推入数组。这就是为什么它们这么短。在您的方框上循环时,您必须更新方框并将右侧设置为当时正在处理的蜡烛的当前时间。此外,出于性能原因,您可以考虑仅在 barstate.islast

上执行此操作
//@version=5
if bearishImb
    array.push(bearishImbBox, box.new(left = time[1], top = low[2], right = time, bottom = high, extend = extend.none,
     xloc = xloc.bar_time, border_color = color.new(bearishImbColorInput, imbTranspInput), border_style = line.style_dashed, bgcolor = color.new(bearishImbColorInput, imbTranspInput)))
    if array.size(bearishImbBox) > numberOfZonesInput
        box.delete(array.shift(bearishImbBox))
   
if barstate.islast and 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))
            box.set_right(array.get(bearishImbBox, i), time) // this was added
            // 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)

性能优化

//@version=5
if bearishImb
    array.push(bearishImbBox, box.new(left = time[1], top = low[2], right = time, bottom = high, extend = extend.none,
     xloc = xloc.bar_time, border_color = color.new(bearishImbColorInput, imbTranspInput), border_style = line.style_dashed, bgcolor = color.new(bearishImbColorInput, imbTranspInput)))
    if array.size(bearishImbBox) > numberOfZonesInput
        box.delete(array.shift(bearishImbBox))

var box myBox = na

if barstate.islast
    arraySize = array.size(bearishImbBox)
    if arraySize > 0
        for i = 0 to arraySize-1
            if i <= arraySize-1 and arraySize > 0
                myBox := array.get(bearishImbBox, i)
                t = box.get_top(myBox)
                b = box.get_bottom(myBox)
                box.set_right(myBox, time) // this was added
                // delete Box        
                if  t <= high
                    box.delete(array.remove(bearishImbBox, i))
                // update Box
                else if b <= high
                    box.set_bottom(myBox, high)

屏幕上最大框数不能超过numberOfZonesInput

//@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(IMB1, "", 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]

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)
// }